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?