# register variable in conjunction with when\_changed

**URL:** <https://forum.ansible.com/t/register-variable-in-conjunction-with-when-changed/12403>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [February 22, 2013, 5:27am UTC](https://forum.ansible.com/t/register-variable-in-conjunction-with-when-changed/12403 "2013-02-22T05:27:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 22, 2013, 5:27am UTC](https://forum.ansible.com/t/register-variable-in-conjunction-with-when-changed/12403/1 "2013-02-22T05:27:17Z")

</div>

> I think I understand what's happening; the $packet\_changed "variable" isn't  
> getting "auto-created" because the action isn't being executed due to the  
> only\_if  
> So I tried the following workaround:  
> In group\_/vars/all, I defined package\_changed with the proper default  
> "skeleton" :  
> package\_changed: {"changed": false, "item": "", "msg": "", "rc": 0,  
> "results": [""]}
> 
> This seems to work : )  
> Am I missing something? Alternatively, can anyone point me to a better  
> solution/workaround?

I think you've got it exactly.

I think this is a (somewhat minor) bug we need to file. When  
skipping a conditional statement, store "None" in the value of the  
variable being registered, and is\_set($foo) of a variable that is None  
should return False.

Please file a github on this one and we can take care of it.

---

<div class="post-metadata">

**Author:** ![Asad\_Khan](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@Asad\_Khan](https://forum.ansible.com/u/Asad_Khan)\
**Post date:** [March 2, 2013, 2:21am UTC](https://forum.ansible.com/t/register-variable-in-conjunction-with-when-changed/12403/2 "2013-03-02T02:21:27Z")

</div>

Ticket filed (note: issue has easy and pretty clean workarounds):

Issue **#2269**

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [March 13, 2013, 4:04pm UTC](https://forum.ansible.com/t/register-variable-in-conjunction-with-when-changed/12403/3 "2013-03-13T16:04:24Z")

</div>

My solution to this was to initialize the variable to track. The result was something like this:

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [March 13, 2013, 4:30pm UTC](https://forum.ansible.com/t/register-variable-in-conjunction-with-when-changed/12403/4 "2013-03-13T16:30:36Z")

</div>

I used to do that (initialise the variable), then started to use "when\_set":  
[https://coderwall.com/p/q7dj5w?i=8&p=2&q=sort%3Ascore+desc&t[]=ansible](https://coderwall.com/p/q7dj5w?i=8&p=2&q=sort%3Ascore+desc&t%5B%5D=ansible)
