Templates in Ansible are a powerful feature that allows you to dynamically generate configuration files and other text files based on variables and logic defined in your playbooks. Templates are written in the Jinja2 templating language, which is a flexible and expressive language for creating dynamic content.

Key Concepts

  1. Jinja2 Templating Language: The language used to write templates in Ansible.
  2. Template Module: The Ansible module used to apply templates.
  3. Variables: Dynamic values that can be inserted into templates.
  4. Control Structures: Logic such as loops and conditionals that can be used within templates.

Using the Template Module

The template module is used to process a Jinja2 template and copy the rendered file to a remote host.

Basic Syntax

- name: Apply a template
  template:
    src: /path/to/template.j2
    dest: /path/to/destination/file

Example

Let's create a simple template and use it in a playbook.

Step 1: Create a Template File

Create a file named nginx.conf.j2 with the following content:

server {
    listen 80;
    server_name {{ server_name }};
    root {{ document_root }};
    
    location / {
        try_files $uri $uri/ =404;
    }
}

Step 2: Create a Playbook

Create a playbook named deploy_nginx.yml:

---
- name: Deploy Nginx configuration
  hosts: webservers
  vars:
    server_name: example.com
    document_root: /var/www/html
  tasks:
    - name: Apply Nginx configuration template
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify:
        - Restart Nginx

  handlers:
    - name: Restart Nginx
      service:
        name: nginx
        state: restarted

Explanation

  • Template File (nginx.conf.j2): This file contains placeholders ({{ server_name }} and {{ document_root }}) that will be replaced with actual values when the template is rendered.
  • Playbook (deploy_nginx.yml):
    • vars: Defines the variables server_name and document_root.
    • tasks: Uses the template module to apply the nginx.conf.j2 template and copy the rendered file to /etc/nginx/nginx.conf.
    • handlers: Defines a handler to restart Nginx whenever the configuration file is changed.

Practical Exercise

Exercise

  1. Create a Jinja2 template file named index.html.j2 with the following content:

    <!DOCTYPE html>
    <html>
    <head>
        <title>{{ title }}</title>
    </head>
    <body>
        <h1>{{ heading }}</h1>
        <p>{{ message }}</p>
    </body>
    </html>
    
  2. Create a playbook named deploy_website.yml that uses the template module to apply the index.html.j2 template and copy the rendered file to /var/www/html/index.html. Define the following variables in the playbook:

    • title: "Welcome to My Website"
    • heading: "Hello, World!"
    • message: "This is a dynamically generated web page."

Solution

Step 1: Create the Template File

Create a file named index.html.j2 with the following content:

<!DOCTYPE html>
<html>
<head>
    <title>{{ title }}</title>
</head>
<body>
    <h1>{{ heading }}</h1>
    <p>{{ message }}</p>
</body>
</html>

Step 2: Create the Playbook

Create a playbook named deploy_website.yml:

---
- name: Deploy website
  hosts: webservers
  vars:
    title: "Welcome to My Website"
    heading: "Hello, World!"
    message: "This is a dynamically generated web page."
  tasks:
    - name: Apply website template
      template:
        src: index.html.j2
        dest: /var/www/html/index.html

Explanation

  • Template File (index.html.j2): Contains placeholders ({{ title }}, {{ heading }}, and {{ message }}) that will be replaced with actual values when the template is rendered.
  • Playbook (deploy_website.yml):
    • vars: Defines the variables title, heading, and message.
    • tasks: Uses the template module to apply the index.html.j2 template and copy the rendered file to /var/www/html/index.html.

Common Mistakes and Tips

  • Undefined Variables: Ensure all variables used in the template are defined in the playbook.
  • File Permissions: Verify that the destination directory has the correct permissions to allow the template to be copied.
  • Syntax Errors: Check for syntax errors in the Jinja2 template, such as missing braces or incorrect variable names.

Conclusion

Templates in Ansible provide a flexible way to generate dynamic configuration files and other text files. By using the Jinja2 templating language, you can create templates that incorporate variables and logic, making your playbooks more powerful and adaptable. In the next section, we will explore Ansible Vault, which allows you to securely store sensitive data such as passwords and keys.

© Copyright 2024. All rights reserved