Ansible – Templates
Managing configurations of multiple servers and environments are one of the significant uses of Ansible. But these configuration files may vary for each remote servers or each cluster. But apart from some few parameters, all other settings will be same.
A template in Ansible is a file which contains all your configuration parameters, but the dynamic values are given as variables. During the playbook execution, depending on the conditions like which cluster you are using, the variables will be replaced with the relevant values.
The variables in a template file will be denoted by the double curly braces, ``. The template files will usually have the .j2 extension, which denotes the Jinja2 templating engine used.
A Basic Example
In the following task, I am using the template module on the example1.j2 file which will replace the default variables with values given in the playbook.
File: Playbook.yml
---
- hosts: all
vars:
variable1: 'Hello...!!!'
variable2: 'My first playbook using template'
tasks:
- name: Basic Template Example
template:
src: example1.j2
dest: /home/knoldus/Documents/Ansible/output.txt
File: example1.j2
No effects on this line
File: output.txt
Hello...!!!
No effects on this line
My first playbook using template
As you can see, both variables in the example1.j2 are replaced by their values.
A Basic Example of Ansible Template Module
At the bare minimum, you need to have two parameters when using the Ansible module.
src: the source of the template file. This can be relative or absolute path.
dest: the destination path on the remote server
These are some of the other parameters which we can use to change some default behavior of template module :
-
force – If the destination file already exists, then this parameter decides whether it should be replaced or not. By default, the value is ‘yes’.
-
mode – If you want to set the permissions for the destination file explicitly, then you can use this parameter.
-
backup – If you want a backup file to be created in the destination directory, you should set the value of the backup parameter to ‘yes’. By default, the value is ‘no’. The backup file will be created every time there is a change in the destination directory.
-
group – Name of the group that should own the file/directory. It is similar to executing chown for a file in Linux systems.
Using lists in Ansible templates
In the next example, I’ll be using the template module to print all the items present in a list using the for the loop.
# File: Playbook.yml
- hosts: all
vars:
list1: ['Apple','Banana','Cat', 'Dog']
tasks:
- name: Template Loop example.
- template:
src: example2.j2
dest: /home/knoldus/Documents/Ansible/output.txt
# File: example2.j2
Example of template module loop with a list.
# File: output.txt
Example of template module loop with a list.
Apple
Banana
Cat
Dog
Working With Multiple Files in Ansible
We can use the with_items parameter on a dictionary to render multiple files. For example, if we want to render three templates each with different source and destination, with_items parameter can be put to use. An illustration is given below.
- hosts: all
tasks:
- name: Template with_items example.
template:
src: ""
dest: ""
with_items:
- {src: 'example.j2',dest: '/home/knoldus/Documents/Ansible/output.txt'}
- {src: 'example1.j2',dest: '/home/knoldus/Documents/Ansible/output1.txt'}
- {src: 'example2.j2',dest: '/home/knoldus/Documents/Ansible/output2.txt'}