You are on page 1of 6

ANSIBLE

Ansible is a Opensource tool for deployment, configuration management of application on


multiple server from single machine (ansible server).

Ansible does not have any service/daemon running, but relies on SSH. So it is called as
agentless service, only make sure that ssh service is running properly on both server and
clients and ansible server user public key is copied to all the agent servers.

Ansible key terms:


++++++++++++++
1) ansible.cfg file: which stores and manages the global parameters of the ansible setup. like
remote port, inventory location, log etc.,

2) hosts: which stores all client machine details in order to communicate from the ansible
server.

3) inventory: an INI file that points the location of hosts file.

4) Task: block which defines the task that need to be executed like: install apache2

5) module: which is developed to do a specific task on the servers which is written in python
language. like apt, yum, service, ini_file modules.

6) role: Roles are good for organizing multiple, related Tasks and encapsulating data needed
to accomplish those Tasks. It’s a pre-defined way for organizing playbooks and other files in
order to facilitate sharing and reusing portions of a provisioning.

===
If roles/x/tasks/main.yml exists, tasks listed therein will be added to the play
If roles/x/handlers/main.yml exists, handlers listed therein will be added to the play
If roles/x/vars/main.yml exists, variables listed therein will be added to the play
If roles/x/defaults/main.yml exists, variables listed therein will be added to the play
If roles/x/meta/main.yml exists, any role dependencies listed therein will be added to the list
of roles
===

7) Facts: Before running any Tasks, Ansible will gather information about the system it's
provisioning, like no of network interfaces, ip address or operating system details like OS
family, version etc.,

8) Handlers: used to trigger service status changes, like restarting or stopping a service.

9) variables: which hold the value to pass in the playbook. Example to store the variable in
vars_files, group_vars etc.,
10) loop: it is typically used to repeat a task using different input values

===
- name: install packages
apt: name={{ item }} state=present
with_items:
- apache2
- mysql-server
- telnet
===

11) conditions: It is used to confirm whether to execute the specific task on the machines or
not.

12) Template: It is used to setup the configuration files on the application etc., It uses jinja2
template engine.

13) Meta: The main.yml file within the meta directory contains Role metadata, including
dependencies.
---
dependencies: []
---
dependencies:
- { role: ssl }

dependencies:
- { role: common, some_parameter: 3 }
- { role: apache, apache_port: 80 }
- { role: postgres, dbname: blarg, other_parameter: 12 }

Steps to install ansible latest version on Ubuntu server:


+++++++++++++++++++++++++++++++++++++++++++++

$ sudo apt-get install software-properties-common


$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible

Roles have a directory structure as mentioned below:

ansible all -m ping -s -k -u vagrant

all --> execute on all servers defined in inventory file


-m ping --> Use the "ping" module, which simply runs the ping command and returns the
results
-s --> Use "sudo" to run the commands
-k --> Ask for a password rather than use key-based authentication
-u testuser --> Log into servers using user testuser

Ansible configuration file structure:


+++++++++++++++++++++++++++++
* ANSIBLE_CONFIG (an environment variable)
* ansible.cfg (in the current directory)
* .ansible.cfg (in the home directory)
* /etc/ansible/ansible.cfg

Directory structure of roles:


+++++++++++++++++++++++

drwxr-xr-x 2 root root 4096 May 9 08:13 defaults


drwxr-xr-x 2 root root 4096 May 9 08:13 handlers
drwxr-xr-x 2 root root 4096 May 9 08:13 meta
-rw-r--r-- 1 root root 1328 May 9 08:13 README.md
drwxr-xr-x 2 root root 4096 May 9 08:13 tasks
drwxr-xr-x 2 root root 4096 May 9 08:13 tests
drwxr-xr-x 2 root root 4096 May 9 08:13 vars

Host and group specific data of Variables:


++++++++++++++++++++++++++++++++++

By default we specify the variables in main inventory file. So we can store the group and
host specific variable values in individual files according to the inventory file.

Example:
1) /etc/ansible/group_vars/<group_name>
2) /etc/ansible/host_vars/<host_name>

Also, we can create files under gourp_vars directory to define variables of group hosts.

1) /etc/ansible/group_vars/<group_name>/<files>
2) /etc/ansible/host_vars/<host_name>/<files>

Note: In Ansible 1.2 or later the group_vars/ and host_vars/ directories can exist in the
playbook directory OR the inventory directory. If both paths exist, variables in the playbook
directory will override variables set in the inventory directory

Example for loop and variable declaration:


++++++++++++++++++++++++++++++++++++++++++

---
- hosts: all
sudo: true
vars:
packages: [ 'apache2', 'wget', 'telnet' ]
tasks:
- name: Install Package
apt: name={{ item }} state=latest
with_items: packages

-----------------
Starting in 1.0, variables can also be passed to include files using an alternative syntax,
which also supports structured variables:

tasks:

- include: wordpress.yml
vars:
wp_user: timmy
ssh_keys:
- keys/one.txt
- keys/two.txt

++++++++++++++++++++++++++

- name: Check if PHP is installed


register: php_installed
command: php -v
ignore_errors: true

- name: This task is only executed if PHP is installed


debug: var=php_install
when: php_installed|success

- name: This task is only executed if PHP is NOT installed


debug: msg='PHP is NOT installed'
when: php_installed|failed

+++++++++++++++++++++++++++

- name: Clone git repository


git: >
dest=/var/www/laravel
repo=https://github.com/do-community/do-ansible-adv-php.git
update=yes
version=example

name: laravel
repository: https://github.com/do-community/do-ansible-adv-php.git
branch: example
domain: laravel.example.com
+++++++++++++++++++++++++++

- include: intro_example.yml

- name: another play


hosts: all
tasks:
- debug: msg=hello

# this is a 'task' include


- include: apache.yml

+++++++++++++++++++++++++++

---

- hosts: webservers
roles:
- { role: some_role, when: "ansible_os_family == 'RedHat'" }

- hosts: webservers
roles:
- common
- { role: foo_app_instance, dir: '/opt/a', app_port: 5000 }
- { role: foo_app_instance, dir: '/opt/b', app_port: 5001 }

- hosts: webservers
roles:
- { role: foo, tags: ["bar", "baz"] }

++++++++++++++++++++++++++++

---

- hosts: webservers

pre_tasks:
- shell: echo 'hello'

roles:
- { role: some_role }

tasks:
- shell: echo 'still busy'

post_tasks:
- shell: echo 'goodbye'
++++++++++++++++++++++++++++

Senarios:
--------------

1) Install webserver on both Debian 14.04 and RedHat6 machines using single playbook.
2) Install tomcat on Redhat 7 machine only using playbook.
3) Install LAMP on Ubuntu 16.04 and CentOS 7.

You might also like