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:


<!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:


--- - 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
OutputBECOME password:   PLAY [all] **********************************************************************************************  TASK [Gathering Facts] ********************************************************************************** ok: []  TASK [Install Nginx] ************************************************************************************ changed: []  TASK [Apply Page Template] ****************************************************************************** changed: []  TASK [Allow all access to tcp port 80] ****************************************************************** changed: []  PLAY RECAP **********************************************************************************************                : 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.