Using more advanced filters with ansible setup module
I'm new to ansible, but I cant find the answer to this question which I think looks fairly simple..
In playbooks, you can get facts in ansible using example {{ ansible_eth0["ipv4"]["address"] }}
or {{ ansible_eth0.ipv4.address }}
.
How can I grab the same information as an information-only command using something like ansible all -m setup -a "filter=???"
As filter
, I have tried both the syntaxes that you want to use in the playbooks, and some more exotic ones with combination of *
.
Is this even the right way to collect data like this?
Sadly, this is not supported. From the online documentation:
The filter option filters only the first level subkey below ansible_facts.
As the ansible_interface
fact is a first level subkey in the structure of the JSON document, the minimum you can access is something like:
# ansible localhost -m setup -a 'filter=ansible_p5p1'
localhost | success >> {
"ansible_facts": {
"ansible_p5p1": {
"active": true,
"device": "p5p1",
"ipv4": {
"address": "10.0.0.2",
"netmask": "255.255.255.0",
"network": "10.0.0.0"
},
"ipv6": [
{
"address": "fd6c:xxxx:xxxx::2",
"prefix": "64",
"scope": "global"
},
{
"address": "fe80::xxxx:xxxx:xxxx:5a56",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "xx:xx:xx:xx:xx:xx",
"module": "foo",
"mtu": 1500,
"promisc": false,
"type": "ether"
}
},
"changed": false
}
If your systems have facter
or ohai
installed, you can access the same information from ansible
:
# ansible localhost -m setup -a 'filter=facter_ipaddress_p5p1'
localhost | success >> {
"ansible_facts": {
"facter_ipaddress_p5p1": "10.0.0.2"
},
"changed": false
}