Monday 22 April 2019

Ansible Plays and Play books

Ansible Plays and Play books
Plays maps hosts to tasks
A play can have multiple tasks
A play book can have multiple plays.
at HOME dir:-
[ansible@ansibleserver ~]$ cat .vimrc
autocmd FileType yaml setlocal ai ts=2 sw=2 et

[ansible@ansibleserver playbooks]$ cat firstplay.yaml
---
- name: first yaml file
  hosts: db
  vars_prompt:
  - name: "sitename"
    prompt: "what is new state name?"
  tasks:
  - debug: msg="new state name is {{sitename}}"
...
[ansible@ansibleserver playbooks]$ ansible-playbook firstplay.yaml
what is new state name?:

PLAY [first yaml file] ***************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
ok: [10.25.12.10]

TASK [debug] *************************************************************************************************************************
ok: [10.25.12.10] => {
    "msg": "new state name is TS"
}

PLAY RECAP ***************************************************************************************************************************
10.25.12.10                : ok=2    changed=0    unreachable=0    failed=0

ANSIBLE modules

Ansible Modules:-
There are three types of modules existed
1)Core Modules : these are built in modules
2)Extra Modules: these are third party modules
3) Deprecated Modules: these are no longer available.

to display:
$ansible -doc     -l
$ansible-doc  ping
$ansible host1 -m setup|more
$ansible host1   -m setup -a "filter=ansible_mounts"

How to use specific Python version as interpreter in Ansible?

Ansible needs 2.X version as interpreter.
we can declare required python version in inventory file
[ansible@ansibleserver ~]$ cat inventory_python
10.25.12.10 ansible_python_interpreter=/usr/bin/python2.7

[ansible@ansibleserver ~]$ ansible all -i inventory_python -m ping -u ansible
10.25.12.10 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Note:- if version not existed throws error.

Ansible inventory

inventory Features:-
1)Behavioral Parameters
2)Groups
3)Group of Groups
4)Assign Variables
5)Scaling Out using multiple files
6)Static/Dynamic
examples:-
cat /etc/ansible/ansible.cfg|grep -i inventory
[ansible@ansibleserver ~]$ cat /etc/ansible/ansible.cfg |grep -i inventory
inventory      = /etc/ansible/hosts

#cat /etc/ansible/hosts
10.25.12.11
10.25.12.10
10.25.12.13
[db]
10.25.12.10

[web]
10.25.12.13

[datacenter:children]
db
web

To know List of Ansible hosts:-
[ansible@ansibleserver ~]$ ansible all --list-hosts
  hosts (3):
    10.25.12.10
    10.25.12.11
    10.25.12.13
[ansible@ansibleserver ~]$ ansible db --list-hosts
  hosts (1):
    10.25.12.10
[ansible@ansibleserver ~]$ ansible web --list-hosts
  hosts (1):
    10.25.12.13
[ansible@ansibleserver ~]$ ansible datacenter --list-hosts
  hosts (2):
    10.25.12.10
    10.25.12.13

=================
we can create local inventories as well.
ex:-
[ansible@ansibleserver ~]$ cat inventory
10.25.12.13 ansible_ssh_user=ansible ansible_ssh_pass=ansible
[ansible@ansibleserver ~]$ ansible all -i inventory -m ping
10.25.12.13 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
[ansible@ansibleserver ~]$ ansible all -i inventory -a 'df -h'
10.25.12.13 | CHANGED | rc=0 >>
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   18G  2.9G   15G  17% /
devtmpfs               481M     0  481M   0% /dev
tmpfs                  490M     0  490M   0% /dev/shm
tmpfs                  490M  6.9M  484M   2% /run
tmpfs                  490M     0  490M   0% /sys/fs/cgroup
/dev/sda1              497M  119M  379M  24% /boot

[ansible@ansibleserver ~]$ ansible all -i inventory -a 'hostname'
10.25.12.13 | CHANGED | rc=0 >>
clinet.example.com

==============
[ansible@ansibleserver ~]$ cat inventory
10.25.12.13 ansible_ssh_user=ansible ansible_ssh_pass=ansible
10.25.12.11 ansible_ssh_user=ansible ansible_ssh_pass=ansible ansible_python_interpreter=/usr/bin/python
[ansible@ansibleserver ~]$ ansible all -i inventory -a 'hostname'
10.25.12.11 | CHANGED | rc=0 >>
yumserver.example.com

10.25.12.13 | CHANGED | rc=0 >>
clinet.example.com

==============
Declaring inventory Varibles:-
[ansible@ansibleserver ~]$ cat inventory
10.25.12.13 ansible_ssh_user=ansible ansible_ssh_pass=ansible
10.25.12.11 ansible_ssh_user=ansible ansible_ssh_pass=ansible ansible_python_interpreter=/usr/bin/python
[db]
10.25.12.11

[web]
10.25.12.13

[datacenter:children]
db
web

[datacenter:vars]
ansible_ssh_user=student
ansible_ssh_pass=student
[ansible@ansibleserver ~]$

[ansible@ansibleserver ~]$ ansible datacenter -i inventory -m ping
10.25.12.11 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
10.25.12.13 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

ANSIBLE Architecture

1) Ansible Architecture:-
inventory+modules=Ansible playbook
Ansible Playbook --> Ansible config -->PYTHON --> SSH --> multiple clients