0

I would like to write the same Ansible template to two different files, one with a value in the file set to True, and the other with a value set to False.

What's the best way to do this? My instinct was to try and pass a value in the template: directive. However, it seems like this is frowned upon.

One way would be to have two different jinja files with almost entirely the same contents; one has the value set to True and the other False.

Another way would be to define a variable, write one template, then use set_fact to change the variable's value, then write the second file. This also seems a little cumbersome.

Another would be to have the template detect what filename it's being rendered as, somehow? And branch in the template based on that.

I must be missing something obvious.

1

2 Answers 2

1

You can use a loop like so:

- Name: Name
  template:
    src: template
    dest: "{{item.name}}"
  loop:
    - { name: one.txt, value: one }
    - { name: two.txt, value: two }

And the template will be like so:

KEY={{ item.value }}
New contributor
Cooper Brotherton is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0

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

---
- hosts: localhost
  tasks:
    - template: src=my_template.j2 dest=out1.txt
      vars:
        name: John
    - template: src=my_template.j2 dest=out2.txt
      vars:
        name: Jane

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.