It puts the files onto a remote system using jinja2 templating language. Ansible template Module Example : Template a single file Step...
It puts the files onto a remote system using jinja2 templating language.
Ansible template Module
Example: Template a single file
Step 1: Create a file called my_app.conf.j2 and add below content
env = {{ env }}
local_id = {{ ansible_host }}
local_OSFamily = {{ ansible_facts['os_family'] }}
local_OS = {{ ansible_distribution }}
Step 2: Create a ansible play book lets call it template.yaml
---
- name: Parsing Jinja2 templates
hosts: "*"
vars:
env: staging
tasks:
- name: template file onto remote hosts
template:
src: my_app.conf.j2
dest: /tmp/myconfigfile
Example Set Permission for parsed jinja file
---
- name: Parsing Jinja2 templates
hosts: "*"
vars:
env: staging
tasks:
- name: template file onto remote hosts
template:
src: my_app.conf.j2
dest: /tmp/myconfigfile
- name: change the permission for the parsed jinja file
file:
path: /tmp/myconfigfile
owner: raman
mode: '0600'
Example Template Multiple files.
Create one more .j2 file let's say my_host.conf.j2
and write following content
OS = {{ ansible_distribution }}
Create a template.yaml file
---
- name: Parsing Jinja2 templates
hosts: "*"
vars:
env: staging
tasks:
- name: template file onto remote hosts
template:
src: '{{ item.src }}'
dest: '{{ item.dest }}'
with_items:
- { src: my_app.conf.j2, dest: /tmp/myconfigfile }
- { src: my_host.conf.j2, dest: /tmp/myhostconfigfile}
- name: change the permission for the parsed jinja file
file:
path: /tmp/myconfigfile
owner: raman
mode: '0600'
COMMENTS