# What is the proper way to check multiple conditions before running a task?

**URL:** <https://forum.ansible.com/t/what-is-the-proper-way-to-check-multiple-conditions-before-running-a-task/13734>\
**Category:** Ansible Project\
**Created:** [October 21, 2013, 5:51pm UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-multiple-conditions-before-running-a-task/13734 "2013-10-21T17:51:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Reagan](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@David\_Reagan](https://forum.ansible.com/u/David_Reagan)\
**Post date:** [October 21, 2013, 5:51pm UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-multiple-conditions-before-running-a-task/13734/1 "2013-10-21T17:51:13Z")

</div>

I have this task:

> - name: ensure nfs mountpoints exist  
> file: path={{ item[1].path }} state=directory group={{ item[1].group }} owner={{ item[1].owner }} mode={{ item[1].mode }}  
> when: nfs\_automount\_use AND item[1].path != 0  
> with\_items: nfs\_automount.mountpoints|dictsort  
> notify: restart nfs\_automount  
> tags: nfs\_automount

In my group\_vars file, nfs\_automount\_use is set to true.

In my host\_vars/hostname file, nfs\_automount\_use is set to false.

When I run the task, I get:

> TASK: [ensure nfs mountpoints exist] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> fatal: [hostname] =\> error while evaluating conditional: {% if nfs\_automount\_use AND item[1].path != 0 %} True {% else %} False {% endif %}  
> FATAL: all hosts have already failed – aborting

If I run the task with:

> when: item[1].path != 0

It iterates through the nfs\_automount.mountpoints as expected.

> when: nfs\_automount\_use

It skips the task like I want.

So, what am I doing wrong? Or is this a bug?

---

<div class="post-metadata">

**Author:** ![David\_Reagan](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@David\_Reagan](https://forum.ansible.com/u/David_Reagan)\
**Post date:** [October 25, 2013, 5:20pm UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-multiple-conditions-before-running-a-task/13734/2 "2013-10-25T17:20:44Z")

</div>

Found a way around the issue. When you are including a task file, you can add the when condition. Like:

> - include: nfs\_automount.yml  
> when: nfs\_automount\_use

If I set the var to false, it skips all the tasks in the yml file. 🙂

Though, there still should be a way I can use normal boolean logic in the when conditional… At least I think.

At least I have a decent, in some ways, actually better method to do what I was trying to do.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 25, 2013, 11:52pm UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-multiple-conditions-before-running-a-task/13734/3 "2013-10-25T23:52:45Z")

</div>

Yep!

Also you can do when conditions as lists, which is how this works internally.

If the first is False, it will not evaluate the rest either.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 25, 2013, 11:53pm UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-check-multiple-conditions-before-running-a-task/13734/4 "2013-10-25T23:53:27Z")

</div>

Additionally

when: nfs\_automount\_use AND item[1].path != 0

when: nfs\_automount is defined and item is defined and …

Technically also should work and short-circuit.
