0

It seems there are two ways to make a file content dependent on a variable.

copy:
  path: /foo
  content: |
    {{ if myFoo }}
      ; lots of stuff
    {{ else }}
      ; lots of other stuff
    {{ end }}

Or I can do

copy:
  path: /foo
  content: |
      ; lots of stuff
when: myFoo

and

copy:
  path: /foo
  content: |
      ; lots of other stuff
when: not myFoo

What is the normal construct here? What's desired?

0

I had to rewrite the tasks.

- copy:
        dest: /tmp/ansiblefile.txt
        content: |
          {% if myFoo %}
            ; lots of stuff
          {% else %}
            ; lots of other stuff
          {% endif %}
- copy:
    dest: /foo
    content: |
      ; lots of stuff
  when: myFoo

- copy:
    dest: /foo
    content: |
      ; lots of stuff
  when: not myFoo

In my opinion, either is fine. However, I would use the latter approach since I find it easier not to deal with jinja templates; the documentation for jinja templates is not very detailed and easy to follow.

| improve this answer | |
  • 1
    The only potential issue with this latter example is a case where both conditional tests could be true. As you have indicated, 'not myFoo' and 'myFoo' should work fine, but how someone interprets and creates this could introduce problems. I always prefer a single 'if > then > else' in a single function, instead of separate functions that have no relationship to the state of the other. But that's just my $0.02. – 0xSheepdog Jan 22 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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