# Find out where variable was defined

**URL:** <https://forum.ansible.com/t/find-out-where-variable-was-defined/31554>\
**Category:** Ansible Project\
**Created:** [October 28, 2019, 4:16pm UTC](https://forum.ansible.com/t/find-out-where-variable-was-defined/31554 "2019-10-28T16:16:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander1](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@Alexander1](https://forum.ansible.com/u/Alexander1)\
**Post date:** [October 28, 2019, 4:16pm UTC](https://forum.ansible.com/t/find-out-where-variable-was-defined/31554/1 "2019-10-28T16:16:04Z")

</div>

Hi,

I’am using Ansible 2.8.6.

Is there a posibility to find out, where a variable was defined?

I want to check inside a role if the variable was defined under the defaults folder or higher in the priority.

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#variable-precedence-where-should-i-put-a-variable](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable)

If the variable is in the default state, I want to promt the message “Are you sure you want to use the role default variable?” and the user must confirm.

Thanks for your help  
Alexander

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [October 28, 2019, 5:17pm UTC](https://forum.ansible.com/t/find-out-where-variable-was-defined/31554/2 "2019-10-28T17:17:25Z")

</div>

I'm not aware of such information being available. But the use-case can be  
easily achieved by including role's defaults into a dictionary and comparing  
the variable. For example

&nbsp;&nbsp;$ cat roles/role1/tasks/main.yml  
&nbsp;&nbsp;- include\_vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file: "{{ role\_path }}/defaults/main.yml"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "defaults"  
&nbsp;&nbsp;- block:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- pause:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prompt: "Are you sure you want to use the role default variable?"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: result.user\_input  
&nbsp;&nbsp;&nbsp;&nbsp;when: var1 == defaults.var1

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Alexander1](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@Alexander1](https://forum.ansible.com/u/Alexander1)\
**Post date:** [October 30, 2019, 8:03pm UTC](https://forum.ansible.com/t/find-out-where-variable-was-defined/31554/3 "2019-10-30T20:03:37Z")

</div>

Great idea. Thanks for your help.
