Ansible Notes
Ansible Notes
Ansible Notes
Ansible Tutorial
++++++++++++++++++++
1) What is Ansible
2) Configuration Management
3) Push Based vs Pull Based
4) How to install Ansible
5) Host Inventory
6) Ansible Modules
7) YAML
8) Playbooks
9) Hands On
10) Conclusion
Configuration Management
++++++++++++++++++++++++++++++
It is a method through which we automate admin tasks.
Configuration management tool turns your code into infrastructure.
So your code would be testable, repeatable and versionable.
Software
Network
People
Process
+++++++++++
Ansible
+++++++++++
-> Ansible is one among the DevOps configuration management tools which is famous
for its simplicity.
-> It is an open source software developed by Michael DeHaan and its ownership is
on RedHat
-> This tool is very simple to use yet powerful enough to automate complex multi-
tier IT application environments.
-> The best part is that you don’t even need to know the commands used to
accomplish a particular task.
-> You just need to specify what state you want the system to be in and Ansible
will take care of it.
-> The main components of Ansible are playbooks, configuration management and
deployment.
-> Ansible uses playbooks to automate deploy, manage, build, test and configure
anything
++++++++++++++++
Ansible Features
+++++++++++++++++
-> Built on top of Python and hence provides a lot of Python's functionality
-> Follows push based architecture for sending configuration related notifications
+++++++++++++++++++++++++++
Push Based Vs Pull Based
+++++++++++++++++++++++++++
-> Agents on the server periodically checks for the configuration information from
central server (Master)
++++++++++++++++++++++
What Ansible can do ?
++++++++++++++++++++++
1) Configuration Management
2) App Deployment
3) Continous Delivery
++++++++++++++++++++++
How Ansible works ?
++++++++++++++++++++++
Ansible works by connecting to your nodes and pushing out a small program called
Ansible modules to them.
Then Ansible executed these modules and removed them after finished.The library of
modules can reside on any machine, and there are no daemons, servers, or databases
required.
The Management Node is the controlling node that controls the entire execution of
the playbook.
The inventory file provides the list of hosts where the Ansible modules need to be
run.
The Management Node makes an SSH connection and executes the small modules on the
host’s machine and install the software.
1) Controlling Nodes
2) Managed Nodes
3) Ansible Playbook
+++++++++++++++++++
Controlling Nodes
+++++++++++++++++++
are usually Linux Bastion Servers that are used to access the switches/routers and
other Network Devices.
These Network Devices are referred to as the Managed Nodes.
+++++++++++++++++++
Managed Nodes
++++++++++++++
Managed Nodes are stored in the hosts file for Ansible automation.
+++++++++++++++++++
Ansible Playbook
+++++++++++++++++++
Ansible Playbooks are expressed in YAML format and serve as the repository for the
various tasks that will be executed on the Managed Nodes (hosts).
Playbooks are a collection of tasks that will be run on one or more hosts.
+++++++++++++++++++
Inventory file
+++++++++++++++++++
Ansible's inventory hosts file is used to list and group your servers.
+++++++++++++++++++++++++++++++++++++++
Few Important Points About Inventory File
+++++++++++++++++++++++++++++++++++++++
Ansible's inventory hosts file is used to list and group your servers. Its default
locaton is /etc/ansible/hosts
#Ungrouped hosts are specifiying before any group headers like below
192.168.122.1
192.168.122.2
192.168.122.3
[webservers]
192.168.122.1
#192.168.122.2
192.168.122.3
[dbserver]
192.168.122.1
192.168.122.2
ashokit-db1.com
ashokit-db2.com
+++++++++++++++
Ansible Setup
+++++++++++++++
=> Create 3 Red Hat Systems in AWS (Free Tier Eligible - t2.micro)
1 - Control Node
2 - Managed Nodes
pwd
confirm pwd
$ sudo visudo
ansible ALL=(ALL) NOPASSWD: ALL
$ sudo vi /etc/ssh/sshd_config
++++++++++++++++++++++++++++++
Install Ansible in Control Node
++++++++++++++++++++++++++++++
-> Switch to Ansible user
$ sudo su ansible
$ python --version
$ ansible --version
-> create ansible.cfg file under /etc/ansible And paste complete content from below
git link.
Open : https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
Copy the content and paste it in ansible.cfg file with below command
$ sudo vi /etc/ansible/ansible.cfg
-> Create hosts file under /etc/ansible. Sample content can found in below git link
Open : https://raw.githubusercontent.com/ansible/ansible/devel/examples/hosts
Copy the content and paste it in hosts file with below command
$ sudo vi /etc/ansible/hosts
1) Update Host Inventory in Ansible Server to add host servers details to test
connection
$ sudo vi /etc/ansible/hosts
2) Use ping module to test Ansible and after successful run you can see the below
output.
"to use the 'ssh' connection type with passwords, you must install the sshpass
program
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++
Generate SSH Key In Control Node and Copy SSH key into Host Nodes (Managed Nodes)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++
$ sudo su ansible
$ ssh-copy-id ansible@<ManagedNode-Private-IP>
Ex : $ ssh-copy-id [email protected]
Note: Repeat below command by updating HOST IP for all the managed Servers.
# vi /etc/ansible/hosts
4) Use ping module to test Ansible and after successful run you can see the below
output.
+++++++++++++++++++++++
Ansible AD-HOC Commands
+++++++++++++++++++++++
$ sudo su ansible
Example:
There are two default groups, all and ungrouped. all contains every host. ungrouped
contains all hosts that don’t have another group
+++++++++++
Ping Module
+++++++++++
# It will ping all the servers which you have mentioned in inventory file
(/etc/ansible/hosts)
$ ansible all -m ping
# Here it will check the disk space use for all the nodes which are from dbservers
group
$ ansible dbservers -a "df -h"
# Here it will check the disk space use for all the nodes which are from webservers
group
$ ansible webservers -a "free -m"
+++++++++++++++
Yum Module
+++++++++++++++
# It will install vim package in all node machine which you have menyioned in host
inventory file.
$ ansible all -b -m yum -a "name=vim"
present : install
latest : update to latest
absent : un-install
# to install any software in ubuntu server then we should use apt package manager
Ans) Ansible introduced "package manager" to work with underlying package manager
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++
YAML (Yet Another Markup Language )
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++
-> We can make use of this language to store data and configuration in a human-
readable format.
Key-Value Pair
+++++++++++++++++
Fruit: Apple
Vegetable: Carrot
Liquid: Water
Meet: Chicken
Array/List
++++++++++++
Fruits:
- Orange
- Apple
- Banana
- Guava
Vegetables:
- Carrot
- Cauliflower
- Tomoto
name: Ashok
age: 29
phno: 123456
email: [email protected]
hobbies:
- cricket
- dance
- singing
person:
id: 101
name: Raju
email: [email protected]
address:
city: Hyd
state: TG
country: India
job:
companyName: IBM
role: Tech Lead
pkg: 25 LPA
hobbies:
- cricket
- chess
- singing
- dance
---
person:
id: 101
name: Raju
email: [email protected]
address:
city: Hyd
state: TG
country: India
job:
companyName: IBM
role: Tech Lead
pkg: 25 LPA
hobbies:
- cricket
- chess
- singing
- dance
---
movie:
name: Bahubali
hero: Prabhas
heroine: Anushka
villian: Rana
director: SS Rajamouli
budget: 100cr
...
+++++++++++++++++++++
Playbooks
+++++++++++++++++++++
-> Playbook is a single YAML file, containing one or more ‘plays’ in a list.
-> Plays are ordered sets of tasks to execute against host servers from your
inventory file.
Examples are
a) Execute a command
b) Run a shell script
c) Install a package
d) Shutdown / Restart the hosts
Note : Playbooks YML / YAML starts with the three hyphens ( --- ) and ends with
three dots ( … )
2) Host section – Defines the target machines on which the playbook should run.
This is based on the Ansible host inventory file.
3) Variable section – This is optional and can declare all the variables needed
in the playbook. We will look at some examples as well.
4) Tasks section – This section lists out all the tasks that should be executed
on the target machine. It specifies the use of Modules. Every task has a name which
is a small description of what the task will do and will be listed while the
playbook is run.
+++++++++++++++++++++++++++++++++++++++++++++++
Playbook To Ping All Host Nodes
+++++++++++++++++++++++++++++++++++++++++++++++
---
- hosts: all
gather_facts: no
remote_user: anisble
tasks:
- name : Test connection
ping:
remote_user: ansible
#name: which is the task name that will appear in your terminal when you run the
playbook.
#remote_user: This parameter was formerly called just user. It was renamed in
Ansible 1.4 to make it more distinguishable from the user module (used to create
users on remote systems).
$ ansible-playbook playbook.yml -v
$ ansible-playbook playbook.yml -vv
$ ansible-playbook playbook.yml -vvv
# It will display the which hosts would be effected by a playbook before run
$ ansible-playbook playbook.yml --list-hosts
+++++++++++++++++++++++++++++++++++++++++++++++
Install HTTPD + copy index.html + Start Service
++++++++++++++++++++++++++++++++++++++++++++++++
---
- hosts: all
become: true
tasks:
- name: Install Httpd
yum:
name: httpd
state: present
- name: Copy index.html
copy:
src: index.html
dest: /var/www/html/index.html
- name: Start Httpd Server
service:
name: httpd
state: started
...
++++++++++++++++++++++++++++++++++++
Variables
++++++++++++++++++++++++++++++++++++
---
- hosts: all
become: true
tasks:
- name: Install Httpd
yum:
name: "{{package_name}}"
state: present
- name: Copy index.html
copy:
src: index.html
dest: /var/www/html/index.html
- name: Start Http Server
service:
name: "{{package_name}}"
state: started
...
---
- hosts: all
become: true
vars:
package_name: httpd
tasks:
- name: Install Httpd
yum:
name: "{{package_name}}"
state: present
- name: Copy index.html
template:
src: index.html
dest: /var/www/html/index.html
- name: Start Http Server
service:
name: "{{package_name}}"
state: started
...
---
- hosts: all
become: true
tasks:
- name: install software
yum:
name: "{{package_name}}"
state: present
...
=================
Group Variables
=================
group_vars/all.yml
group_vars/<groupName>.yml
Ex:
$ mkdir /etc/ansible/group_vars
$ sudo vi /etc/ansible/group_vars/webservers.yml
package: git
$ sudo vi /etc/ansible/group_vars/dbservers.yml
package: mysql
============
Host vars
=============
-> server specific variables
-> For every host if we wan seperate variables then we should go for host vars
-> vi /etc/ansible/host_vars/172.138.1.1.yml
=====================================
Variable Value we can declare with in playbook
Variable value we can supply in runtime
===============
Ansible Vault
===============
-> When we configure uname and pwd in variables files everybody can see them which
is not a good practise
-> When we are dealing with sensitive data then we should secure that data
-> Using Ansible vault we can encrypt and we can decrypt data
=======================
Ansible Vault Commands
=======================
-> You can store vault password in a file and you can give that file as input to
execute playbook
$ vi valutpass
$ ansible-playbook filename.yml --vault-password-file=~/vaultpass
# We can see encrypted file in human readable format
$ ansible-vault view /etc/ansible/group_vars/all.yml
=================
Handlers and Tags
================
-> Using Handlers we can execute tasks based on other tasks status
-> Using tag name we can execute particular task and we can skip particular task
also
---
- hosts: all
become: true
gather_facts: no
vars:
package_name: httpd
tasks:
- name: install httpd
yum:
name: "{{package_name}}"
state: present
tags:
- install
- name: Copy index.html
copy:
src: index.html
dest: /var/www/html/
tags:
- copy
notify:
Start Httpd Server
handlers:
- name: Start Httpd Server
service:
name: "{{package_name}}"
state: started
...
++++++++++++++++++++++++++++++
Installing Multiple Softwares
++++++++++++++++++++++++++++++
- hosts: all
tasks:
- name: install softwares
yum:
name: "{{item}}"
state: present
with_items:
- wget
- zip
- unzip
++++++++++++++++++++++++++++++
Another approach
++++++++++++++++++++++++++++++
- hosts: all
tasks:
- name: install softwares
yum:
name: ['wget', 'zip', 'unzip']
state: present