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".
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.
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.
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.
While very similar, template serves an extra function. copy takes a file from host, "as-is", and copies it to the remote destination.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With