Day 92: Managing Jinja2 Templates Using Ansible | 100 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_hostnamevariable instead of hardcoding server names.
๐น What I Learned
Creating reusable Jinja2 templates
Using the Ansible Template module
Using the built-in
inventory_hostnamevariableUpdating 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.j2replaces
{{ 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 =
bannerGroup =
bannerPermissions =
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
- #managing-jinja2-templates-using-ansible
- #jinja2
- #jinja2-templates
- #ansible-jinja2
- #ansible-template
- #ansible
- #ansible-playbook
- #ansible-module
- #ansible-adhoc
- #ansible-role
- #ansible-automation
- #ansible-project
- #ansible-inventory-file
- #ansible-devops
- #ansible-roles
- #ansible-role-directory-structure
- #devops
- #devops-articles
- #devops-journey
- #devopscommunity
- #devops-trends
- #devops-tools
- #devops-engineer
- #devopsculture
- #devopsjourney
- #devopsproject
- #100daysofdevops
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.
Comments
Join the discussionNo comments yet. Be the first to comment.
100_days_of_devops
Documenting my 100 Days of DevOps journey with hands-on practice from KodeKloud, along with notes, commands, and mini projects