Score:1

Ansible: filling a template that contains immutable interpolations

au flag

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.

Score:1
br flag

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 }}
au flag
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)"
br flag
The explanation is fine. I've simplified the example. Is this what you're looking for?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.