Skip to main content

Day 92: Managing Jinja2 Templates Using Ansible | 100 Days of DevOps

Updated 7 min readMarkdown
Part of series100_days_of_devops

Content

Today I enhanced an existing Ansible role by adding a Jinja2 template and a task to deploy it. This lab helped me learn how to use the Ansible Template module, create reusable templates, and dynamically generate content using the built-in inventory_hostname variable instead of hardcoding server names.


๐Ÿ”น What I Learned

  • Creating reusable Jinja2 templates

  • Using the Ansible Template module

  • Using the built-in inventory_hostname variable

  • Updating an existing Ansible role

  • Setting file ownership and permissions while deploying templates

  • Executing an Ansible role through a playbook


๐Ÿ”น Task Requirement

One of the Nautilus DevOps team members was developing a role for httpd installation and configuration. Most of the work had already been completed, but a few tasks were still pending.

I needed to:

  • Update the existing playbook:
/home/thor/ansible/playbook.yml

to execute the httpd role on App Server 3.

  • Create the following Jinja2 template:
/home/thor/ansible/role/httpd/templates/index.html.j2

containing:

This file was created using Ansible on <respective server>

without hardcoding the server name. Instead, I had to use:

{{ inventory_hostname }}
  • Add a task inside:
/home/thor/ansible/role/httpd/tasks/main.yml

to deploy the template as:

/var/www/html/index.html

with:

  • owner = respective sudo user

  • group = respective sudo user

  • permissions = 0755

Finally, the playbook had to execute successfully using:

ansible-playbook -i inventory playbook.yml

without any extra arguments.


๐Ÿ”น Steps I Followed

1. Navigated to the Ansible Working Directory

I first moved to the Ansible project directory on the jump host.

cd ~/ansible

2. Examined the Existing Inventory

Before making any changes, I checked the inventory file.

cat inventory

Output:

stapp01 ansible_host=stapp01 ansible_ssh_pass=Ir0nM@n ansible_user=tony
stapp02 ansible_host=stapp02 ansible_ssh_pass=Am3ric@ ansible_user=steve
stapp03 ansible_host=stapp03 ansible_ssh_pass=BigGr33n ansible_user=banner

Observation

The inventory was already configured for all three application servers.

I also noticed that each server has its own login user defined using:

ansible_user=

For App Server 3, the login user is:

banner

This meant I could use:

owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"

instead of hardcoding the username.


3. Checked the Existing Playbook

Before modifying the playbook, I viewed its contents.

cat playbook.yml

Output:

---
- hosts:
  become: yes
  become_user: root

  roles:
    - role/httpd

Observation

The playbook already existed and was configured to execute the httpd role.

However, the hosts field was empty.

Since the task required running the role only on App Server 3, I needed to update:

hosts: stapp03

4. Created the Required Jinja2 Template

The task required creating a template named:

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

I created it using:

vi /home/thor/ansible/role/httpd/templates/index.html.j2

Added the following line:

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

Saved the file.


Understanding the Template

The task specifically mentioned not to hardcode the server name.

Instead of writing:

This file was created using Ansible on stapp03

I used:

{{ inventory_hostname }}

inventory_hostname is a built-in Ansible variable that automatically resolves to the hostname of the current managed host.

For App Server 3, the rendered file becomes:

This file was created using Ansible on stapp03

This makes the template reusable for any server in the inventory.


5. Updated the Role Tasks

Next, I edited the role's task file.

vi /home/thor/ansible/role/httpd/tasks/main.yml

Added the following task:

- name: copy template to server
  ansible.builtin.template:
    src: templates/index.html.j2
    dest: /var/www/html/index.html
    owner: "{{ ansible_user }}"
    group: "{{ ansible_user }}"
    mode: '0755'

Final main.yml

---
# tasks 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: copy template to server
  ansible.builtin.template:
    src: templates/index.html.j2
    dest: /var/www/html/index.html
    owner: "{{ ansible_user }}"
    group: "{{ ansible_user }}"
    mode: '0755'

