All exams← All Red Hat Certified Engineer (RHCE) exam (EX294) notes

Creating and Using Templates: Red Hat Certified Engineer (RHCE) exam (EX294) revision notes

Revision notes

## Creating and Using Templates in Ansible

Ansible templates are a powerful feature for creating customized configuration files dynamically on managed hosts. They allow you to embed variables and control structures directly into static file content, which is then rendered on the target system. Ansible uses the Jinja2 templating language for this purpose.

Why Use Templates?

Templates are essential for:

  • Dynamic Configuration: Generating configuration files (e.g., Apache virtual hosts, Nginx server blocks, user profiles) that vary based on host-specific data, group variables, or facts.
  • Reduced Duplication: Maintaining a single template file and populating it with different data, rather than having multiple slightly different static configuration files.
  • Idempotency: The `template` module, like other Ansible modules, ensures that the target file is only updated if its content changes, maintaining idempotency.

The `template` Module

The core of using templates in Ansible is the `template` module.

It takes a source template file (on the control node) and renders it on the managed node, substituting Jinja2 expressions with actual values.

Key parameters for the `template` module:

  • `- src`: Specifies the path to the template file on the Ansible control node. This path is typically relative to the playbook or defined in `ansible.cfg`. A common convention is to place templates in a `templates/` subdirectory relative to the playbook.
  • `- dest`: Specifies the absolute path where the rendered file will be created or updated on the managed node.
  • `- owner`, `- group`, `- mode`: Optional parameters to set the ownership, group, and file permissions of the destination file on the managed node, similar to the `copy` module.

Jinja2 Syntax Basics

Understanding basic Jinja2 syntax is crucial for writing effective templates:

  • Expressions and Variables: Use `{{ variable_name }}` to insert the value of an Ansible variable. For example, `ServerName {{ ansible_hostname }}`.
  • Statements and Control Structures: Use `{% statement %}` for logic like loops (`for`) and conditionals (`if/else`).
  • Example `for` loop:

```jinja2

{% for user in users %}

User {{ user.name }} with UID {{ user.uid }}

{% endfor %}

```

  • Example `if` statement:

```jinja2

{% if enable_feature %}

Feature is enabled.

{% else %}

Feature is disabled.

{% endif %}

```

  • Comments: Use `{# comment #}` for comments within the template that will not appear in the rendered output.

Variables used within templates can originate from various Ansible sources, including inventory variables, `group_vars`, `host_vars`, `vars` sections in playbooks, `set_fact` tasks, and Ansible facts.

Key facts

Flashcards

What templating language does Ansible use for its templates?
Jinja2
tap to reveal
Which Ansible module is used to deploy a template file to a managed node?
`template` module
tap to reveal
In the `template` module, what does the `src` parameter specify?
The path to the template file on the Ansible control node.
tap to reveal
In the `template` module, what does the `dest` parameter specify?
The absolute path where the rendered file will be created on the managed node.
tap to reveal
What is the Jinja2 syntax for embedding an Ansible variable within a template?
`{{ variable_name }}`
tap to reveal
What is the Jinja2 syntax for control structures like loops or conditionals within a template?
`{% statement %}`
tap to reveal
Where are template files typically stored relative to an Ansible playbook?
In a `templates/` subdirectory.
tap to reveal
Name two optional parameters for the `template` module to manage the destination file's permissions.
`owner`, `group`, `mode` (any two)
tap to reveal
Practice questions

Creating and Using Templates: 5 practice questions

Q1. Which Ansible module is primarily used to process a Jinja2 template file on the control node and deploy the resulting file to a managed node?
  • A.  ansible.builtin.fetch
  • B.  ansible.builtin.template
  • C.  ansible.builtin.copy
  • D.  ansible.builtin.file
Show answer & explanation
✓ Answer: B. ansible.builtin.template
The `ansible.builtin.template` module is specifically designed to render Jinja2 templates and copy the resulting file to the target host.
Q2. Inside a Jinja2 template file, what is the correct syntax to insert the value of an Ansible variable named `http_port`?
  • A.  ((http_port))
  • B.  ${http_port}
  • C.  {{ http_port }}
  • D.  <% http_port %>
Show answer & explanation
✓ Answer: C. {{ http_port }}
Jinja2 uses double curly braces `{{ ... }}` to enclose expressions and variables that should be printed to the output.
Q3. A template needs to use the variable `timeout`, but this variable might not always be defined. Which Jinja2 expression ensures that if `timeout` is undefined, the value `30` is used instead, preventing an error?
  • A.  {{ timeout ? 30 }}
  • B.  {{ timeout or 30 }}
  • C.  {{ default(timeout, 30) }}
  • D.  {{ timeout | default(30) }}
Show answer & explanation
✓ Answer: D. {{ timeout | default(30) }}
The `default` filter is the standard and safest way to provide a fallback value for a variable that may be undefined, avoiding playbook failures.
Q4. Given a list variable `users: [alice, bob]`, which Jinja2 snippet correctly iterates over the list to create user entries in a configuration file?
  • A.  {% for user in users %} user = {{ user }} {% endfor %}
  • B.  {{ for user in users }} user = {{ user }} {{ endfor }}
  • C.  {{ loop user over users }} user = {{ user }} {{ endloop }}
  • D.  <loop user in users> user = {{ user }} </loop>
Show answer & explanation
✓ Answer: A. {% for user in users %} user = {{ user }} {% endfor %}
Jinja2 control structures, like for loops, are enclosed in `{% ... %}` delimiters. The correct syntax is `{% for item in list %}...{% endfor %}`.
Q5. What is the correct syntax for adding a comment inside a Jinja2 template that will NOT appear in the final rendered file?
  • A.  {# This is a comment #}
  • B.  <!-- This is a comment -->
  • C.  # This is a comment
  • D.  /* This is a comment */
Show answer & explanation
✓ Answer: A. {# This is a comment #}
Jinja2 uses `{# ... #}` as the delimiter for comments. These comments are processed by the template engine and do not appear in the final output.
Test yourself
See the Red Hat Certified Engineer (RHCE) exam (EX294) page

More Red Hat Certified Engineer (RHCE) exam (EX294) topics