Ansible_Role_Template_Example

Ansible_Role_Template_Example

Ansible_Role_Template_Example

In this example, I am using RHEL7 with below configuration.

[root@feenixdv apache_ntp]# ansible --version
ansible 2.7.5
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  2 2016, 04:20:16) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]

I am creating below task using roles and template.

  1. Copy customize “resolve.conf” and “ntp.conf” on target servers.
  2. Install Apache and copy customize “index.html” on target servers.

Here I am creating two roles inside “/etc/ansible/roles/apache_ntp”.

  1. Common
  2. web

To create role and template structure use “ansible-galaxy" command.

[root@feenixdv apache_ntp]# ansible-galaxy init /etc/ansible/roles/apache_ntp/common  --offline
/etc/ansible/roles/apache_ntp/common  was created successfully

[root@feenixdv apache_ntp]# ansible-galaxy init /etc/ansible/roles/apache_ntp/web  --offline
/etc/ansible/roles/apache_ntp/web  was created successfully

Finally, we have below the directory structure for common.

[root@feenixdv apache_ntp]# pwd
/etc/ansible/roles/apache_ntp
[root@feenixdv apache_ntp]# tree common/
common/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
│   ├── ntp.conf
│   └── resolv.conf
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml
8 directories, 10 files

And below directory structure for the web.

[root@feenixdv apache_ntp]# pwd
/etc/ansible/roles/apache_ntp

[root@feenixdv apache_ntp]# tree web/
web/
├── defaults
│   └── main.yml
├── files
│   └── index.html
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml
8 directories, 9 files

For common roles:-

For “common/tasks/main.yml”

[root@feenixdv apache_ntp]# cat common/tasks/main.yml
---
- name: Configure DNS client
  template: src=resolv.conf dest=/etc/resolv.conf
  tags: dns

- name: Install NTP
  yum: name=ntp state=present
  tags: ntp

- name: Copy NTP file
  template: src=ntp.conf dest=/etc/ntp.conf
  tags: ntp
  notify: restart ntp service

For “common/templates/resolv.conf”

[root@feenixdv apache_ntp]# cat common/templates/resolv.conf
# Generated by NetworkManager
search {{ dnsserver }}
nameserver {{ dnsserver }}

Here “dnsserver” variable picked from “group_vars/all” which is looking like below.

[root@feenixdv apache_ntp]# pwd
/etc/ansible/roles/apache_ntp
[root@feenixdv apache_ntp]# cat group_vars/all
---

dnsserver: 8.8.8.8
ntpserver: 192.168.40.177

For “common/templates/ntp.conf”

[root@feenixdv apache_ntp]# cat common/templates/ntp.conf
##anisible managed file
driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict -6 ::1

server {{ ntpserver  }}
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

Here “ntpserver” variable picked from “group_vars/all”

For “common/handlers/main.yml”

[root@feenixdv apache_ntp]# cat common/handlers/main.yml
---
# handlers file for apache_ntp/common
- name: restart ntp service
  service: name=ntpd state=started

For web roles:-

For “web/tasks/main.yml”

[root@feenixdv apache_ntp]# cat web/tasks/main.yml
---
# tasks file for apache_ntp/web
- name: Install apache packages
  yum: name={{ item }} state=present
  with_items:
    - httpd
    - httpd-tools

- name: Copy apache index page
  copy: src=index.html dest=/var/www/html/index.html
  tags: apache
  notify: restart apache service

For “web/files/index.html”

[root@feenixdv apache_ntp]# cat web/files/index.html
Hello ansible

For “web/handlers/main.yml”

[root@feenixdv apache_ntp]# cat web/handlers/main.yml
---
# handlers file for apache_ntp/common
- name: restart apache service
  service: name=httpd state=started

Now configure host file and main.yml to call role one by one.

[root@feenixdv apache_ntp]# pwd
/etc/ansible/roles/apache_ntp
[root@feenixdv apache_ntp]# cat hosts
[all]
192.168.40.178
192.168.40.179
[web]
192.168.40.178

[root@feenixdv apache_ntp]# cat main.yml
---
- hosts: all
  tasks:

- name: Apply common configuration
  hosts: all
  user: root
  become: yes
  tasks:
  roles:
    - common

- name: Install and configure apache web server
  hosts: web
  user: root
  become: yes
  tasks:
  roles:
    - web

Now check syntax.

[root@feenixdv apache_ntp]# ansible-playbook main.yml -i hosts --syntax-check
playbook: main.yml

Looking good

Now launch the play book.

[root@feenixdv apache_ntp]# ansible-playbook main.yml -i hosts

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

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

ok: [192.168.40.179]

PLAY [Apply common configuration] **********************************************************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************************************************************************
ok: [192.168.40.179]
ok: [192.168.40.178]

TASK [common : Configure DNS client] *******************************************************************************************************************************************************************
changed: [192.168.40.179]
changed: [192.168.40.178]

TASK [common : Install NTP] ****************************************************************************************************************************************************************************
ok: [192.168.40.178]
ok: [192.168.40.179]

TASK [common : Copy NTP file] **************************************************************************************************************************************************************************
changed: [192.168.40.179]
changed: [192.168.40.178]

RUNNING HANDLER [common : restart ntp service] *********************************************************************************************************************************************************
ok: [192.168.40.178]
ok: [192.168.40.179]

PLAY [Install and configure apache web server] ********************************************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************************************************************************
ok: [192.168.40.178]
TASK [web : Install apache packages] *******************************************************************************************************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple items and specifying `name: "{{ item }}"`, please use

`name: ['httpd', 'httpd-tools']` and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [192.168.40.178] => (item=[u'httpd', u'httpd-tools'])

TASK [web : Copy apache index page] ********************************************************************************************************************************************************************
changed: [192.168.40.178]
RUNNING HANDLER [web : restart apache service] *********************************************************************************************************************************************************
changed: [192.168.40.178]

PLAY RECAP *********************************************************************************************************************************************************************************************
192.168.40.178             : ok=10   changed=5    unreachable=0    failed=0

192.168.40.179             : ok=6    changed=2    unreachable=0    failed=0

Looking good.

Check action.

For “192.168.40.179  “

[root@localhost ~]# ifconfig |grep inet
        inet 192.168.40.179  netmask 255.255.255.0  broadcast 192.168.40.255

        inet6 fe80::25c:a552:e7aa:8f2  prefixlen 64  scopeid 0x20<link>

        inet6 fe80::9e73:c84:139c:c56c  prefixlen 64  scopeid 0x20<link>

        inet 127.0.0.1  netmask 255.0.0.0

        inet6 ::1  prefixlen 128  scopeid 0x10<host>

[root@localhost ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search 8.8.8.8
nameserver 8.8.8.8
[root@localhost ~]# cat /etc/ntp.conf
##anisible managed file
driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict -6 ::1
server 192.168.40.177
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

For “192.168.40.178 “

Apache service accessible

 

Leave a Reply

Your email address will not be published. Required fields are marked *