# Either/or tasks

**URL:** <https://forum.ansible.com/t/either-or-tasks/17089>\
**Category:** Ansible Project\
**Created:** [December 17, 2014, 11:57am UTC](https://forum.ansible.com/t/either-or-tasks/17089 "2014-12-17T11:57:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Warbrick](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@Jon\_Warbrick](https://forum.ansible.com/u/Jon_Warbrick)\
**Post date:** [December 17, 2014, 11:57am UTC](https://forum.ansible.com/t/either-or-tasks/17089/1 "2014-12-17T11:57:37Z")

</div>

Apologies if this is a FAQ, but I haven't found been able to find it anywhere.

I keep wanting a pattern where something happens if a variable is true and something positively doesn't happen if the variable is false (or unset). For example having a service running or not running. Is there any way to achieve this other than having pairs of tasks with inverted when clauses?

e.g:

- name: be sure openais is running and enabled  
&nbsp;&nbsp;&nbsp;service: name=openais state=running enabled=yes  
&nbsp;&nbsp;&nbsp;when: enable\_service\_address is defined and enable\_service\_address

- name: be sure openais is NOT running and NOT enabled  
&nbsp;&nbsp;&nbsp;service: name=openais state=stopped enabled=no  
&nbsp;&nbsp;&nbsp;when: enable\_service\_address is not defined or not enable\_service\_address

Or, as an other example, creating or removing a marker file (which seems to be further complicated by needing 'copy' for create and 'file' for remove):

- name: Set standalone marker file  
&nbsp;&nbsp;&nbsp;copy: content='' dest=/var/role-standalone  
&nbsp;&nbsp;&nbsp;when: '"standalone" in group\_names'

- name: Unset standalone marker file  
&nbsp;&nbsp;&nbsp;file: path=/var/role-standalone state=absent  
&nbsp;&nbsp;&nbsp;when: '"standalone" not in group\_names'

Jon.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 17, 2014, 3:11pm UTC](https://forum.ansible.com/t/either-or-tasks/17089/2 "2014-12-17T15:11:07Z")

</div>

not currently, what you have is what works.

---

<div class="post-metadata">

**Author:** ![Jon\_Warbrick](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@Jon\_Warbrick](https://forum.ansible.com/u/Jon_Warbrick)\
**Post date:** [January 5, 2015, 11:27am UTC](https://forum.ansible.com/t/either-or-tasks/17089/3 "2015-01-05T11:27:50Z")

</div>

Ta. At least I wasn't missing something obvious...

Jon.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [January 5, 2015, 2:13pm UTC](https://forum.ansible.com/t/either-or-tasks/17089/4 "2015-01-05T14:13:28Z")

</div>

Alternatively, something like this:

vars:  
openais\_enabled: "{% if enable\_service\_address is defined and enable\_service\_address %}yes{% else %}no{% endif %}

openais\_running: "{% if enable\_service\_address is defined and enable\_service\_address %}running{% else %}stopped{% endif %}

and in tasks:

- name: be sure openais is running and enabled  
service: name=openais state={{ openais\_running }} enabled={{ openais\_enabled }}

I’m not sure I really like that, as the conditional is a bit thick, though.

More so just sharing for completeness.

---

<div class="post-metadata">

**Author:** ![Christian\_Thiemann](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@Christian\_Thiemann](https://forum.ansible.com/u/Christian_Thiemann)\
**Post date:** [January 5, 2015, 6:34pm UTC](https://forum.ansible.com/t/either-or-tasks/17089/5 "2015-01-05T18:34:27Z")

</div>

I like to use the ternary operator in cases like these (not as “thick” as the conditionals):

vars:  
openais\_enabled: “{{ ‘yes’ if enable\_service\_address is defined and enable\_service\_address else ‘no’ }}”

openais\_running: “{{ ‘running’ if enable\_service\_address is defined and enable\_service\_address else ‘stopped’ }}”

Or, to avoid variables and have the logic all in the task definition:

- name: be sure openais is running and enabled  
service:  
name: openais

state: “{{ ‘running’ if enable\_service\_address is defined and enable\_service\_address else ‘stopped’ }}”

enabled: “{{ ‘yes’ if enable\_service\_address is defined and enable\_service\_address else ‘no’ }}”

Cheers,  
Christian