Observation

The role already handled:

  • installing Apache

  • starting the Apache service

The only missing part was deploying the Jinja2 template.

Therefore, I only needed to append one additional task instead of modifying the existing ones.


Understanding the Template Module

The Template module copies a Jinja2 template from the control node to the managed host after replacing any template variables.

In this task it:

  • reads index.html.j2

  • replaces {{ inventory_hostname }}

  • creates:

/var/www/html/index.html

It also applies:

owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
mode: "0755"

during deployment.


6. Updated the Playbook

After completing the role, I updated the playbook.

vi playbook.yml

Final playbook:

---
- hosts: stapp03
  become: yes
  become_user: root

  roles:
    - role/httpd

Observation

Only one change was required.

I updated the hosts field so the role would execute on App Server 3.


7. Executed the Playbook

I executed the playbook using the existing inventory.

ansible-playbook -i inventory playbook.yml

Output:

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 : copy template to server]
changed: [stapp03]

PLAY RECAP

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

๐Ÿ”น Understanding the Output

Gathering Facts

Ansible first collected information about the managed host.


HTTPD Installation

The existing role ensured the latest version of Apache HTTP Server was installed.


Service Configuration

The Apache service was started successfully.


Template Deployment

The Template module rendered:

{{ inventory_hostname }}

into:

stapp03

and copied the generated file to:

/var/www/html/index.html

with the required ownership and permissions.


Play Recap

ok=4
changed=3
failed=0

Observation

The playbook executed successfully without any failures.

This confirmed that both the updated playbook and the modified role were configured correctly.


8. Verified the Configuration

Verified the Generated File

ansible stapp03 -i inventory -m shell -a "cat /var/www/html/index.html"

Output:

This file was created using Ansible on stapp03

Observation

The hostname was automatically inserted by the Jinja2 template, confirming that inventory_hostname was rendered correctly.


Verified Through the Web Server

curl http://stapp03

Output:

This file was created using Ansible on stapp03

Observation

The Apache web server was successfully serving the generated page.


Verified Ownership and Permissions

ansible stapp03 -i inventory -m shell -a "ls -la /var/www/html/index.html"

Output:

-rwxr-xr-x 1 banner banner 48 Jul 1 13:56 /var/www/html/index.html

Observation

The verification confirmed:

  • Owner = banner

  • Group = banner

  • Permissions = 0755

This matched the task requirements because banner is the ansible_user defined for stapp03 in the inventory.


๐Ÿ”น Verification

โœ… Updated the playbook to execute the httpd role on App Server 3

โœ… Created the required Jinja2 template

โœ… Used inventory_hostname instead of hardcoding the hostname

โœ… Added the Template module task inside the role

โœ… Deployed the template to /var/www/html/index.html

โœ… Set the owner and group to the respective sudo user

โœ… Set file permissions to 0755

โœ… Verified the generated page using curl


๐Ÿ”น My Understanding

This task helped me understand how Jinja2 templates make Ansible roles more reusable by generating dynamic content during playbook execution. I also learned how the Template module deploys rendered templates while applying the required ownership and permissions.


๐Ÿ”น What I Found Interesting

I found it interesting that a single Jinja2 template can work for multiple servers by simply using {{ inventory_hostname }}. This small change makes the role more flexible, reusable, and easier to maintain.

๐Ÿ“Œ Full notes: GitHub link

About the author

  • Iโ€™m currently working in DevOps and documenting my learning journey along the way. From CI/CD pipelines to cloud and containers, Iโ€™m exploring how modern systems are built and deployed. This blog is where I share what I learn, break down concepts in simple terms, and track my progress. Learning one concept at a time, and trying to apply it practically.

No comments yet. Be the first to comment.

100_days_of_devops

Part 1 of 50

Documenting my 100 Days of DevOps journey with hands-on practice from KodeKloud, along with notes, commands, and mini projects

More from this blog