# How to avoid error when using a registered variable in check mode?

**URL:** <https://forum.ansible.com/t/how-to-avoid-error-when-using-a-registered-variable-in-check-mode/23461>\
**Category:** Ansible Project\
**Created:** [January 15, 2017, 3:03pm UTC](https://forum.ansible.com/t/how-to-avoid-error-when-using-a-registered-variable-in-check-mode/23461 "2017-01-15T15:03:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 15, 2017, 3:03pm UTC](https://forum.ansible.com/t/how-to-avoid-error-when-using-a-registered-variable-in-check-mode/23461/1 "2017-01-15T15:03:11Z")

</div>

Hi,

I have a playbook that has to use the command module and registers its  
output in a variable like so:

- command: do something here  
&nbsp;&nbsp;register: my\_variable

- debug: msg="Here is the output of {{ my\_variable.stdout }}"

When running this in check\_mode, the command task is skipped and the  
variable my\_variable does not have a object stdout. So ansible errors out.

How to avoid this? I got two thoughts, any hints or better practices  
are highly welcome.

1. Set check\_mode: no and always run the command task so the variable  
always contains the stdout object.  
2. Only run the debug task when my\_variable.skipped is not defined.

Thanks in advance,  
Johannes

---

<div class="post-metadata">

**Author:** ![Anthony\_Bond1](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@Anthony\_Bond1](https://forum.ansible.com/u/Anthony_Bond1)\
**Post date:** [January 15, 2017, 6:43pm UTC](https://forum.ansible.com/t/how-to-avoid-error-when-using-a-registered-variable-in-check-mode/23461/2 "2017-01-15T18:43:41Z")

</div>

Hello Johannes,

You might want to use the [Jinja2 defined test](http://docs.ansible.com/ansible/playbooks_conditionals.html#the-when-statement).

Your debug task would look something like this.

`

- debug: msg=“Here is the output of {{ my\_variable.stdout }}”  
when: my\_variable is defined

`

Just a thought…

---

<div class="post-metadata">

**Author:** ![Paul\_Totterman](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Paul\_Totterman](https://forum.ansible.com/u/Paul_Totterman)\
**Post date:** [January 16, 2017, 9:32am UTC](https://forum.ansible.com/t/how-to-avoid-error-when-using-a-registered-variable-in-check-mode/23461/3 "2017-01-16T09:32:07Z")

</div>

> 1. Set check\_mode: no and always run the command task so the variable

> always contains the stdout object.  
> 2. Only run the debug task when my\_variable.skipped is not defined.

1. only run the debug task when not in check\_mode
2. set some sort of default value when in check\_mode  
[https://docs.ansible.com/ansible/playbooks\_checkmode.html](https://docs.ansible.com/ansible/playbooks_checkmode.html)

Cheers,  
Paul

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 17, 2017, 7:34pm UTC](https://forum.ansible.com/t/how-to-avoid-error-when-using-a-registered-variable-in-check-mode/23461/4 "2017-01-17T19:34:57Z")

</div>

Thanks, I already had that in mind for the element skipped of my\_variable:

"2. Only run the debug task when my\_variable.skipped is not defined."

Johannes

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 17, 2017, 7:36pm UTC](https://forum.ansible.com/t/how-to-avoid-error-when-using-a-registered-variable-in-check-mode/23461/5 "2017-01-17T19:36:32Z")

</div>

Thanks for the thoughts. Which one would you pick? Which seems the  
cleanest?

Johannes
