Confusing "variable is undefined" error

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?

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.

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

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.