Ansible Notes For Devops
Ansible Notes For Devops
Ansible (an IT automation, Configuration Management and Provision tool) is a simple, flexible, and
extremely powerful tool that gives you the ability to automate common infrastructure tasks, run ad
hoc commands, and deploy multitier applications spanning multiple machines.
Main features
Controller: This is the only host that needs to have Ansible installed, and works as a controller. This
is used to launch the ansible-playbook commands from the controller.
Ansible is agentless and manages nodes using SSH transport, no additional setup is needed on the
nodes except for ensuring that the SSH service is running.
KEY COMPONENTS
A playbook consists of one or more plays, which map groups of hosts to well-defined tasks.
• The fi st li e of a pla ook should egi ith "--- " (three hyphens) which indicates the beginning
of the YAML document.
• Lists i YAML a e ep ese ted ith a h phe follo ed a white space. A playbook contains a list
of plays; they are represented with "- ". Each play is an associative array, a dictionary, or a map in
terms of key-value pairs.
Before we even start writing our playbook with Ansible, we need to define an inventory of all hosts
that need to be configured, and make it available for Ansible to use. Later, we will start running plays
against a selection of hosts from this inventory. For text-based local inventory, the default location is
/etc/ansible/hosts.
Tasks:
Plays map hosts to tasks. Tasks are a sequence of actions performed against a group of hosts that
match the pattern specified in a play. Each play typically contains multiple tasks that are run serially
on each machine that matches the pattern.
Modules:
Modules are the encapsulated procedures that are responsible for managing specific system
o po e ts o spe ifi platfo s. Co side the follo i g e a ple: • The apt odule fo De ia a d
the u odule fo RedHat helps a age s ste pa kages • The use odule is responsible for
addi g, e o i g, o odif i g use s o the s ste • The se i e odule ill sta t/stop s ste
services Modules abstract the actual implementation from users. They expose a declarative syntax
that accepts a list of the parameters and states of the system components being managed. All this
can be declared using the human-readable YAML syntax, using key-value pairs.
Bits of code copied to the target system. Executed to satisfy the task declaration.
#ansible-doc -l
#ansible-doc <module_name>
• E e ti e ide pota e is u ultiple ti es, the apt odule ill o pa e hat has been
declared in the playbook versus the current state of that package on the system. The first time it
runs, Ansible will determine that Nginx is not installed, and will go ahead with the installation.
• Fo e e o se ue t u , it ill skip the installation part, unless there is a new version of the
package available in the upstream repositories. This allows executing the same task multiple times
without resulting in the error state. Most of the Ansible modules are idempotent, except for the
command and shell modules. Users will have to make these modules idempotent.
Understanding roles:
Roles provide a way to create modular code, which then can then be shared and reused.
Lab setup:
server1:
# iptables -F
# chkconfig iptables
# vi /etc/sysconfig/selinux
SELINUX=disabled
# vi /etc/hosts
IPaddr1 server1
IPaddr2 server2
IPaddr3 server3
#userdd test
# passwd test
# visudo
# reboot
server1:
# cd /etc/ansible
#vi hosts
[webserver]
server2
server3
# su – test
$ ssh-keygen
$ ssh-copy-id server2
$ ssh-copy-id server3
Example1:
$ vi webserver.yml
- hosts: webserver
remote_user: test
become: yes
tasks:
$ ansible-playbook webserver.yml
Example2:
$ vi variables.yml
remote_user: test
become: yes
vars:
pkg1: httpd
vars_files:
- abc.yml
vars_prompt:
- name: pkg3
private: no
tasks:
$ more abc.yml
---
pkg2: telnet
Example3:
Dealing with errors.
$ vi errors.yml
- hosts: webserver
remote_user: test
become: yes
tasks:
ignore_errors: yes
$ ansible-playbook errors.yml
Example4:
Handler usage:
$ vi handler.yml
- hosts: webserver
remote_user: test
become: yes
tasks:
handlers:
$ ansible-playbook handler.yml
Example5:
$ vi commands.yml
- hosts: webserver
remote_user: test
become: yes
tasks:
script: ./xyz.sh
$ more xyz.sh
#! /bin/bash
echo "hello" > /home/user1/file1
$ ansible-playbook commands.sh
Example6:
Gathering facts
$ vi fact.yml
- hosts: webserver
remote_user: test
become: yes
gather_facts: yes
tasks:
$ ansible-playbook fact.yml
Example7:
$ vi condition.yml
- hosts: webserver
remote_user: test
become: yes
tasks:
$ ansible-playbook condition.yml
Example8:
$ vi template.yml
- hosts: webserver
remote_user: test
become: yes
tasks:
$ more index.html.j2
hello
{{ansible_hostname}}
World
$ ansible-playbook template.yml
Example 9:
$ vi register.yml
- hosts: webserver
remote_user: test
become: yes
tasks:
register: result
- debug: var=result
$ ansible-playbook template.yml
Example10:
$ vi tag.yml
- hosts: webserver
remote_user: test
become: yes
tasks:
tags:
- install
tags:
- copy
tags:
- service
Example11:
Loops:
$ vi loops.yml
- hosts: webserver
remote_user: test
become: yes
tasks:
with_items:
- lynx
- telnet
- curl
$ ansible-playbook loops.yml
Example12:
$ vi unarchive.yml
- hosts: server2
remote_user: test
become: yes
tasks:
$ ansible-playbook unarchive.yml
Example13:
$ vi url.yml
- hosts: server2
remote_user: test
become: yes
tasks:
$ ansible-playbook url.yml
Example14:
Roles
$ cd /home/test
$ mkdir roles
$ mkdir {defaults,files,handlers,meta,tasks,templates,vars}
$ cd tasks
$ vi main.yml
---
$ cd ../files
$ vi index.html
$ cd ../handlers
$ vi main.yml
---
$ vi role1.yml
- hosts: webserver
remote_user: test
become_method: sudo
become: yes
roles:
- webservers
$ ansible-playbook role1.yml
Example15:
Tomcat installation:
$ vi tomcat.yml
--- # tomcat setup
- hosts: server2
remote_user: test
become: yes
tasks:
get_url: url=http://mirror.fibergrid.in/apache/tomcat/tomcat-7/v7.0.78/bin/apache-tomcat-
7.0.78.tar.gz dest=/tmp
$ ansible-playbook tomcat.yml
Example16:
Example 17:
3. Start and Enable HTTPD service on web hosts only if a new httpd package is installed.
Solution:
$ vi sol1.yml
--
- hosts: all
remote_user: test
become: yes
vars:
selinux: permissive
tasks:
-hosts: web
remote_user: test
become: yes
tasks:
notify: RestartSSH
handlers:
- name: RestartSSH
$ ansible-playbook sol1.yml
Discuss about Ansible Tower and Ansible Galaxy along with other cases in class.