1

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.

Volodymyr Melnyk
  • 537
  • 5
  • 18

1 Answers1

1

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 }}
Vladimir Botka
  • 3,791
  • 6
  • 17
  • Thank you, Vladimir! I had to update the problem description, as I didn't explained it in the original post: "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)" – Volodymyr Melnyk Nov 25 '21 at 11:10
  • The explanation is fine. I've simplified the example. Is this what you're looking for? – Vladimir Botka Nov 25 '21 at 16:19