Sunday, April 26, 2020

Working with Ansible Roles

As you know in every organisation, we have diffrent envoirment, such as PRODUCTION, DEVLOPMENT, DATABASE and WEB etc., so this ansible roles make your task easy to organise your diffrent playbook and other data which required for playbook,, such as templated, file vars etc.

Using Roles make larger projects are more manageable,Ansible roles allow you to organize their playbooks into smaller playbooks and files.


Following is Ansible role's directory structure.
==============================
[ansible@ansiblemaster roles]$ tree motd/
motd/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml


defaults: The main.yml file in this directory contains the default values of role variables and it can be overwritten when we use the role in playbook.

files:  Static files that are referenced by role tasks are stored in this directory.

handlers: This directory contains the role's handler definitions in main.yml  file.

meta:  This directory contains information about the role, including author, license, platforms, and optional role dependencies under main.yml file.

tasks: This directory contains the role's task definitions in main.yml file.

templates:  Jinja2 templates that are referenced by role tasks stored in this directory.

tests:     test.yml playbook that can be used to test the role.

vars:     This directory defines the role's variable values, in main.yml file


[ansible@ansiblemaster roles]$ pwd
/home/ansible/roles
[ansible@ansiblemaster roles]$
[ansible@ansiblemaster roles]$ cat ../inventory
[webserver]
web1

[dbserver]
db1
[ansible@ansiblemaster roles]$


[ansible@ansiblemaster ~]$ mkdir roles


[ansible@ansiblemaster ~]$ cd roles/


Note: You can create ansible role directory structure  manually or let ansible do this for you using "ansible-galaxy init < role name> "  as mentioned below.
 
[ansible@ansiblemaster roles]$ ansible-galaxy init motd
- motd was created successfully


[ansible@ansiblemaster roles]$ tree motd/
motd/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

8 directories, 8 files
[ansible@ansiblemaster roles]$



===================================
Let`s create template for motd role:
===================================

[ansible@ansiblemaster roles]$ vi  motd/templates/motd.j2
This system is {{ ansible_hostname }} server of  mylinuxfriends.blogspot.com
Today date is : {{ ansible_date_time.date }}

You can drop email to {{ system_owner }} in case any query.
Thanks......!

======================================================================================
Create default value for the role under default/main.yml  directory of motd role
=====================================================================================

[ansible@ansiblemaster roles]$ vi motd/defaults/main.yml
# defaults file for motd
---
system_owner: anuj@mylinuxfriends.blogspot.com
[ansible@ansiblemaster roles]$


======================================================================================
Now let`s use this role in playbook, create playbook motd.playbook.yml
======================================================================================

[ansible@ansiblemaster roles]$ vi motd.playbook.yml
---
- name: use motd role playbook
  hosts: all
  user: ansible
  become: true
  roles:
    - motd

============================================================
               Lets check current contents of /etc/motd of managed hosts
============================================================


[ansible@ansiblemaster roles]$ ansible all -i  ../inventory -m command -a 'cat /etc/motd'
db1 | SUCCESS | rc=0 >>
This server has Total memory: 1014 MBs.
free memory on this system is : 760 MBs.

Note: if you see any memory issue email: ansible@localhost

web1 | SUCCESS | rc=0 >>


[ansible@ansiblemaster roles]$



======================================
Now lets check syntex for motd.playbook.ymk
 =====================================

[ansible@ansiblemaster roles]$ ansible-playbook -l webserver -i ../inventory motd.playbook.yml -C

PLAY [use motd role playbook] *****************************************************************************************************************************************

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

TASK [motd : updating /etc/motd on managed node] **********************************************************************************************************************
changed: [web1]

PLAY RECAP ************************************************************************************************************************************************************
web1                       : ok=2    changed=1    unreachable=0    failed=0 



