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.

Kevin Burke
  • 61,194
  • 76
  • 188
  • 305
  • Possible duplicate of [How to use template module with different set of variables?](http://stackoverflow.com/questions/31142369/how-to-use-template-module-with-different-set-of-variables) – Kevin Burke Jul 28 '16 at 18:00

1 Answers1

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
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193