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.

ansible.builtin.template renders a Jinja2 template file on the Ansible controller and writes the result to a remote host. Variable substitution, loops, and conditionals run at render time, making it the right choice for any configuration file that differs between hosts or environments — such as nginx configs, systemd unit files, or application settings.
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_date
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
srcpath - template ("templates/" dir)destpath - target locationvalidatestring - validation command before ("%s")backupboolean - no/yesmode/owner/group- permissionsetype/seuser/selevel- SELinux
## 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 directoryafter 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
Category: installation
Watch the video: ansible.builtin.template: Deploy Jinja2 Templates with Ansible (Guide) — Video Tutorial