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
- Jinja2 Templating Language: The language used to write templates in Ansible.
- Template Module: The Ansible module used to apply templates.
- Variables: Dynamic values that can be inserted into templates.
- 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
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
anddocument_root
. - tasks: Uses the
template
module to apply thenginx.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.
- vars: Defines the variables
Practical Exercise
Exercise
-
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>
-
Create a playbook named
deploy_website.yml
that uses thetemplate
module to apply theindex.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
, andmessage
. - tasks: Uses the
template
module to apply theindex.html.j2
template and copy the rendered file to/var/www/html/index.html
.
- vars: Defines the variables
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.
Ansible: From Beginner to Advanced
Module 1: Introduction to Ansible
Module 2: Ansible Basics
Module 3: Playbooks
- Introduction to Playbooks
- Writing Your First Playbook
- Playbook Structure
- Variables and Facts
- Conditionals and Loops
Module 4: Roles
Module 5: Advanced Playbook Techniques
Module 6: Ansible Galaxy
Module 7: Ansible Tower
- Introduction to Ansible Tower
- Installing Ansible Tower
- Using Ansible Tower
- Managing Projects and Inventories