Ansible Notes
Ansible Notes
Challenges:
1. Human effort
2. Can lead to human errors
3. Time consuming task
4. Efforts can be re-used - No reuseablity
5. At Runtime , Application may be break.
===================================================================================
======================================
How ansible works?
===================================================================================
======================================
Ansible uses push mechansim which is based on passwordless connection.
Ansible is agentless mechansim ie it doesnt require any agent to be installed in
target nodes.
Ansible works on 2 tier architecture ie Ansible controller and Ansible target
nodes.
Ansible controller -> Ansible target
Ansible playbooks - yamls
===================================================================================
===============
Alternate Configurtion management tools:
===================================================================================
===============
Puppet - pull mechanism - agent -> puppet agent is required to installed puppet
machines.
puppet workstation -> puppet master -> puppet nodes(agent)
puppet manifest -> yamls
3 tier architecture
===================================================================================
===============
chef - pull mechanism - agent -> chef client is required to installed chef nodes
Chef cookbooks -> yamls
3 tier architecture
===================================================================================
==========
Pre-requisite between controller and target node for passwordless connection
===================================================================================
==========
1. Ansible user/password on both controller and target nodes
sudo su
useradd ansible
passwd ansible
vi /etc/ssh/sshd_config
PermitRootLogin yes
PAsswordAuthentication yes
4. Exit from root user and switch to ansible user on both controller and target
nodes
exit
su - ansible
a. ssh-keygen -t rsa
b. cd .ssh
cat id_rsa.pub > ~/.ssh/authorized_keys
============================================================================
Ansible Installation only on Master/controller :
============================================================================
1. Install wget:
sudo yum install wget -y
3. Configuration setup:
/etc/ansible/ansible.cfg
[webservers]
Private-IP-of-target-Node
====================================================================
Verify if target machine is reachable:
====================================================================
ansible webservers -m ping
===================================================================================
================
5. user : User module is used for creating user in linux.
=======================================
Create file and directory:
=======================================
=======================================
Delete file and directory:
=======================================
===================================================================================
=============================
8. service module: Service module is used starting the service of the package or
application.
===================================================================================
========================
Inventory :
===================================================================================
========================
Inventory is a file which contains all the target nodes
IP-Address/hostname/hostgroups.
With help inventory file , Ansible controller connects to target machines.
=========================
Types of Inventories :
=========================
1. INI format : File is based format which doesnt require any indentations ie
proper spaces and INI format inventory does not have any extension.
================
vi inventory
===============
#ungrouped servers
10.0.0.24
db-server-01
backend-application-server
#grouped servers
[webservers]
10.0.0.54
webservers-01
webservers-02
webservers-03
webservers-[10-20]
[appservers]
appservers-[1-10]
[prod-dbservers]
10.0.43.65
172.32.23.24
===========================================
2. YAML format: Yaml format inventory is indentation based inventory which requires
proper spaces and also it has extension as .yml/yaml.
===============================
vi inventory.yml
===============================
all:
hosts: // ungrouped servers
10.0.0.24
db-server-01
backend-application-server
children:
hosts:
webservers: // grouped servers
webserver-01
webservers-[101-110]
appservers: // grouped servers
10.0.0.23
app-server-10
app-server-[1-4]
===================================================================================
=======================================
Ansible Playbook:
===================================================================================
==================================
Playbook is instruction file where all script/code is written in yaml format for
running linux commands, executing scripts, displaying outputs, validating the
outputs etc
====================================
Execution of playbook:
====================================
syntax: ansible-playbook -i /path-to-inventory playbook.yml -vv(verbose)
eg: a=10
echo $a
10
MAVEN_HOME=/opt/maven
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk
==================================
vi variables-playbook.yml
==================================
---
- hosts: webservers
vars:
port: 9000
tasks:
- name: Display Port value
debug:
msg={{ port }}
---
- hosts: webservers
vars:
port:
- 9000
- 9001
- 9002
- 9003
tasks:
- name: Display Port value
debug:
msg={{ port[0],port[1],port[2] }}
---
- hosts: webservers
vars:
port: 8080
server: db-server
tasks:
- name: Display Port value
debug:
msg={{ port, server }}
===================================================================================
============================
Tags: Tag is used for uniquely identifying an task or play.
With help of tags, we can run specific play or task without modifying the
playbook.
=================================================================
Tags at task level:
=================================================================
vi without-tags-at-task-level.yml
============================================
---
- hosts: webservers
become: true
tasks:
- name: Install httpd server
yum: name=httpd state=present
- name: Install wget package
yum: name=wget state=present
- name: Install zip server
yum: name=httpd state=present
- name: Install nginx package
yum: name=nginx state=present
============================================
vi with-tags-at-task-level.yml
============================================
---
- hosts: webservers
become: true
tasks:
- name: Install httpd server
yum: name=httpd state=present
tags:
- install_httpd
- name: Install wget package
yum: name=wget state=present
tags:
- install_wget
- name: Absent zip server
yum: name=zip state=absent
tags:
- install_zip
- name: Install nginx package
yum: name=nginx state=present
tags:
- install_nginx
- uninstall_httpd
- name: Uninstall wget package
yum: name=wget state=absent
tags:
- uninstall_httpd
- name: Uninstall zip server
yum: name=zip state=absent
tags:
- uninstall_httpd
- name: Uninstall nginx package
yum: name=nginx state=absent
tags:
- uninstall_httpd
eg: vi register.yml
---
- hosts: webservers
become: true
tasks:
- name: Install httpd server
yum: name=httpd state=present
register: httpd_output
- debug:
var: httpd_output
===================================================================================
==========================================
Handlers:
===================================================================================
==========================================Handlers are used for to avoid
unnecessary restarts for any running service.
Whenever there is any configuration changes in files, automatically handlers will
triggered the restart for services. In case, no configuration is found, it will not
restart the service.
====================================
How handlers works?
====================================
Handlers works with notify keyword/module. Notify keyword/module will inform/call
handlers whenever there is an configuration change and handlers will restart the
service. In case, no change found notify will not call the handlers.
Notify is mentioned under the configuration task.
eg:
===================================
vi without-handlers.yml
===================================
---
- hosts: webservers
become: true
tasks:
- name: Install httpd server
yum: name=httpd state=present
- name: Start the httpd service
service: name=httpd state=started
- name: Configuration file changes
copy: src=/https/www.scribd.com/home/ansible/index.html dest=/var/www/html/index.html
- name: Restart Httpd Service
service: name=httpd state=restarted
=====================================
vi with-handlers.yml
===================================
---
- hosts: webservers
become: true
tasks:
- name: Install httpd server
yum: name=httpd state=present
- name: Start the httpd service
service: name=httpd state=started
- name: Configuration file changes
copy: src=/https/www.scribd.com/home/ansible/index.html dest=/var/www/html/index.html
notify:
- Restart Httpd Service
handlers:
- name: Restart Httpd Service
service: name=httpd state=restarted
===================================================================================
====================================
Loops : Loops are used for performing repetative task.
===================================================================================
===================================
1. Single value loop :
syntax:
with_items: // defining the values
- 'git'
- 'zip'
- 'wget'
- 'httpd'
========================================
vi with-single-loop.yml
========================================
---
- hosts: webservers
become: true
tasks:
- name: Install Packages
yum: name={{ item }} state=present
with_items:
- 'git'
- 'zip'
- 'wget'
- 'httpd'
=================================================
2. Multi value loop:
syntax:
with_items: // defining the values
- { name: git, state: present }
- { name: zip, state: absent }
- { name: wget, state: absent }
- { name: httpd, state: latest }
========================================
vi without-multi-loop.yml
========================================
---
- hosts: webservers
become: true
tasks:
- name: Install git server
yum: name=git state=present
- name: Uninstall zip server
yum: name=zip state=absent
- name: Uninstall wget server
yum: name=wget state=absent
- name: Upgrade httpd server
yum: name=httpd state=latest
========================================
vi with-multi-loop.yml
========================================
---
- hosts: webservers
become: true
tasks:
- name: Package Management
yum: name={{ item.name }} state={{ item.state }}
with_items:
- { name: git, state: present }
- { name: zip, state: absent }
- { name: wget, state: absent }
- { name: httpd, state: latest }
===================================================================================
=========================
Templates:
===================================================================================
========================
Template is a file which contains actual values of configuration file which helps
to replace the default values of configuration files while
installation/Upgradation.
Templates called as jinja templates.
Templates have an extension as .j2
Module - ansible
Playbook - ansible-playbook
Roles - ansible-galaxy
8 directories, 8 files
[ansible@ip-172-31-2-51 httpd_role]$
===================================================================================
=======================================
eg: Divide playbook into small playbooks via roles:
===================================================================================
=======================================
---
- hosts: webservers
become: true
tasks:
- name: Install httpd server
yum: name=httpd state=present
- name: Start the httpd service
service: name=httpd state=started
- name: Configuration file changes
copy: src=/https/www.scribd.com/home/ansible/index.html dest=/var/www/html/index.html
notify:
- Restart Httpd Service
handlers:
- name: Restart Httpd Service
service: name=httpd state=restarted
===================================================================================
===================================
1. Create an custom role :
[ansible@ip-172-31-2-51 ~]$ ansible-galaxy init httpd_role
- Role httpd_role was created successfully
[ansible@ip-172-31-2-51 ~]$
===================================================================================
===================================
2. cd httpd_role/tasks
===================================================================================
===================================
a. install.yml
b. service.yml
c. configure.yml
d. main.yml
===================================================================================
====================================
a. vi install.yml
---
- name: Install httpd server
yum: name=httpd state=present
b. vi service.yml
---
- name: Start the httpd service
service: name=httpd state=started
c. vi configure.yml
---
- name: Configuration file changes
copy: src=index.html dest=/var/www/html/index.html
notify:
- Restart Httpd Service
d. vi main.yml
---
- import_tasks: install.yml
- import_tasks: service.yml
- import_tasks: configure.yml
===================================================================================
=====================================
3. cd httpd_role/files
vi index.html
<H1> Static website via roles </H1>
===================================================================================
====================================
4. cd httpd_role/handlers
vi main.yml
---
- name: Restart Httpd Service
service: name=httpd state=restarted
===================================================================================
======================================
5. cd httpd_role/meta
vi main.yml
Gallaxy_info:
name:
description:
===================================================================================
======================================
Main playbook should be outside the roles directory.
vi setup_httpd_role.yml
---
- hosts: webservers
become: true
roles:
- httpd_role
===================================================================================
=====================================
Execution: ansible-playbook -i inventory setup_httpd_role.yml -vv
===================================================================================
=====================================
Tips and Tricks of Ansible:
===================================================================================
=====================================
1. limit : Limit is a filter where we can run playbooks on specific hosts/IP's
without editing the inventory file.
===================================================================================
======================================
2. skip-tags: skip-tags is used for skipping the particular task from playbooks.
===================================================================================
======================================
3. start-at-task: start-at-task is used for start running playbook from particular
task.
eg: 20 tasks in a playbook, start playbook execution from 12th task till the end.
===================================================================================
======================================
4. step: step is used for step by step execution and debugging the playbooks.
step will pause after every task and take confirmation and then proceed.
===================================================================================
=========================================
5. list-hosts: list-hosts will be list all the hosts/IP inside in the inventory on
which playbooks the hosts/IP are going to be executed.
playbook: sample.yml
===================================================================================
==========================================
6.syntax-check :syntax check verifies whether all the script written inside the
playbook is according to ansible standardsie its verifying the ansible syntax.
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got
from each:
JSON: Expecting value: line 1 column 1 (char 0)
vars
name: john
^ here
[ansible@ip-172-31-2-51 ~]$ vi sample.yml
[ansible@ip-172-31-2-51 ~]$ ansible-playbook -i inventory sample.yml --syntax
playbook: sample.yml
[ansible@ip-172-31-2-51 ~]$
===================================================================================
=====================================
7.ping: Ping is used checking the connnectivity between the controller machine and
target nodes.
===================================================================================
=================================