Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use template module with different set of variables?

My use case is the following :

I have a template file, and I would like to create 2 different files from that template, with the variables being filled by a different set of variables for each file.

For example, lets say I want to template the file containing the line:

mkdir -p {{myTemplateVariable}} 

I would like to find a proper way to get this variable filled by "File1" and "File2". Something like :

- name: template test 1   template:          src=myTemplateFile         dest=result1   - name: template test 2   template:          src=myTemplateFile         dest=result2 

where I could specify for the first templating that the variable to use is a = "File1" and for the second, b = "File2".

like image 358
Kestemont Max Avatar asked Jun 30 '15 15:06

Kestemont Max


People also ask

How are parameters used in template files in Ansible?

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.

How do I use Ansible template module?

You can use Ansible facts, variables, and user-defined variables in your Jinja2 templates. On your Jinja2 template, you can print the value of a variable using the {{ variableName }} syntax. If the variable is an object, you can print individual object properties using the {{ objectVariable. propertyName }} syntax.

What does Ansible template module do?

Ansible template module helps to template a file out to a remote server. Simply put, at runtime ansible template module updates the jinja2 interpolation syntax variables with actual values and copy the template file to the remote server with the specified name.

What is the difference between template and copy module in Ansible?

While very similar, template serves an extra function. copy takes a file from host, "as-is", and copies it to the remote destination.


2 Answers

With Ansible 2.x you can use vars: with tasks.

Template test.j2:

mkdir -p {{myTemplateVariable}} 

Playbook:

- template: src=test.j2 dest=/tmp/File1   vars:     myTemplateVariable: myDirName  - template: src=test.j2 dest=/tmp/File2   vars:     myTemplateVariable: myOtherDir 

This will pass different myTemplateVariable values into test.j2.

like image 114
Konstantin Suvorov Avatar answered Sep 22 '22 17:09

Konstantin Suvorov


For Ansible 2.x:

- name: template test   template:      src: myTemplateFile     dest: result1   vars:     myTemplateVariable: File1  - name: template test   template:      src: myTemplateFile     dest: result2   vars:     myTemplateVariable: File2 

For Ansible 1.x:

Unfortunately the template module does not support passing variables to it, which can be used inside the template. There was a feature request but it was rejected.

I can think of two workarounds:

1. Include

The include statement supports passing variables. So you could have your template task inside an extra file and include it twice with appropriate parameters:

my_include.yml:

- name: template test   template:          src=myTemplateFile         dest=destination 

main.yml:

- include: my_include.yml destination=result1 myTemplateVariable=File1  - include: my_include.yml destination=result2 myTemplateVariable=File2 

2. Re-define myTemplateVariable

Another way would be to simply re-define myTemplateVariable right before every template task.

- set_fact:      myTemplateVariable: File1  - name: template test 1   template:          src=myTemplateFile         dest=result1  - set_fact:      myTemplateVariable: File2  - name: template test 2   template:          src=myTemplateFile         dest=result2 
like image 45
udondan Avatar answered Sep 24 '22 17:09

udondan