Also check your configuration, INJECT_FACTS_AS_VARS will enable/prevent top level variables from being created. Note that ansible_facts['os_family'] would be present either way if facts were gathered or cached.
FYI, the output of the setup module will have the ansilbe_ prefix on facts, it is when used in a playbook that the prefix is removed and only added back in the case of ‘injection’ being enabled. When using ‘adhoc’ you do not get any of this processing.
I would try ansible -i inventory -l host2 all -m setup | grep os_family since this will catch the fact if it exists with or without the ansible_ prefix. I’m also curious if ansible -i inventory -l host2 all -m setup | grep distribution returns anything.
If only a single host among thousands is failing to report these facts (prefix or not), then I would think that perhaps something is wrong with that host’s /etc/os-release file. Modifications to this file (or missing it entirely) can cause Ansible to misclassify or fail to gather OS related facts altogether.
Working host:
ansible -i inventory -l host1 all -m setup | grep os_family
“ansible_os_family”: “Debian”,
ansible -i inventory -l host1 all -m setup | grep distribution
“ansible_distribution”: “Ubuntu”,
“ansible_distribution_file_parsed”: true,
“ansible_distribution_file_path”: “/etc/os-release”,
“ansible_distribution_file_variety”: “Debian”,
“ansible_distribution_major_version”: “20”,
“ansible_distribution_release”: “focal”,
“ansible_distribution_version”: “20.04”,
Problematic host:
ansible -i inventory -l host2 all -m setup | grep os_family
<>
ansible -i inventory -l host2 all -m setup | grep distribution
<>
If only a single host among thousands is failing to report these facts (prefix or not), then I would think that perhaps something is wrong with that host’s /etc/os-release file.
Yes, only 1 host has problem.
/etc/os-release is identical to other hosts.
It might be easier to debug with -vvv than tracing the code (assuming you’re using the ssh connection plugin which includes the detail at that verbosity level): ansible -i inventory host2 -m setup -a 'gather_subset="!min,!all,distribution"' -vvv | grep '.*{"ansible_facts":.*"'
The gather_subset option limits the running code/output, and the grep matches the JSON returned by the setup module in -vvv since otherwise there’s a lot to sift through.
I’m just emulating the behavior by raising an arbitrary exception in the distribution facts logic, but the output could look similar to:
Can you help me understand how does the ssh verbose option of -vvv fit here?
And how do I find which ansible variable belong to which gather_subset group?