Managing Jinja2 Templates Using Ansible

Task :

One of the Nautilus DevOps team members is working to develop a role for httpd installation and configuration. Work is almost completed, however there is a requirement to add a jinja2 template for index.html file. Additionally, the relevant task needs to be added inside the role. The inventory file ~/ansible/inventory is already present on jump host that can be used. Complete the task as per details mentioned below:

a. Update ~/ansible/playbook.yml playbook to run the httpd role on App Server 3.

b. Create a jinja2 template index.html.j2 under /home/thor/ansible/role/httpd/templates/ and add a line This file was created using Ansible on <respective server> (for example This file was created using Ansible on stapp01 in case of App Server 1). Also please make sure not to hard code the server name inside the template. Instead, use inventory_hostname variable to fetch the correct value.

c. Add a task inside /home/thor/ansible/role/httpd/tasks/main.yml to copy this template on App Server 3 in /var/www/html/index.html. Also make sure that /var/www/html/index.html file permissions 0744.

d. The user/group owner of /var/www/html/index.html file must be respective sudo user of server (for example tony in case of stapp01).

Note: Validation will try to run playbook using command ansible-playbook -i inventory playbook.yml so please make sure playbook works this way, without passing any extra arguments.

Solution :

thor@jump_host /$ vi /home/thor/ansible/playbook.yml 
- hosts: stapp03
become: yes
- role/httpd

thor@jump_host /$ vi /home/thor/ansible/role/httpd/templates/index.html.j2
This file was created using Ansible on {{ ansible_hostname }}

thor@jump_host /$ vi /home/thor/ansible/role/httpd/tasks/main.yml 
# tasks file for role/test

- name: install the latest version of HTTPD
     name: httpd
     state: latest

- name: Start service httpd
     name: httpd
     state: started

- name: Use Jinja2 template to generate index.html
     src: /home/thor/ansible/role/httpd/templates/index.html.j2
     dest: /var/www/html/index.html
     mode: "0744"
     owner: "{{ ansible_user }}"
     group: "{{ ansible_user }}"

thor@jump_host /$ cd /home/thor/ansible/
thor@jump_host ~/ansible$ ansible-playbook -i inventory playbook.yml

PLAY [stapp03] *************************************************************************

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

TASK [role/httpd : install the latest version of HTTPD] ********************************
changed: [stapp03]

TASK [role/httpd : Start service httpd] ************************************************
changed: [stapp03]

TASK [role/httpd : Use Jinja2 template to generate index.html] *************************
changed: [stapp03]

PLAY RECAP *****************************************************************************
stapp03 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

thor@jump_host ~/ansible$ ansible stapp03 -a "cat /var/www/html/index.html" -i inventorystapp03 | CHANGED | rc=0 >>
This file was created using Ansible on stapp03
thor@jump_host ~/ansible$


Leave a Reply

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