# Bug in Conditional Includes

**URL:** <https://forum.ansible.com/t/bug-in-conditional-includes/13125>\
**Category:** Ansible Project\
**Created:** [July 15, 2013, 8:18pm UTC](https://forum.ansible.com/t/bug-in-conditional-includes/13125 "2013-07-15T20:18:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Feanil\_Patel](https://avatars.discourse-cdn.com/v4/letter/f/e36b37/32.png) [@Feanil\_Patel](https://forum.ansible.com/u/Feanil_Patel)\
**Post date:** [July 15, 2013, 8:18pm UTC](https://forum.ansible.com/t/bug-in-conditional-includes/13125/1 "2013-07-15T20:18:21Z")

</div>

Hi,

I know that when using conditionals on an include, ‘All the tasks get evaluated, but the conditional is applied to each and every task’. However this breaks when some of that tasks register variables and other tasks in the group use those variable.

Example:

main.yml:

```auto
- include: extra.yml
when: do_extra is defined

```

extra.yml:

```auto
- name: check if we can do task A
shell: check_if_task_A_possible
register: A_possible
ignore_errors: yes

- name: task A
shell: run_task_A
when: A_possible.rc == 0

```

Now if you run main.yml and ‘do\_extra’ is not defined, the run will fail on ‘task A’ because when the ‘when’ condition is evaluated, the variable A\_possible will not exist.

---

<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:** [July 15, 2013, 8:45pm UTC](https://forum.ansible.com/t/bug-in-conditional-includes/13125/2 "2013-07-15T20:45:28Z")

</div>

So you are saying the conditional in this case should be added _above_ the other conditional, not below. That seems reasonable to me, as I can’t think of a case where that would be a bad thing.

Please make sure there is a github ticket filed to track this one and we can change it to insert rather than append.

---

<div class="post-metadata">

**Author:** ![Feanil\_Patel](https://avatars.discourse-cdn.com/v4/letter/f/e36b37/32.png) [@Feanil\_Patel](https://forum.ansible.com/u/Feanil_Patel)\
**Post date:** [July 15, 2013, 8:53pm UTC](https://forum.ansible.com/t/bug-in-conditional-includes/13125/3 "2013-07-15T20:53:20Z")

</div>

I don’t think that is sufficient because right now it looks like the two conditions are compounded and tested together which will still fail because A\_possible is not defined. I think you would have to run the file level conditional before the task level ones to keep this from happening.

Updated my original message with my above statement and added it as an issue to github.

[https://github.com/ansible/ansible/issues/3539](https://github.com/ansible/ansible/issues/3539)

---

<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:** [July 15, 2013, 9:15pm UTC](https://forum.ansible.com/t/bug-in-conditional-includes/13125/4 "2013-07-15T21:15:24Z")

</div>

Forgot about that, but …

If the conditional is compounded it will still have short circuit evaluation behavior.

I suspect it’s added to the end and not the start.

---

<div class="post-metadata">

**Author:** ![Feanil\_Patel](https://avatars.discourse-cdn.com/v4/letter/f/e36b37/32.png) [@Feanil\_Patel](https://forum.ansible.com/u/Feanil_Patel)\
**Post date:** [July 15, 2013, 9:29pm UTC](https://forum.ansible.com/t/bug-in-conditional-includes/13125/5 "2013-07-15T21:29:48Z")

</div>

Ah, you’re right. That will be just fine.
