Ansible Pilot

Apply a file template - Ansible module template - HTML placeholder

Apply a file template - Ansible module template

How to create and use templates in Ansible Playbooks. An included example is how to deploy an HTML placeholder using the Jinga2 template 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 demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible applies a file template

Today 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.

Parameters

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.

demo

Apply a file template with Ansible Playbook

---
- name: template module demo
  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

<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 demo] ***********************************************************************

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 [email protected]
[email protected]:~ $ sudo su
[email protected]:/home/devops# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
[email protected]:/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

[email protected]:/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
[email protected]:/home/devops# ls -al /var/www
ls: cannot access '/var/www': No such file or directory

after execution

$ ssh [email protected]
[email protected]:~ $ sudo su
[email protected]:/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

[email protected]:/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
[email protected]:/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
[email protected]:/home/devops# less /var/www/html/index.html
[email protected]:/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>

code with ❤️ in GitHub

Recap

Now you know how to apply a file template with Ansible. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

Donate

Want to keep this project going? Please donate

Follow me

Subscribe not to miss any new releases

November 23, 2021