Win_reg_stat - set_fact problem with missing dict key

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:
   Thisvar: {{ myvar.value }}
   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

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:
    Thisvar: '{{ myvar.value }}'
  when: myvar.value is defined

Thanks Kai.

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

Thank you.