Ansible Fundamentals To Advance
Ansible Fundamentals To Advance
fun
da
mentals
github.com/omerbsezer
Ansible is an open-source automation tool
documentation: https://docs.ansible.com
linkedin.com/in/omerberatsezer
two types of nodes (servers)
1. control node (master)
2. worker nodes
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
linkedin.com/in/omerberatsezer
configuration
(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
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)
linkedin.com/in/omerberatsezer
playbook
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)
linkedin.com/in/omerberatsezer
roles
(tasks)
---
- 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:$ ####################################
https://github.com/omerbsezer
user@ansible:$ ####################################
linkedin.com/in/omerberatsezer