0% found this document useful (0 votes)
77 views18 pages

Ansible Fundamentals To Advance

Uploaded by

trailokya1089
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views18 pages

Ansible Fundamentals To Advance

Uploaded by

trailokya1089
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Ansible

fun
da
mentals

github.com/omerbsezer
Ansible is an open-source automation tool

Ansible automates tasks and commands to


manage multiple nodes (servers, PCs)

commands, tasks, codes turn into the


infrastructure as code (IaC)

savable, versionable, repeatable and


testable codes with IaC

agentless: on the worker node, any agent app


is not required to run

documentation: https://docs.ansible.com

linkedin.com/in/omerberatsezer
two types of nodes (servers)
1. control node (master)
2. worker nodes

control node communicates


with remote nodes
via SSH (for Linux and Windows)
or WinRM (for Windows)

linkedin.com/in/omerberatsezer
SSH keys can be used
SSH private key on control node
SSH public key on worker nodes

linkedin.com/in/omerberatsezer
install Ansible

Ansible installed on a control node

the control node manages target


machines via SSH
without requiring any agents
on the worker nodes

user@ansible:$ sudo apt update && sudo apt install ansible -y


# On Debian/Ubuntu
[user@ansible ~]:# sudo yum install ansible -y
# On CentOS/RHEL

linkedin.com/in/omerberatsezer
configuration
(ansible.cfg)

defining global settings


like inventory location,
SSH connection details,
and plugin paths
ansible.cfg

[defaults]
inventory = ./inventory # inventory file path
private_key_file = ~/.ssh/id_rsa # private SSH key path
remote_user = ubuntu # defines the default SSH user
host_key_checking = False # SSH host key verification required?
retry_files_enabled = False # retry files are disabled
log_path = /var/log/ansible.log # to save logs of Ansible runs

linkedin.com/in/omerberatsezer
inventory

an inventory file lists target


hosts with IP or DNS name
and their grouping for Ansible operations

inventories/inventory

[webservers]
web1 ansible_ssh_host=192.168.1.10 ansible_user=ubuntu
web2 ansible_ssh_host=192.168.1.11 ansible_user=ubuntu
[databases]
db1 ansible_ssh_host=192.168.1.20 ansible_user=root

linkedin.com/in/omerberatsezer
inventory
(test)

to test inventory and Ansible

user@ansible:$ ansible all -m ping


# Ping all hosts in the inventory
user@ansible:$ ansible webservers -a "uptime"
# Run uptime command on web server

linkedin.com/in/omerberatsezer
playbook

a YAML file defining tasks, roles, or workflows


for managing hosts

playbooks are declarative and describe the


desired state of systems

linkedin.com/in/omerberatsezer
playbook
(yaml file)

deploy.yml
- name: Install and start Apache
hosts: webservers # select on which group of servers to run (inventory)
become: yes # run tasks with elevated privileges (sudo)
tasks:
- name: Install Apache
apt: # apt module
name: apache2
state: present # apt module parameter (to install)
- name: Ensure Apache is running
service:
name: apache2
state: started

linkedin.com/in/omerberatsezer
playbook
(run playbook)

user@ansible:$ ansible-playbook playbooks/deploy.yml


# run the playbook
PLAY [linux] *******************************************************************
TASK [Gathering Facts]*********************************************************
ok: [web1]
ok: [web2]
TASK [Install Apache]**********************************************************
changed: [web1]
changed: [web2]
TASK [Ensure Apache is running]************************************************
changed: [web1]
changed: [web2]
PLAY RECAP ******************************************************************
web : ok=2 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

linkedin.com/in/omerberatsezer
roles
(tasks)

roles in Ansible are reusable


units that encapsulate a set of tasks,
templates, variables
tasks/main.yml

---
- name: Install nginx
apt:
name: nginx
state: present
update_cache: yes
become: yes
notify: start nginx # notify the handler to start nginx after installing

linkedin.com/in/omerberatsezer
roles
(defaults)

defaults represent
default values

defaults/main.yml
---
nginx_port: 80
nginx_server_name: "localhost"

linkedin.com/in/omerberatsezer
roles
(handlers)

handler triggered in
tasks/main.yaml with notify

handlers/main.yml
---
- name: start nginx
service:
name: nginx
state: started
enabled: yes # ensure nginx starts on boot
become: yes

linkedin.com/in/omerberatsezer
roles
(playbook)

playbook is required
to call role
role_playbook.yml
---
- name: Install and start nginx
hosts: webservers
become: yes

roles:
- install_nginx # includes the install_nginx, to call role

linkedin.com/in/omerberatsezer
Ansible project
file structure

user@ansible:$ tree
---- ansible.cfg
---- inventories
-------- inventory
---- playbooks
-------- deploy.yml
-------- role_playbook.yml
---- roles
-------- install_nginx
---------- defaults
-------------- main.yml
---------- tasks
-------------- main.yml
---------- handlers
-------------- main.yml

linkedin.com/in/omerberatsezer
playbook
(more modules)

linkedin.com/in/omerberatsezer
user@ansible:$ ####################################

Follow for Tips on AWS, K8s, Docker, Linux


Terraform, Ansible, DevOps
Why? Cause; More will unfold over time
||
V
https://linkedin.com/in/omerberatsezer

https://github.com/omerbsezer

Feel free to like, share, or repost


to help more people see it

user@ansible:$ ####################################

linkedin.com/in/omerberatsezer

You might also like