# Ansible skipping the rest of the tasks after one conditional task

**URL:** <https://forum.ansible.com/t/ansible-skipping-the-rest-of-the-tasks-after-one-conditional-task/19623>\
**Category:** Ansible Project\
**Created:** [October 27, 2015, 7:55am UTC](https://forum.ansible.com/t/ansible-skipping-the-rest-of-the-tasks-after-one-conditional-task/19623 "2015-10-27T07:55:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joshua\_J\_Kugler](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@Joshua\_J\_Kugler](https://forum.ansible.com/u/Joshua_J_Kugler)\
**Post date:** [October 27, 2015, 7:55am UTC](https://forum.ansible.com/t/ansible-skipping-the-rest-of-the-tasks-after-one-conditional-task/19623/1 "2015-10-27T07:55:57Z")

</div>

So, granted, I'm rather new at Ansible, this is really not fitting my  
understanding of Ansible. 🙂

I conditionally include this file:

[https://gist.github.com/jkugler/3c683b539575ba4f0d66](https://gist.github.com/jkugler/3c683b539575ba4f0d66)

The condition that includes it is satisfied, and it is included. The first task  
runs (the stat to check for a file). This causes the second task to skip, since  
"st.stat.exists == False" is not so (i.e. don't run if the file exists).  
However, then the rest of the tasks in that file are skipped as well, even  
though they have no "when" conditional on them.

Footnote: I know I don't have to do the stat check before the get\_url. I had  
named a file wrong, and get\_url was failing, and I wasn't understanding the  
error message, thus the stat call. But it did bring up this odd behavior that  
I would still like to understand.

j

---

<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:** [October 28, 2015, 12:51am UTC](https://forum.ansible.com/t/ansible-skipping-the-rest-of-the-tasks-after-one-conditional-task/19623/2 "2015-10-28T00:51:41Z")

</div>

the when in the include is applied to each included task, if it also  
relies on the st variable it would explain what you are seeing.

---

<div class="post-metadata">

**Author:** ![Joshua\_J\_Kugler](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@Joshua\_J\_Kugler](https://forum.ansible.com/u/Joshua_J_Kugler)\
**Post date:** [October 28, 2015, 4:45pm UTC](https://forum.ansible.com/t/ansible-skipping-the-rest-of-the-tasks-after-one-conditional-task/19623/3 "2015-10-28T16:45:21Z")

</div>

I'm not sure I'm following. Are you saying you can't reuse register  
variables? Here's how it worked in my task:

- name: Does the prepared ISO exist?  
&nbsp;&nbsp;stat: path=/var/lib/ISO/CentOS-6.7-x86\_64-minimal/.treeinfo  
&nbsp;&nbsp;register: st

- name: Get it and prepare it if not  
&nbsp;&nbsp;include: get\_centos\_67.yml  
&nbsp;&nbsp;when: st.stat.exists == False

So, when it includes the file, the condition is true. It then walks through  
[https://gist.github.com/jkugler/3c683b539575ba4f0d66](https://gist.github.com/jkugler/3c683b539575ba4f0d66)

The first thing it does is another stat, which is stored in 'st'. In this case,  
'st.stat.exists == False' is false, because the file is there, which mean it  
skipped the download. Once I removed the 'when' in the above gist, it  
properly processed the file. It \*SEEMED\* the 'when' in the gist was affecting  
the rest of the steps in the gist.

j

---

<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:** [October 28, 2015, 5:15pm UTC](https://forum.ansible.com/t/ansible-skipping-the-rest-of-the-tasks-after-one-conditional-task/19623/4 "2015-10-28T17:15:24Z")

</div>

no, im saying that if you had a condition on the include, each task  
inherits it and evaluates it when run

so even if you don't have an explicit condition on them you have an implicit one

---

<div class="post-metadata">

**Author:** ![Joshua\_J\_Kugler](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@Joshua\_J\_Kugler](https://forum.ansible.com/u/Joshua_J_Kugler)\
**Post date:** [October 28, 2015, 5:55pm UTC](https://forum.ansible.com/t/ansible-skipping-the-rest-of-the-tasks-after-one-conditional-task/19623/5 "2015-10-28T17:55:23Z")

</div>

OK, so the condition for the include was satisfied, which means for each step  
in the file should have the condition satisfied.

Are you saying because I used the same var in both places, the \*second\* use of  
'st' is used to evaluate the 'when' condition that is implicitly applied to  
all the included steps? That sounds like a scoping problem, if so.

j

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [October 28, 2015, 7:07pm UTC](https://forum.ansible.com/t/ansible-skipping-the-rest-of-the-tasks-after-one-conditional-task/19623/6 "2015-10-28T19:07:45Z")

</div>

Registered variables are stored on a per-host basis, and thus are always visible to the host when it’s compiling its variables. So it’s not really a scoping problem, as it’s not tied to the include. Now, if you passed the variable in as a parameter to the include, it would in fact limit the scope to that include.

---

<div class="post-metadata">

**Author:** ![Joshua\_J\_Kugler](https://avatars.discourse-cdn.com/v4/letter/j/ecc23a/32.png) [@Joshua\_J\_Kugler](https://forum.ansible.com/u/Joshua_J_Kugler)\
**Post date:** [October 28, 2015, 8:48pm UTC](https://forum.ansible.com/t/ansible-skipping-the-rest-of-the-tasks-after-one-conditional-task/19623/7 "2015-10-28T20:48:17Z")

</div>

OK, that makes sense. First part of the confusion came because I didn't  
realize Ansible eval'ed the 'when' condition for each step of the include.  
Probably didn't remember reading that in the docs.

As for the scoping, to be sure I'm understanding:

The "outer" file creates a var called 'st' and uses it in the 'when' to include  
\<file\>.

\<file\> has a step which registers its result in 'st' (and changes the value in  
this case).

The outer file then re-evaluates the 'st' var (which is the \*same\* var), sees  
the condition is no longer true, and skips the rest of the steps.

That's actually kind of handy...the included file could short-circuit its own  
execution if it changed the value of the var used in the 'when'.

Much thanks to you and Brian for your patience in walking me through this!

j
