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]$


   

No comments:

Post a Comment

kubernetes Pod Scheduling

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