AnsibleUndefinedVariable: ‘ansible.vars.hostvars.HostVarsVars object’ has no attribute ‘my_var’

Having issues to troubleshoot this error and finding the faulty host? Verbose mode won’t help you, neither will looking at your hosts.

One way to go at it, is to run ad-hoc commands, for instance we want to test the variable “ansible_host” which is (or should be) defined in host vars (we use it for templating):

ansible -i inventory all -m debug -a "var=hostvars[inventory_hostname].ansible_host"

Then you could get grep to filter out the matches like so:

ansible -i inventory all -m debug -a "var=hostvars[inventory_hostname].ansible_host" | grep -B1 "NOT DEFINED"

--

--

https://gomes.sh/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store