================================ 
Lets execute playbook
================================
[ansible@ansiblemaster roles]$ ansible-playbook -l webserver -i ../inventory motd.playbook.yml -vv
ansible-playbook 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/ansible/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible-playbook
  python version = 2.7.5 (default, Aug  7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Using /etc/ansible/ansible.cfg as config file

PLAYBOOK: motd.playbook.yml *******************************************************************************************************************************************
1 plays in motd.playbook.yml

PLAY [use motd role playbook] *****************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [web1]
META: ran handlers

TASK [motd : updating /etc/motd on managed node] **********************************************************************************************************************
task path: /home/ansible/roles/motd/tasks/main.yml:4
changed: [web1] => {"changed": true, "checksum": "bfdfda31a068005d18e7c70a7838e0a425fb194a", "dest": "/etc/motd", "gid": 0, "group": "root", "md5sum": "5cbf58f3aa0bd915548f2606d9cbea11", "mode": "0444", "owner": "root", "secontext": "system_u:object_r:etc_t:s0", "size": 158, "src": "/home/ansible/.ansible/tmp/ansible-tmp-1587895613.12-157428397814000/source", "state": "file", "uid": 0}
META: ran handlers
META: ran handlers

PLAY RECAP ************************************************************************************************************************************************************
web1                       : ok=2    changed=1    unreachable=0    failed=0  

[ansible@ansiblemaster roles]$



=====================================================================================
After execution of our playbook, Lets check contents of /etc/motd of managed hosts
=====================================================================================

[ansible@ansiblemaster roles]$ ansible webserver  -i ../inventory -m command -a 'cat /etc/motd'
web1 | SUCCESS | rc=0 >>
This system is web1 server of  mylinuxfriends.blogspot.com
Today date is : 2020-04-26

You can drop email to anuj@mylinuxfriends.blogspot.com in case any query.
Thanks......!



[ansible@ansiblemaster roles]$
[ansible@ansiblemaster roles]$ ansible dbserver  -i ../inventory -m command -a 'cat /etc/motd'
db1 | SUCCESS | rc=0 >>
This server has Total memory: 1014 MBs.
free memory on this system is : 760 MBs.

Note: if you see any memory issue email: ansible@localhost

[ansible@ansiblemaster roles]$


   

Tuesday, April 21, 2020

Ansible jinja2 template

How to create ansible jinja2 template and use in playbook
======================================

Ansible uses the Jinja2 template to modify files before they are distributed to managed hosts, it is preferable to avoid modifying configuration files through logic in templates and  templates can be useful when systems need to have slightly modified versions of the same file.

Following is tree structure of our jijna2 template, that we will use to create and use template.



Let`s gather some facts it from setup module, and we will use it in jinja template
==================================================

 [ansible@ansiblemaster templates]$ ansible localhost -m setup | grep -i ansible_distribution
        "ansible_distribution": "CentOS",
        "ansible_distribution_file_parsed": true,
        "ansible_distribution_file_path": "/etc/redhat-release",
        "ansible_distribution_file_variety": "RedHat",
        "ansible_distribution_major_version": "7",
        "ansible_distribution_release": "Core",
        "ansible_distribution_version": "7.7.1908",


 [ansible@ansiblemaster templates]$ ansible localhost -m setup | grep -i ansible_distribution_version
        "ansible_distribution_version": "7.7.1908",

[ansible@ansiblemaster templates]$

[ansible@ansiblemaster templates]$ ansible localhost -m setup | grep -i ansible_architecture
        "ansible_architecture": "x86_64",

[ansible@ansiblemaster templates]$

[ansible@ansiblemaster templates]$



1.) [ansible@ansiblemaster ~]$  cd templates
[ansible@ansiblemaster templates]$


[ansible@ansiblemaster templates]$  vi motd-facts.j2

This system is based on {{ ansible_distribution }} {{ ansible_distribution_version }} deployed on a  {{ ansible_architecture }} architecture.

:wq!

2.)  [ansible@ansiblemaster templates]$ cat playbook.yml
---
- hosts: all
  tasks:
    - template:
        src: motd-facts.j2
        dest: /etc/motd
        owner: root
        group: root
        mode: 0644

[ansible@ansiblemaster templates]$


3.) Now let`s  see whats motd showing for all managed hosts, run adhoc command.

 [ansible@ansiblemaster templates]$ ansible all -m command -a 'cat /etc/motd'
db1 | SUCCESS | rc=0 >>
db1 192.168.122.180 52:54:00:cf:9c:08 2

web1 | SUCCESS | rc=0 >>
web1 192.168.122.188 52:54:00:66:0f:09 1

4.) lets check syntax of current playbook  and then will run it if no issue found.

[ansible@ansiblemaster templates]$ ansible-playbook playbook.yml --syntax-check

playbook: playbook.yml

5.) Now run our playbook with jinja2 template

 [ansible@ansiblemaster templates]$ ansible-playbook playbook.yml -vv -b
