Ansible: filling a template that contains immutable interpolations
I need to make a playbook that will take a template file, interpolate some variables and put the resulting file to another directory.
The first part of the problem is that the resulting file will be used as another Ansible playbook, so the source file contains other interpolations and these interpolations must be preserved as they are. As an example, let's assume that the source file contains something like that:
key1: {{ value1 }}
key2: {{ value2 }}
The value of key1
must be interpolated by my playbook (it shall become the values of the value1
variable), but at the same time value2
needs to be kept just as it is (it shall be {{ value2 }}
.
The second part of the problem is that I can't just modify the source file and add backslashes before the curly brackets, because this file is being updated by other processes. Moreover, as the template file is being updated from time to time, I can't predict what variables needs to be skipped after the next update. I only know that I need to interpolate value1
, but I don't know what are the other variables' names (today there are value2
and value3
, tomorrow the developers renamed value2
to value2_deprecated
and added value3
somewhere).
Can I ask Ansible that I need to interpolate value1
only?
Thank you.
Q: "The template contains interpolations and these interpolations must be preserved as they are."
A: Declare default variables
lbr: '{{ "{{" }}'
rbr: '{{ "}}" }}'
value1: '{{ lbr }} value1 {{ rbr }}'
value2: '{{ lbr }} value2 {{ rbr }}'
and create the template
shell> cat template.yml.j2
key1: {{ value1 }}
key2: {{ value2 }}
Q: "I need to interpolate value1 only."
A: Override the default value, e.g.
- template:
src: template.yml.j2
dest: playbook.yml
vars:
value1: value1
gives
shell> cat playbook.yml
key1: value1
key2: {{ value2 }}