I have in a playbook a blockinfile which should be present on not depending on the value of a previously set variable. The bruteforce solution is
- name: shorewall rules
blockinfile:
dest: /etc/shorewall/rules
state: present
block: |
# settings for machine {{ machine }}
# outgoing internet
ACCEPT {{ machine }} int
# more here
when: active == "y"
- name: shorewall rules
blockinfile:
dest: /etc/shorewall/rules
state: absent
block: |
# settings for machine {{ machine }}
# outgoing internet
ACCEPT {{ machine }} int
# more here
when: active == "n"
Is there a way to set a condition in state? Something along the lines of
state: present if active == "y" else absent
You can do it like this:
state: "{{ 'present' if active == 'y' else 'absent' }}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With