How To Create and Use Templates in Ansible Playbooks

Templates allow you to create new files on the nodes using predefined models based on the Jinja2 templating system. Ansible templates are typically saved as .tpl files and support the use of variables, loops, and conditional expressions.

Templates are commonly used to configure services based on variable values that can be set up on the playbook itself, in included variable files, or obtained via facts. This enables you to create more versatile setups that adapt behavior based on dynamic information.

To try it out this feature with a practical example, create a new directory to hold non-playbook files inside your ansible-practice directory:

  • mkdir ~/ansible-practice/files

Next, create a new template file for an HTML landing page. Later on, we’ll set up a playbook which will configure your remote nodes to serve the landing page with Nginx:

  • nano ~/ansible-practice/files/landing-page.html.j2

Add the following content to the template file:

~/ansible-practice/files/landing-page.html.j2
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>{{ page_title }}</title>
  <meta name="description" content="Created with Ansible">
</head>
<body>
    <h1>{{ page_title }}</h1>
    <p>{{ page_description }}</p>
</body>
</html>

Save and close the file when you’re done.

This template uses two variables that must be provided whenever the template is applied in a playbook: page_title and page_description.

The following playbook sets up the required variables, installs Nginx, and then applies the specified template to replace the existing, default Nginx landing page located at /var/www/html/index.nginx-debian.html. The last task uses the ufw module to enable tcp access on port 80, in case you have your firewall enabled as recommended in our initial server setup guide.

Create a new file called playbook-11.yml in your ansible-practice directory:

  • nano ~/ansible-practice/playbook-11.yml

Add the following content to the new playbook file:

~/ansible-practice/playbook-11.yml
---
- hosts: all
  become: yes
  vars:
    page_title: My Landing Page
    page_description: This is my landing page description.
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: latest

    - name: Apply Page Template
      template:
        src: files/landing-page.html.j2
        dest: /var/www/html/index.nginx-debian.html

    - name: Allow all access to tcp port 80
      ufw:
        rule: allow
        port: '80'
        proto: tcp    

Remember to provide the -K option if you run this playbook, since it requires sudo permissions:

  • ansible-playbook -i inventory playbook-11.yml -u sammy -K
Output
BECOME password: PLAY [all] ********************************************************************************************** TASK [Gathering Facts] ********************************************************************************** ok: [203.0.113.10] TASK [Install Nginx] ************************************************************************************ changed: [203.0.113.10] TASK [Apply Page Template] ****************************************************************************** changed: [203.0.113.10] TASK [Allow all access to tcp port 80] ****************************************************************** changed: [203.0.113.10] PLAY RECAP ********************************************************************************************** 203.0.113.10 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

When the play has finished, you can access the web server’s public IP address from your browser. You’ll see a page like this:

Screenshot showing custom landing page

That means your playbook worked as expected, and the default Nginx page was replaced by the template you have created.

Originally posted on DigitalOcean Community Tutorials
Author: Erika Heidi

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *