Managing Jinja2 Templates Using Ansible

Ticker

6/recent/ticker-posts

Managing Jinja2 Templates Using Ansible

Question : 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 2.

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 2 in /var/www/html/index.html. Also make sure that /var/www/html/index.html file permissions 0644.

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).


Please Note :-  Perform the below commands based on your question server,  user name & other details that might differ. So please read the task carefully before executing it. All the Best 👍

Solution:  

1Go through the folder mentioned in task and verified the playbook     

thor@jump_host /$ cd  ~/ansible/

thor@jump_host ~/ansible$

thor@jump_host ~/ansible$ ll

total 12

-rw-r--r-- 1 thor thor  237 Jul 19 14:39 inventory

-rw-r--r-- 1 thor thor   73 Jul 19 14:39 playbook.yml

drwxr-xr-x 3 thor thor 4096 Jul 19 14:40 role

thor@jump_host ~/ansible$


2.  Edit the exiting file /home/thor/ansible/playbook.yml and add the required host

thor@jump_host ~/ansible$ cat playbook.yml

---

- hosts:

  become: yes

  become_user: root

  roles:

    - role/httpd

thor@jump_host ~/ansible$

thor@jump_host ~/ansible$ vi playbook.yml

thor@jump_host ~/ansible$ cat playbook.yml

---

- hosts: stapp02

  become: yes

  become_user: root

  roles:

    - role/httpd

thor@jump_host ~/ansible$

    
3.  Create a Jinja2 template file with the below content

thor@jump_host ~/ansible$ vi /home/thor/ansible/role/httpd/templates/index.html.j2

thor@jump_host ~/ansible$ cat  /home/thor/ansible/role/httpd/templates/index.html.j2

This file was created using Ansible on {{ ansible_hostname }}

thor@jump_host ~/ansible$


4.  Edit the file to add a task to copy the Jinja2 template ( refer below video)

thor@jump_host ~/ansible$ vi /home/thor/ansible/role/httpd/tasks/main.yml

thor@jump_host ~/ansible$ cat  /home/thor/ansible/role/httpd/tasks/main.yml

---

#task file for role/test

- name: install the latest version of httpd

  yum:

    name: httpd

    state: latest

- name: Start service httpd

  service:

    name: httpd

    state: started

- name: Use Jinja2 template to generate index.html

  template:

    src: /home/thor/ansible/role/httpd/templates/index.html.j2

    dest: /var/www/html/index.html

    mode: "0644"

    owner: "{{ ansible_user }}"

    group: "{{ ansible_user }}"

thor@jump_host ~/ansible$

 
5.  Post file saved , run below command to execute the playbook 

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

 PLAY [stapp02] **************************************************************************************************************

 TASK [Gathering Facts] **********************************************************************************

ok: [stapp02]

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

changed: [stapp02]

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

changed: [stapp02]

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

changed: [stapp02]

 PLAY RECAP ******************************************************************************************************************

stapp02                    : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

 thor@jump_host ~/ansible$


6. Validate the task by running the below command     

thor@jump_host ~/ansible$ ansible stapp02 -a "cat /var/www/html/index.html" -i inventory

stapp02 | CHANGED | rc=0 >>

This file was created using Ansible on stapp02

thor@jump_host ~/ansible$


7. Click on Finish & Confirm to complete the task successfully

Happy Learning!!!!


Apart from this if you need more clarity,  I have made a  tutorial video on this , please go through and share your comments. Like and share the knowledge



Post a Comment

0 Comments

Latest Posts

KodeKloud Kubernetes Security CKS  Lab Challenge 4 |  Audit-policy | Install & configure falco utility | Inspect the API server audit logs and identify the user