Ansible Template Module Examples-DecodingDevOps

Ansible Template Module Examples-DecpdongDevOps

Ansible Template module is little Different form COPY module. Instead of copy file from control machine to destination machine, it will supply the variable values first to the file, after that it will copy the file to destination. So using ansible template module we can copy dynamic files to the target server. Whenever you have dynamic values, ansible has a templating system. That templating system is Jinja. Jinja templates are the files with extension ‘.j2’. Most of the time we use them to replace configuration files or place some documents on the remote server. We can also perform conditional statements, loops, filters for transforming the data using ansible templates. in this post i will explain ansible template module with different examples.

Ansible Template Module Examples

Lets try to solve the sample example

Create a decodingdevops.j2 file with following content

$ cat decodingdevops.j2
 

Os family is {{ ansible_os_family }}
Os distribution is {{ ansible_distribution }}
version is {{ ansible_distribution_version }}

above you can see that we have a decodingdevops.j2 file is having three variables those are ansible_os_family, ansible_distribution, and ansible_distribution_version. Here we we are copying this file from local to target(destination) server and when we are copying the file we can pass values to these variables using ansible template module.

so in the below ansible playbook i will give the values of above three variables and using template module i will copy the file to destination or target server.

$ cat template.yml
 
---
- hosts: localhost
  vars:
    ansible_os_family: 'Linux'
    ansible_distribution: 'Ubuntu'
    ansible_distribution_version: '16.04'
  tasks:
  - name: copy the readme
    template:
      src: decodingdevops.j2
      dest: "home/ubuntu/decodingdevops.txt"

Run this playbook and check the results ansible-playbook template.yml 

$ cat decodingdevops.txt
 

Os family is linux 
Os distribution is ubuntu 
version is 16.04

we can see, Three variables in the decodingdevops.j2 are replaced by their values.

src: The source of the template file. This can be a relative or absolute path.
dest: The destination path on the remote server

Parameters in Templates:

  • Force :  If the destination file already exists, then this parameter decides whether it should be replaced or not. By default, the value is ‘yes’.  yes will replace the destination file with source file. if you mention ‘no’ the destination file will be not replaced if the destination file already exists.
  • Mode: If you want to set the permissions for the destination file, we use can use this parameter.
  • Backup: If you want a backup file to be created in the destination directory, we use can use this parameter. you should set the value of the backup parameter to ‘yes’. By default, the value is ‘no’.
  • Group: Name of the group that will assigned for the  file/directory. It is similar like chown command in Linux.

Use Lists in Ansible Templates:

  • print all the items present in a list
  • Create a list.j2 file with following content
$ cat list.j2
 
List Example.
{% for item in list %}
  {{ item }}
{% endfor %}

Write a simple playbook with following content list.yml

$ cat list.yml
 
- hosts: localhost
  vars:
    list: ['list1','list2','list3', 'list4']
  tasks:
    - name: list example.
    - template:
        src: list.j2
        dest: /home/ubuntu/list.txt

Run this playbook and check the results ansible-playbook list.yml 

$ cat list.txt
 
List Example.
list1
list2
list3
list4

With_Items in Ansible Templates:

  • if we want to render two templates each with different source and destination, with_items parameter will be helpful.
  • Check the below example for your reference
  • create a playbook with_item.yml
  • Run the playbook and check the results ansible-playbook with_item.yml
$ cat with_item.yml
 
- hosts: localhost
  tasks:
    - name: With_Item example.
      template:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
      with_items:
        - {src: 'list.j2',dest: '/home/ubuntu/list1.txt'}
        - {src: 'list.j2',dest: '/home/ubuntu/list2.txt'}

Finally it will copy the files in two different locations in destination server.