ansible-playbook 2.4.2.0
  config file = /home/ansible/templates/ansible.cfg
  configured module search path = [u'/home/ansible/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible-playbook
  python version = 2.7.5 (default, Aug  7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Using /home/ansible/templates/ansible.cfg as config file

PLAYBOOK: playbook.yml ************************************************************************************************************************************************
1 plays in playbook.yml

PLAY [all] ************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [db1]
ok: [web1]
META: ran handlers

TASK [template] *******************************************************************************************************************************************************
task path: /home/ansible/templates/playbook.yml:4
changed: [db1] => {"changed": true, "checksum": "39df069d58782975273f42569700e369bd1b66ee", "dest": "/etc/motd", "gid": 0, "group": "root", "md5sum": "04e8d49e92ec67834a73d1c1b2dff83c", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:etc_t:s0", "size": 76, "src": "/home/ansible/.ansible/tmp/ansible-tmp-1587468294.96-1416932211518/source", "state": "file", "uid": 0}
changed: [web1] => {"changed": true, "checksum": "39df069d58782975273f42569700e369bd1b66ee", "dest": "/etc/motd", "gid": 0, "group": "root", "md5sum": "04e8d49e92ec67834a73d1c1b2dff83c", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:etc_t:s0", "size": 76, "src": "/home/ansible/.ansible/tmp/ansible-tmp-1587468294.95-57618716391126/source", "state": "file", "uid": 0}
META: ran handlers
META: ran handlers

PLAY RECAP ************************************************************************************************************************************************************
db1                        : ok=2    changed=1    unreachable=0    failed=0 
web1                       : ok=2    changed=1    unreachable=0    failed=0 

[ansible@ansiblemaster templates]$


[ansible@ansiblemaster templates]$


[ansible@ansiblemaster templates]$

[ansible@ansiblemaster ~]$ tree templates/
templates/
├── ansible.cfg
├── inventory
├── motd-facts.j2
└── playbook.yml

0 directories, 4 files
[ansible@ansiblemaster ~]$

6.) Now let`s  run adhoc command to see changes are showing in motd file of managed hosts.



[ansible@ansiblemaster templates]$ ansible all -m command -a 'cat /etc/motd'
web1 | SUCCESS | rc=0 >>
This system is based on CentOS 7.7.1908 deployed on a  x86_64 architecture.

db1 | SUCCESS | rc=0 >>
This system is based on CentOS 7.7.1908 deployed on a  x86_64 architecture.

[ansible@ansiblemaster templates]$




==========================================
            Now lets do some more on jinja2 template
==========================================

ansible@ansiblemaster templates]$vi  motd-facts.j2
Today date is {{ ansible_date_time.date }},
system is based on {{ ansible_distribution }} with hostname {{ ansible_hostname }}
{{ ansible_distribution_version }} deployed on a  {{ ansible_architecture }} architecture.
You can ask {{ system_owner }} for access
[ansible@ansiblemaster templates]$

[ansible@ansiblemaster templates]$ vi playbook.yml
---
- hosts: all
  user: ansible
  become: true
  vars:
    system_owner: ansible@localhost
  tasks:
    - template:
        src: motd-facts.j2
        dest: /etc/motd
        owner: root
        group: root
        mode: 0644
[ansible@ansiblemaster templates]$



******** Check syntax and then run playbook ********


[ansible@ansiblemaster templates]$ ansible-playbook playbook.yml --syntax-check

playbook: playbook.yml



[ansible@ansiblemaster templates]$
[ansible@ansiblemaster templates]$

[ansible@ansiblemaster templates]$ ansible-playbook playbook.yml -C

PLAY [all] ************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [db1]
ok: [web1]

TASK [template] *******************************************************************************************************************************************************
changed: [web1]
changed: [db1]

PLAY RECAP ************************************************************************************************************************************************************
db1                        : ok=2    changed=1    unreachable=0    failed=0  
web1                       : ok=2    changed=1    unreachable=0    failed=0  

[ansible@ansiblemaster templates]$



******** RUN playbook as no error reported ********

 ansible@ansiblemaster templates]$ ansible-playbook playbook.yml -v
Using /home/ansible/templates/ansible.cfg as config file

PLAY [all] ************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [web1]
ok: [db1]

TASK [template] *******************************************************************************************************************************************************
changed: [web1] => {"changed": true, "checksum": "d1c17b727e137320f2e0883c5023eec114d200b1", "dest": "/etc/motd", "gid": 0, "group": "root", "md5sum": "eb738f69de06acabe042739dee765d23", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:etc_t:s0", "size": 158, "src": "/home/ansible/.ansible/tmp/ansible-tmp-1587469475.49-54949771310688/source", "state": "file", "uid": 0}
changed: [db1] => {"changed": true, "checksum": "8f6d3d0305a193bf274f62c2b99698df6b75a816", "dest": "/etc/motd", "gid": 0, "group": "root", "md5sum": "06e2c4fcb023acb78548bf33a77100ed", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:etc_t:s0", "size": 157, "src": "/home/ansible/.ansible/tmp/ansible-tmp-1587469475.5-272023629871730/source", "state": "file", "uid": 0}

PLAY RECAP ************************************************************************************************************************************************************
db1                        : ok=2    changed=1    unreachable=0    failed=0 
web1                       : ok=2    changed=1    unreachable=0    failed=0



******* Now run adhoc command and see /etc/motd of managed hosts ********

[ansible@ansiblemaster templates]$ ansible webserver -m command -a 'cat /etc/motd'
web1 | SUCCESS | rc=0 >>
Today date is 2020-04-21,
system is based on CentOS with hostname web1
7.7.1908 deployed on a  x86_64 architecture.
You can ask ansible@localhost for access

[ansible@ansiblemaster templates]$





          ===========================================
                            
              Lets` do some more handson on  jinja2 template
        ============================================

Now lets caputure some facts that we will use in jinja2 template
===================================================================

[ansible@ansiblemaster]$ ansible localhost -m setup -a 'filter=ansible_memtotal_mb'
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_memtotal_mb": 1837
    },
    "changed": false
}
[ansible@ansiblemaster jinja2-lab]$
[ansible@ansiblemaster jinja2-lab]$ ansible localhost -m setup -a 'filter=ansible_freemem_mb'
localhost | SUCCESS => {
    "ansible_facts": {},
    "changed": false
}
[ansible@ansiblemaster]$


[ansible@ansiblemaster templates]$ mkdir jinja2-lab

[ansible@ansiblemaster jinja2-lab]$ vi motd-fact.j2  <------ create motd-fact.j2 template
This server has Total memory: {{ ansible_memtotal_mb }} MBs.
free memory on this system is : {{ ansible_memfree_mb }} MBs.

Note: if you see any memory issue email: ansible@localhost


[ansible@ansiblemaster jinja2-lab]$


[ansible@ansiblemaster jinja2-lab]$ vi playbook.yml
---
- hosts: all
  user: ansible
  become: true
  tasks:
    - template:
        src: motd-fact.j2
        dest: /etc/motd
        owner: root
        group: root
        mode: 0644
[ansible@ansiblemaster jinja2-lab]$


***** Now let`s check current message in /etc/mots on managed hosts *********


ansible@ansiblemaster jinja2-lab]$ ansible all -m command -a 'cat /etc/motd'
web1 | SUCCESS | rc=0 >>


db1 | SUCCESS | rc=0 >>



*******************************************************************************
Currently /etc/motd is empty, so now lets do syntax check for playbook 
thereafter will execute if no error reported
*******************************************************************************


ansible@ansiblemaster jinja2-lab]$ ansible-playbook playbook.yml -C

PLAY [all] ************************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************
ok: [db1]
ok: [web1]

TASK [template] *******************************************************************************************************************************************************
changed: [web1]
changed: [db1]

PLAY RECAP ************************************************************************************************************************************************************
db1                        : ok=2    changed=1    unreachable=0    failed=0  
web1                       : ok=2    changed=1    unreachable=0    failed=0  

[ansible@ansiblemaster jinja2-lab]$

[ansible@ansiblemaster jinja2-lab]$ ansible-playbook playbook.yml --syntax-check

playbook: playbook.yml
[ansible@ansiblemaster jinja2-lab]$


**************************************
playbook is good to execute,
lets execute on single group "dbserver"
 ***************************************

[ansible@ansiblemaster jinja2-lab]$ ansible-playbook -l dbserver playbook.yml -v
Using /home/ansible/templates/jinja2-lab/ansible.cfg as config file

PLAY [all] ************************************************************************************************************************************************************

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

TASK [template] *******************************************************************************************************************************************************
changed: [db1] => {"changed": true, "checksum": "7a05cea115ae70498513822ddd99c5ce55a6e6b2", "dest": "/etc/motd", "gid": 0, "group": "root", "md5sum": "568523a8e84cb5cef937fde8375d3374", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:etc_t:s0", "size": 141, "src": "/home/ansible/.ansible/tmp/ansible-tmp-1587481398.82-161512122431271/source", "state": "file", "uid": 0}

PLAY RECAP ************************************************************************************************************************************************************
db1                        : ok=2    changed=1    unreachable=0    failed=0  



****************************************************************
Now lets check motd of dbserver, its should show updated message *****************************************************************

[ansible@ansiblemaster jinja2-lab]$ ansible dbserver -m command -a 'cat /etc/motd'
db1 | SUCCESS | rc=0 >>
This server has Total memory: 1014 MBs.
free memory on this system is : 760 MBs.

Note: if you see any memory issue email: ansible@localhost


***********************************************************************
lets check motd of webserver showing new motd message or not  
***********************************************************************

[ansible@ansiblemaster jinja2-lab]$
[ansible@ansiblemaster jinja2-lab]$ ansible webserver -m command -a 'cat /etc/motd'
web1 | SUCCESS | rc=0 >>


[ansible@ansiblemaster jinja2-lab]$





kubernetes Pod Scheduling

 ===================   Deployment ================= 1.) Deployment without any nodeName or nodeSelector, pod will spread among all of the av...