# Confusing "variable is undefined" error

**URL:** <https://forum.ansible.com/t/confusing-variable-is-undefined-error/20221>\
**Category:** Ansible Project\
**Created:** [January 14, 2016, 9:49pm UTC](https://forum.ansible.com/t/confusing-variable-is-undefined-error/20221 "2016-01-14T21:49:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guy\_Knights](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Guy\_Knights](https://forum.ansible.com/u/Guy_Knights)\
**Post date:** [January 14, 2016, 9:49pm UTC](https://forum.ansible.com/t/confusing-variable-is-undefined-error/20221/1 "2016-01-14T21:49:28Z")

</div>

I have a variable defined in group\_vars that I’m trying to use in a task but it is failing for me. So I created a debug task to dump the contents of this variable so I can see what ansible thinks is defined. If I try the following:

- debug: var=mysql

It outputs “mysql”: “VARIABLE IS NOT DEFINED!”. I changed the above debug task to:

- debug: var=hostvars[inventory\_hostname][‘mysql’]

When I run it with the change above I get instead:

`"hostvars[inventory_hostname]['mysql']": { "dbs": [{ "backup": { "retention": 3, "window": "06:00-06:30" }, "name": "db1", "size": 100, "type": "rds", "users": [ { "host": "127.0.0.1", "name": "db1user", "password": "userpass", "privs": [ "db1.*:ALL"] } ] }, { "name": "db2", "type": "mysql", "users": [{ "host": "127.0.0.1", "name": "root", "password": "testpass", "privs": [ "*.*:ALL"] }, { "host": "::1", "name": "root", "password": "testpass", "privs": ["*.*:ALL"] }, { "host": "localhost", "name": "root", "password": "testpass", "privs": ["*.*:ALL"] }, { "host": "{{ hostvars[inventory_hostname][ansible_hostname] }}", "name": "root", "password": "testpass", "privs": ["*.*:ALL"] }, { "host": "127.0.0.1", "name": "db2user", "password": "userpass", "privs": ["db2.*:ALL"] } ] }, { "name": "db3", "type": "mysql", "users": [{ "host": "127.0.0.1", "name": "root", "password": "testpass", "privs": [ "*.*:ALL"] }, { "host": "::1", "name": "root", "password": "testpass", "privs": ["*.*:ALL"] }, { "host": "localhost", "name": "root", "password": "testpass", "privs": ["*.*:ALL"] }, { "host": "{{ hostvars[inventory_hostname][ansible_hostname] }}", "name": "root", "password": "testpass", "privs": ["*.*:ALL"] }, { "host": "127.0.0.1", "name": "db3user", "password": "userpass", "privs": ["db3.*:ALL"] } ] } ] }`

So, it works when I reference the var from the hostvars, but when I reference it as a standalone variable it doesn’t. I also checked with some other vars that are defined in the exact same way within my group\_vars, and they output fine whichever method I use.

What’s the issue here? Am I missing something?

---

<div class="post-metadata">

**Author:** ![Guy\_Knights](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Guy\_Knights](https://forum.ansible.com/u/Guy_Knights)\
**Post date:** [January 14, 2016, 9:50pm UTC](https://forum.ansible.com/t/confusing-variable-is-undefined-error/20221/2 "2016-01-14T21:50:03Z")

</div>

Sorry, I should also mention that I’m on the devel branch. I tried it using stable-2.0 but it did the same thing.

---

<div class="post-metadata">

**Author:** ![Guy\_Knights](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Guy\_Knights](https://forum.ansible.com/u/Guy_Knights)\
**Post date:** [January 18, 2016, 9:42pm UTC](https://forum.ansible.com/t/confusing-variable-is-undefined-error/20221/3 "2016-01-18T21:42:56Z")

</div>

I still haven’t found the source of this issue, and I’ve just discovered another variable that is showing the same behaviour. I might also mention the structure of my group\_vars folder; these vars I’m having trouble with are in separate files within _inventory/group\_vars/all/_. As I understand it, using this pattern should be fine, and as I mentioned above I’ve tried dumping the contents of other variables from within the _inventory/group\_vars/all/\*.yml_ files and they are fine. So it’s really just these two specific variables that don’t seem to be recognised.

For the record, the two vars I’m having trouble with are called _mysql_ and _ips_. I thought maybe it was an issue where these are reserved words in Ansible, so I changed the var names but it didn’t fix the problem.

If anyone has any suggestions, I’m all ears. I’ve been playing with this for a few days now and it’s starting to do my head in.

Thanks,  
Guy

---

<div class="post-metadata">

**Author:** ![Guy\_Knights](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Guy\_Knights](https://forum.ansible.com/u/Guy_Knights)\
**Post date:** [January 18, 2016, 9:59pm UTC](https://forum.ansible.com/t/confusing-variable-is-undefined-error/20221/4 "2016-01-18T21:59:26Z")

</div>

Ok, of course I figured this out just as I updated my post. For future reference, the reason it was failing is because I was using the hostvars var (eg. hostvars[inventory\_hostname][‘ansible\_hostname’]) as one of the values in a nested sub-list of these dict variables. I guess this doesn’t work because this variable wasn’t defined for specific hosts, and thus the list item was undefined. However, it wasn’t clear that this was the case because none of the errors I received indicated that the problem was related to my use of hostvars in this context.

I’ve removed the hostvars items from both of my dicts and my playbooks now seem to work, so the problem is solved.
