ansible.builtin.template: Deploy Jinja2 Templates with Ansible (Guide)
By Luca Berton · Published 2024-01-01 · Category: installation
Learn to create and use templates in Ansible Playbooks, including an example of deploying an HTML placeholder using Jinja2 templates in your Nginx webserver.

How to apply a file template to the target host with Ansible? This is extremely useful for service configuration files, placeholder web pages, reports, and so much more use cases. I'm going to show you a live Playbook with some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible applies a file template
• ansible.builtin.template • Template a file out to a target host • ansible_managed, template_host, template_uid, template_path, template_fullpath, template_destpath, and template_run_dateToday we're talking about Ansible module template.
The full name is ansible.builtin.template, it's part of ansible-core and is included in all Ansible installations.
It templates a file out to a target host. Templates are processed by the Jinja2 templating language.
Also you could use also some special variables in your templates: ansible_managed, template_host, template_uid, template_path, template_fullpath, template_destpath, and template_run_date.
It supports a large variety of Operating Systems.
For basic text formatting, use the Ansible ansible.builtin.copy module or for empty file Ansible ansible.builtin.file module.
For Windows, use the ansible.windows.win_template module instead.
See also: Ansible Concatenate Files: Merge Multiple Files in Order (Guide)
Parameters
•src path - template ("templates/" dir)
• dest path - target location
• validate string - validation command before ("%s")
• backup boolean - no/yes
• mode/owner/group - permission
• setype/seuser/selevel - SELinux
Let me highlight the most useful parameters for template module. The only required parameters are "src" and "dest". The "src" parameter specifies the template file name. Templates usually are stored under "templates" directories with ".j2" file extension. The "dest" parameter specifies the path where to render the template on the remote machine. The "validate" parameters allow you to specify the validation command to run before copying it into place. It's very useful with configuration files for services. Please note that the special escape sequence "%s" is going to be expanded by Ansible with the destination path. If the "backup" parameter is enabled Ansible creates a backup file including the timestamp information before copying it to the destination. Let me also highlight that we could also specify the permissions and SELinux properties.
## Playbook
Apply a file template with Ansible Playbook
code
• template.yml---
- name: template module Playbook
hosts: all
become: true
vars:
page_title: "Placeholder"
page_description: |
This is my placeholder page example.
Multiline is possible ;-)
tasks:
- name: install Nginx
ansible.builtin.apt:
name: nginx
state: latest
- name: apply page template
ansible.builtin.template:
src: templates/placeholder.html.j2
dest: /var/www/html/index.html
• templates/placeholder.html.j2
<html>
<head>
<title>{{ page_title }}</title>
</head>
<body>
<h1>{{ page_title }}</h1>
<p>{{ page_description }}</p>
</body>
</html>
execution
$ ansible-playbook -i virtualmachines/ubuntu/inventory apply\ template/template.yml
PLAY [template module Playbook] ***********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [ubuntu.example.com]
TASK [install Nginx] ******************************************************************************
changed: [ubuntu.example.com]
TASK [apply page template] ************************************************************************
changed: [ubuntu.example.com]
PLAY RECAP ****************************************************************************************
ubuntu.example.com : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
before execution
$ ssh devops@ubuntu.example.com
devops@ubuntu:~ $ sudo su
root@ubuntu:/home/devops# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
root@ubuntu:/home/devops# apt list installed nginx -a
Listing... Done
nginx/focal-updates,focal-updates,focal-security,focal-security 1.18.0-0ubuntu1.2 all
nginx/focal,focal 1.17.10-0ubuntu1 all
root@ubuntu:/home/devops# ls -al /var/
total 52
drwxr-xr-x 13 root root 4096 Oct 27 15:38 .
drwxr-xr-x 19 root root 4096 Oct 27 16:05 ..
drwxr-xr-x 2 root root 4096 Apr 15 2020 backups
drwxr-xr-x 12 root root 4096 Oct 27 15:43 cache
drwxrwxrwt 2 root root 4096 Oct 27 15:38 crash
drwxr-xr-x 41 root root 4096 Oct 27 15:49 lib
drwxrwsr-x 2 root staff 4096 Apr 15 2020 local
lrwxrwxrwx 1 root root 9 Oct 27 15:35 lock -> /run/lock
drwxrwxr-x 10 root syslog 4096 Nov 22 11:56 log
drwxrwsr-x 2 root mail 4096 Jul 31 2020 mail
drwxr-xr-x 2 root root 4096 Jul 31 2020 opt
lrwxrwxrwx 1 root root 4 Oct 27 15:35 run -> /run
drwxr-xr-x 2 root root 4096 Jul 10 2020 snap
drwxr-xr-x 4 root root 4096 Oct 27 15:35 spool
drwxrwxrwt 5 root root 4096 Nov 22 11:56 tmp
root@ubuntu:/home/devops# ls -al /var/www
ls: cannot access '/var/www': No such file or directory
after execution
$ ssh devops@ubuntu.example.com
devops@ubuntu:~ $ sudo su
root@ubuntu:/home/devops# apt list installed nginx -a
Listing... Done
nginx/focal-updates,focal-updates,focal-security,focal-security,now 1.18.0-0ubuntu1.2 all [installed]
nginx/focal,focal 1.17.10-0ubuntu1 all
root@ubuntu:/home/devops# ls -al /var/www/
total 12
drwxr-xr-x 3 root root 4096 Nov 22 12:06 .
drwxr-xr-x 14 root root 4096 Nov 22 12:06 ..
drwxr-xr-x 2 root root 4096 Nov 22 12:06 html
root@ubuntu:/home/devops# ls -al /var/www/html/
total 16
drwxr-xr-x 2 root root 4096 Nov 22 12:06 .
drwxr-xr-x 3 root root 4096 Nov 22 12:06 ..
-rw-r--r-- 1 root root 173 Nov 22 12:06 index.html
-rw-r--r-- 1 root root 612 Nov 22 12:06 index.nginx-debian.html
root@ubuntu:/home/devops# less /var/www/html/index.html
root@ubuntu:/home/devops# cat /var/www/html/index.html
<html>
<head>
<title>Placeholder</title>
</head>
<body>
<h1>Placeholder</h1>
<p>This is my placeholder page example.
Multiline is possible ;-)
</p>
</body>
Conclusion
Now you know how to apply a file template with Ansible.See also: Ansible hostname Module: Set System Hostname on Linux (Guide)
Related Articles
• whitespace control in Jinja2 for Ansible • Ansible Multiline Strings Guide • Ansible Windows playbook patterns • sudo and become in Ansible playbooks • static and dynamic Ansible inventoryCategory: installation
Watch the video: ansible.builtin.template: Deploy Jinja2 Templates with Ansible (Guide) — Video Tutorial