FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Follow publication

Ansible And Jinja2

--

Folks who are familiar with ansible know how tiresome it can be to write configuration files or any file with changing parameters that are hardcoded. Maybe your job would have been easier if you could just loop certain things since things get repetitive, in these situations Jinja2 comes to the aid.

For those who do not know what Jinja2 is, it is a fast, expressive, extensible templating engine. Special placeholders in the template allow writing code similar to Python syntax. Then the template is passed data to render the final document. Consider it as Obi-Wan in the templating world.

Note: Please make sure you are familiar with the basics of ansible such as roles and playbooks along with the basics of Jinja before proceeding. I am also using docker to establish a local environment for ansible. If you want to do the same, follow the tutorial — Running ansible through docker

Let’s see how a typical Jinja file looks like. It has an extension of j2.

example.j2

{{ name }} & {{ profession }} are variables and can be used as shown above.

Now, let’s see it in action!

We are doing this locally using docker, I have created 4 machine instances, one is the master machine ( ansible ) and slave machines ( remote-host-one, remote-host-two, remote-host-three ).

I have also created an inventory file for the same as follows.

inventory.txt

We will install Nginx inside a host machine which in our case is remote-host-three, Nginx always runs on port 80 and has a default web page that looks like this

Default Nginx landing page

Now, our job is to create a playbook that installs Nginx and then replaces the Nginx template further, which will look something like this.

The path where the Nginx landing page is /var/www/html/index.nginx-debian.html will be replaced by our playbook.

example_template.j2 and main.yml

In the example_template.j2 file, I have simply added another line

<br />Hello, I am {{ firstName }} and I am a {{ profession }}.

Now run this playbook using ansible-playbook -i inventory.txt main.yml

Now, use the docker inspect remote-host-threecommand to get the IP of the container which in our case is 172.17.0.2 and you can simply put it in the browser and hit enter!

Custom Nginx page

As you can see we have successfully changed the landing page Nginx.

Now, let's see an example where I can loop through information using Jinja2.

I will be making a variable named team in main.yml

main.yml

I will also create an unordered list to iterate through the variable team as shown below.

example_template.j2

Again run the playbook using ansible-playbook -i inventory.txt main.yml and refresh the page.

As you can see, a list has been made along with the previous content of the page.

I will conclude the tutorial here, you can do all the things that you can do with Jinja2 such as applying filters, conditional statements, etc.

Feel free to post your doubts in the comment section along with your suggestions!

Happy Learning! : )

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Written by Vic

A software engineer, orator, video editor, chef (At least for myself), singer (Not exactly good), artist (Decent one) and a writer (Okayish one).

Responses (1)

Write a response