Turn Your Vehicle Into a Smart Earning Asset

While you’re not driving your car or bike, it can still be working for you. MOTOSHARE helps you earn passive income by connecting your vehicle with trusted renters in your city.

🚗 You set the rental price
🔐 Secure bookings with verified renters
📍 Track your vehicle with GPS integration
💰 Start earning within 48 hours

Join as a Partner Today

It’s simple, safe, and rewarding. Your vehicle. Your rules. Your earnings.

Ansible: How to use template in Ansible using Jinja2?

Here’s a comprehensive tutorial on Ansible Templates and Jinja2, suitable for engineers or anyone preparing for an advanced Ansible role.



1. What are Ansible Templates?

Ansible templates are files written in Jinja2 templating language that allow you to generate dynamic configuration files or scripts on your managed hosts. Templates let you insert variables, run loops, add conditional logic, and more—creating configuration files that are customized to each host or environment.

Templates in Ansible typically have the .j2 extension and are processed using the template module.


2. What is Jinja2?

Jinja2 is a powerful templating engine for Python. It allows embedding dynamic expressions and logic into text files (like configuration files, scripts, HTML, etc.).

Key Jinja2 features used in Ansible:

  • Variable substitution
  • Filters for modifying output
  • Conditionals (if, else)
  • Loops (for)
  • Includes for reusing template parts

3. Why Use Templates in Ansible?

  • DRY (Don’t Repeat Yourself): Write one template, render differently for each host.
  • Environment-specific files: Custom config for dev, staging, production, etc.
  • Host-specific configuration: Each server gets a config with its own variables (IP, hostname, ports, etc.).
  • Automate tedious file creation: No more manual editing!

4. Basic Usage of the Template Module

Syntax:

- name: Deploy custom configuration file
  template:
    src: myconfig.conf.j2
    dest: /etc/myapp/myconfig.conf
Code language: JavaScript (javascript)
  • src: Path to your template (relative to templates/ directory in your role/playbook).
  • dest: Destination path on the remote host.

5. Example 1: Simple Variable Substitution

Template file: greeting.txt.j2

Hello, my name is {{ name }} and I am a {{ role }}.

Playbook snippet:

- name: Generate greeting file
  hosts: all
  vars:
    name: "Rajesh"
    role: "DevOps Engineer"
  tasks:
    - name: Create greeting file
      template:
        src: greeting.txt.j2
        dest: /tmp/greeting.txt
Code language: JavaScript (javascript)

Resulting /tmp/greeting.txt:

Hello, my name is Rajesh and I am a DevOps Engineer.

6. Example 2: Looping with Jinja2

Template file: servers.txt.j2

Server List:
{% for server in servers %}
- {{ server }}
{% endfor %}
Code language: PHP (php)

Playbook snippet:

- name: Render list of servers
  hosts: localhost
  vars:
    servers:
      - web01.example.com
      - db01.example.com
      - cache01.example.com
  tasks:
    - name: Create server list file
      template:
        src: servers.txt.j2
        dest: /tmp/servers.txt
Code language: PHP (php)

Result:

Server List:
- web01.example.com
- db01.example.com
- cache01.example.com
Code language: CSS (css)

7. Example 3: Conditionals in Templates

Template file: nginx.conf.j2

server {
  listen 80;
  server_name {{ server_name }};
  {% if enable_ssl %}
  listen 443 ssl;
  ssl_certificate {{ ssl_cert }};
  ssl_certificate_key {{ ssl_key }};
  {% endif %}
}
Code language: PHP (php)

Playbook snippet:

- name: Render NGINX config
  hosts: all
  vars:
    server_name: "example.com"
    enable_ssl: true
    ssl_cert: "/etc/nginx/ssl/cert.pem"
    ssl_key: "/etc/nginx/ssl/key.pem"
  tasks:
    - name: Deploy nginx.conf
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
Code language: JavaScript (javascript)

8. Example 4: Using Filters

Filters let you modify variable values on the fly.

Template example:

All uppercase: {{ mystring | upper }}
Reversed: {{ mylist | reverse }}
Comma separated: {{ mylist | join(', ') }}
Code language: JavaScript (javascript)

Playbook snippet:

vars:
  mystring: "ansible"
  mylist:
    - a
    - b
    - c
Code language: JavaScript (javascript)

Result:

All uppercase: ANSIBLE
Reversed: c, b, a
Comma separated: a, b, c

9. Example 5: Template Directory Structure in Roles

A typical role with templates:

myrole/
├── tasks/
│   └── main.yml
├── templates/
│   ├── app.conf.j2
│   └── nginx.conf.j2

In tasks/main.yml:

- name: Configure app
  template:
    src: app.conf.j2
    dest: /etc/myapp/app.conf
Code language: JavaScript (javascript)

10. Advanced: Template Includes and Macros

You can include other templates or define macros for repeated sections.
For most playbooks, this isn’t needed, but Jinja2 supports it if you need.


11. Tips and Best Practices

  • Always use variables, not hardcoded values, in templates.
  • Test your templates with ansible-playbook --check before running in production.
  • Use comments in your .j2 files ({# comment #}) for clarity.
  • Store templates in the templates/ directory for roles or in the same directory as your playbook for small projects.

12. Common Jinja2 Expressions for Ansible

ExpressionDescription
{{ var }}Print a variable
{% for item in list %}Loop through a list
{% if condition %}Conditional logic
`{{ varupper }}`
{# This is a comment #}Jinja2 comment

13. Troubleshooting

  • Template syntax errors: If your template fails, Ansible will show the error. Check for missing {% ... %} or {{ ... }}.
  • Variable undefined: Use default filter to avoid errors: {{ myvar | default('defaultvalue') }}.

14. Further Reading


Summary:

  • Ansible templates use Jinja2 for dynamic configuration files.
  • You can use variables, loops, conditionals, and filters in .j2 files.
  • The template module copies the rendered file to the remote host.

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x