# Conditional register not working

**URL:** <https://forum.ansible.com/t/conditional-register-not-working/15872>\
**Category:** Ansible Project\
**Created:** [August 6, 2014, 2:02pm UTC](https://forum.ansible.com/t/conditional-register-not-working/15872 "2014-08-06T14:02:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manuel\_Quinones](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Manuel\_Quinones](https://forum.ansible.com/u/Manuel_Quinones)\
**Post date:** [August 6, 2014, 2:02pm UTC](https://forum.ansible.com/t/conditional-register-not-working/15872/1 "2014-08-06T14:02:10Z")

</div>

Hi,

I’m trying to do a conditional register in a playbook:

tasks:

- shell: echo /opt/cache/www/  
register: www\_data  
when: ansible\_hostname | match(“^xy-”)

- shell: echo /mnt/code/www/  
register: www\_data  
when: ansible\_hostname | match(“^az-”)

- debug: var=www\_data.stdout  
when: www\_data.rc is defined and www\_data.rc == 0

Only the last register “/mnt/code/www/” is shown by debug, and for the other I’m getting failures.  
I would like to know what is the proper way to do this.

Thanks,

---

<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:** [August 6, 2014, 3:02pm UTC](https://forum.ansible.com/t/conditional-register-not-working/15872/2 "2014-08-06T15:02:53Z")

</div>

Can you share what a failure means in the above, any output you have, and what ansible version you are running?

Thanks!

---

<div class="post-metadata">

**Author:** ![Manuel\_Quinones](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Manuel\_Quinones](https://forum.ansible.com/u/Manuel_Quinones)\
**Post date:** [August 7, 2014, 11:45am UTC](https://forum.ansible.com/t/conditional-register-not-working/15872/3 "2014-08-07T11:45:34Z")

</div>

I found the answer in a previous post: [https://groups.google.com/d/topic/ansible-project/NGXcbyQvz-Y/discussion](https://groups.google.com/d/topic/ansible-project/NGXcbyQvz-Y/discussion)

So register stores the variable even if the task is skipped, and this is the intended behaviour.

I was able to do what I want with one longer shell command, although I was expecting something more elegant:

tasks:

- name: test  
shell: if [{{ ansible\_hostname | match(“^xy-”) }} == True]; then echo /opt/cache/www; else if [{{ ansible\_hostname | match(“^az-”) }} == True]; then echo /mnt/code/www; fi; fi  
register: www\_data

- debug: var=www\_data.stdout  
when: www\_data.rc == 0

---

<div class="post-metadata">

**Author:** ![Manuel\_Quinones](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Manuel\_Quinones](https://forum.ansible.com/u/Manuel_Quinones)\
**Post date:** [August 7, 2014, 12:34pm UTC](https://forum.ansible.com/t/conditional-register-not-working/15872/4 "2014-08-07T12:34:14Z")

</div>

Hi Michael,

ansible version is 1.6.3, and below is a sample output. You will see that debug task is being executed only for the vars registered in the second shell task. For failures I was wrong, sorry, there are all “failed=0” in this run.

Might be a good addition to the docs that register always store, even when tasks are skipped. I can write a line and send a pull request if you want.  
Also, maybe print a warning when the same register is being used more than once?

Thanks!

`

---

<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:** [August 7, 2014, 9:30pm UTC](https://forum.ansible.com/t/conditional-register-not-working/15872/5 "2014-08-07T21:30:53Z")

</div>

“Also, maybe print a warning when the same register is being used more than once?”

It’s somewhat common for someone to use a repeated variable “result”, so this isn’t really a problem.

The above conditionals could be made easier using “set\_fact” BTW:

- set\_fact: path=default\_path\_goes\_here
- set\_fact: path=other\_path  
when: ansible\_hostname | match(“^xy-”)

+1 for cowsay installs. Don’t see it enough 🙂

---

<div class="post-metadata">

**Author:** ![Manuel\_Quinones](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Manuel\_Quinones](https://forum.ansible.com/u/Manuel_Quinones)\
**Post date:** [August 8, 2014, 2:49am UTC](https://forum.ansible.com/t/conditional-register-not-working/15872/6 "2014-08-08T02:49:02Z")

</div>

I’m glad to know about set\_fact now. Thanks Michael!
