# conditional with register in one task?

**URL:** <https://forum.ansible.com/t/conditional-with-register-in-one-task/17719>\
**Category:** Ansible Project\
**Created:** [March 11, 2015, 3:06pm UTC](https://forum.ansible.com/t/conditional-with-register-in-one-task/17719 "2015-03-11T15:06:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Patermann](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Marc\_Patermann](https://forum.ansible.com/u/Marc_Patermann)\
**Post date:** [March 11, 2015, 3:06pm UTC](https://forum.ansible.com/t/conditional-with-register-in-one-task/17719/1 "2015-03-11T15:06:52Z")

</div>

Hi,

should this work:

&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;- name: test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: uname -a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: env == "test"

&nbsp;&nbsp;&nbsp;- name: test 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file: path=/opt/test.txt state=touch  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: env == "test1"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: test\_1

&nbsp;&nbsp;&nbsp;- name: test 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file: path=/opt/test2 state=touch  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: test\_1 is defined

Task "test 1" should run, if some conditional is true.  
Depending on if it runs, task "test 2" should run.  
That's why i registered a variable test\_1.

The result is, that "test 1" ist skipped", because env != "test" (as expected; OK). But "test 2" does run too.

I could use a handler, but that does only run after all the tasks, which why I have to change the running order. Is there another way?

Marc

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [March 11, 2015, 3:08pm UTC](https://forum.ansible.com/t/conditional-with-register-in-one-task/17719/2 "2015-03-11T15:08:52Z")

</div>

Even though a task is skipped, the registered variable is still created. You would want to check something like the following on your “test 2” task instead:

when: not test\_1|skipped

---

<div class="post-metadata">

**Author:** ![Marc\_Patermann](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Marc\_Patermann](https://forum.ansible.com/u/Marc_Patermann)\
**Post date:** [March 12, 2015, 10:53am UTC](https://forum.ansible.com/t/conditional-with-register-in-one-task/17719/3 "2015-03-12T10:53:24Z")

</div>

Matt,

Matt Martz schrieb (11.03.2015 16:08 Uhr):

> Even though a task is skipped, the registered variable is still created. You would want to check something like the following on your "test 2" task instead:
> 
> when: not test\_1|skipped

this works, thank you!

The docs could be a bit clearer about this part.  
[http://docs.ansible.com/playbooks\_conditionals.html](http://docs.ansible.com/playbooks_conditionals.html) does not explain what "result|skipped" exactly means.

Marc
