# Win\_reg\_stat - set\_fact problem with missing dict key

**URL:** <https://forum.ansible.com/t/win-reg-stat-set-fact-problem-with-missing-dict-key/26247>\
**Category:** Ansible Project\
**Created:** [December 11, 2017, 2:26pm UTC](https://forum.ansible.com/t/win-reg-stat-set-fact-problem-with-missing-dict-key/26247 "2017-12-11T14:26:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gareth\_Stockdale](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Gareth\_Stockdale](https://forum.ansible.com/u/Gareth_Stockdale)\
**Post date:** [December 11, 2017, 2:26pm UTC](https://forum.ansible.com/t/win-reg-stat-set-fact-problem-with-missing-dict-key/26247/1 "2017-12-11T14:26:58Z")

</div>

Hi

So new to ansible. I am using win\_reg\_stat to lookup registry keys to ultimately uninstall some software. If the reg entry is found, myvar.value ( after defining "register: myvar") contains the registry value. If it is not found, that key is not defined in the myvar dict.

Problem I have is that I cannot use below in both cases as if they key does not exist, it fails with Ansible defined variable error

Set\_fact:  
&nbsp;&nbsp;&nbsp;Thisvar: {{ myvar.value }}  
&nbsp;&nbsp;&nbsp;When: myvar.value is defined

I have tried a when: clause with is/is not defined, but the presence of the .value key in the setting of the variable/fact name causes failure. I would of thought (or liked to think), the when condition would prevent the setting and thus check of the undefined variable.

Any thoughts?

Thanks

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 11, 2017, 2:58pm UTC](https://forum.ansible.com/t/win-reg-stat-set-fact-problem-with-missing-dict-key/26247/2 "2017-12-11T14:58:55Z")

</div>

You have three problems, set\_fact and when is all lower case.  
When you have curly brackets after colon you need quotes around it.  
Your When becomes a variable since it is indented to far in, when must be on the task, indented the same as set\_fact.

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;Thisvar: '{{ myvar.value }}'  
&nbsp;&nbsp;when: myvar.value is defined

---

<div class="post-metadata">

**Author:** ![Gareth\_Stockdale](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Gareth\_Stockdale](https://forum.ansible.com/u/Gareth_Stockdale)\
**Post date:** [December 11, 2017, 5:40pm UTC](https://forum.ansible.com/t/win-reg-stat-set-fact-problem-with-missing-dict-key/26247/3 "2017-12-11T17:40:13Z")

</div>

Thanks Kai.

Apologise for the code presentation, I was typing from my phone. However your point 3 was the winner! 😀

Thank you.
