0%

0/1 Lessons

Course Introduction

• 19min

0 / 2 lessons complete

IT Lab Setup

• 31min

0 / 3 lessons complete

Installing Ansible

• 57min

0 / 4 lessons complete

Managing your Ansible Inventory

• 2hr 45min

0 / 8 lessons complete

Ansible Basics

• 2hr 54min

0 / 9 lessons complete

Ansible Roles

• 2hr 38min

0 / 8 lessons complete

Ansible Galaxy

• 2hr 28min

0 / 6 lessons complete

Ansible Facts, Variables, Passwords and Templates

• 3hr 20min

0 / 9 lessons complete

Advanced Ansible Playbook Creation

• 2hr 23min

0 / 8 lessons complete

Course Conclusion

• 11min

0 / 1 lessons complete

Templating with Jinja2

Instructions

Q&A (0)

Notes (0)

Resources (0)

Saving Progress...

Resources

There are no resources for this lesson.

Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.

Create note

In this lesson, you will learn how to use Jinja2 for templating in Ansible. Templating is a powerful feature in Ansible that allows you to dynamically generate files based on variables. This is particularly useful for configuration files that differ slightly between hosts.

By the end of this lesson, you'll be able to create a Jinja2 template for an HTML file that includes the host's name, and update your first_playbook.yml to use this template. We will use the nginx application installed on managed-node-1 and managed-node-2 for this purpose.

Creating a Jinja2 Template

Jinja2 templates in Ansible allow you to create text files, such as HTML, where the content of the file can change depending on the variables. Let's create a Jinja2 template for an HTML file which displays the hostname of the server.

Start by navigating to the templates directory inside the webserver role.

cd ~/roles/webserver/

Now let's move our old index.html file from the files directory into the templates directory:

mv files/index.html templates/index.html.j2

Open this file with a text editor:

nano templates/index.html.j2

Let's update the file to include our {{ ansible_hostname }} variable like so:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to {{ ansible_hostname }}</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>This is a basic web page served from {{ ansible_hostname }} by Nginx that was placed by Ansible.</p>
</body>
</html>

Here, {{ ansible_hostname }} is a variable provided by Ansible which will be replaced with the actual hostname of the server where the template is deployed.

Updating the First Playbook

Let's cd back to our home directory where our ansible playbook is located:

cd ~

Next, we need to modify the first_playbook.yml to ensure the nginx role is installed and to deploy our new template to the web servers. In the next step, we are going to use the template ansible module. Refer to the help to see some examples before we get started:

ansible-doc template

Once you're done, open first_playbook.yml in a text editor:

nano first_playbook.yml

Now we can remove all variable files other than secret.yml and other tasks. Next, we want to add two new tasks for installing nginx to make sure that is present as well as templating our j2 file.

Here’s how you can update your playbook:

---
- hosts: webservers
  vars_files:
    - secret.yml
  tasks:

    # Ensure nginx is installed
    - name: Install nginx
      become: yes
      apt:
        name: nginx
        state: present

    # Deploy templated HTML file
    - name: Deploy templated HTML
      become: yes
      template:
        src: roles/webserver/templates/index.html.j2
        dest: /var/www/html/index.html

Here is a breakdown of what this playbook does:

Let's run the playbook and make sure it executes without error:

ansible-playbook first_playbook.yml

Enter the vault password and wait for it to complete. You should see a change where the templated HTML file was deployed to the server:

Ansible Playbook Templating HTML File
Ansible Playbook Templating HTML File

Verification

Now we can open a browser on our host computer and navigate to the IP address of our managed-node-1 server, and you should see something like the following:

Ansible Templated Web Page
Ansible Templated Web Page

You could also just curl the IP address or DNS name of the servers from our Ansible Controller like so:

curl managed-node-1
Curl of Ansible Managed Nodes
Curl of Ansible Managed Nodes

This will return the raw web page to our Ansible Controller, and we can see our templated values (managed-node-1 or managed-node-2) being presented.

Conclusion

This hands-on experience with Jinja2 templating in Ansible helps you understand how dynamic content can be generated and deployed across different hosts.

Server Academy Members Only

Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments