-m setup -a filter="ansible_eth0.ipv4.address" returns no results

So I’m trying to get the IP address of every server via ansible and expect to use something like

ansible -i inventory host_group -m setup -a filter=“ansible_eth0.ipv4.address” returns no results. It returns something like

`
server38.prod.foo | success >> {
“ansible_facts”: {},
“changed”: false
}

`

But I can do ansible -i inventory host_group -m setup -a filter=“ansible_eth0” and it will return:

`
server38.prod.foo | success >> {
“ansible_facts”: {
“ansible_eth0”: {
“active”: true,
“device”: “eth0”,
“ipv4”: {
“address”: “1.2.3.4.”,
“netmask”: “255.255.255.0”,
“network”: “1.2.3.0”
},
“ipv6”: [
{
“address”: “fe11::225:90ff:fe11:1111”,
“prefix”: “64”,
“scope”: “link”
}
],
“macaddress”: “00:11:11:11:11:11”,
“module”: “e1000e”,
“mtu”: 1500,
“promisc”: false,
“type”: “ether”
}
},
“changed”: false
}

`

So it does return facts…how do I get filter further to return just the IP address?

One of the notes in the docs of the setup module reads: "the filter option filters only the first level subkey below ansible_facts." So you don't. Not that way at least.

What about ansible_all_ipv4_addresses?

Yeah, I’m aware of ansible_all_ipv4_addresses but that shows all interfaces and I just want eth0. Assuming that list is eth0, eth1, etc. etc. then I guess I could just take the first item in the list.

Not a straight way but a roundabout way

ansible -i inventory host_group -m setup -a filter=“ansible_eth0”|grep -A1 ipv4|tail -1|cut -d: -f2|cut -d""" -f2

Need to check if there are different ways to extract it.

$ ansible -i inventory -m setup -a “filter=facter_ipaddress_eth1” hostname.example.org

`

hostname.example.org | SUCCESS => {
“ansible_facts”: {
“facter_ipaddress_eth1”: “192.168.33.10”
},
“changed”: false
}

`

Igor, I’ve been googling a bit and can’t find what the difference between facter_ipaddress_eth1 and ansible_eth0.ipv4.address is. Could you please clarify?

If facter (a Puppet utility) is installed on the node, ansible picks
up those facts along with its own.

If facter (a Puppet utility) is installed on the node, ansible picks
up those facts along with its own.

Thanks Dick, looks like I’ll have to install facter on my CentOS box.

The difference might be a non-existing interface :wink:

facter is requested to show the address of interface "eth1" while
ansible should show the address of "eth0".

D&r
Werner

Agusti Tomas [18.08.2016 17:16]:

Werner,

Yes, that was a typo, but if it would have been ansible_eth1.ipv4.address, would the expressions have been equivalent?

Agusti Tomas [19.08.2016 14:13]:

Werner,

Yes, that was a typo, but if it would have been ansible_eth1.ipv4.address,
would the expressions have been equivalent?

Unfortunately not:

# ansible me -m setup -a "filter=facter_ipaddress_eth1"
localhost | SUCCESS => {
    "ansible_facts": {
        "facter_ipaddress_eth1": "141.65.x.y"
    },
    "changed": false
}

ansible me -m setup -a "filter=ansible_eth1.ipv4.address"
localhost | SUCCESS => {
    "ansible_facts": {},
    "changed": false
}

I get a (lengthy) result for
# ansible me -m setup -a "filter=ansible_eth1"
but I don't get a result for
# ansible me -m setup -a "filter=ansible_eth1.ipv4"
# ansible me -m setup -a "filter=ansible_eth1.ipv4"
# ansible me -m setup -a "filter=ansible_eth1[ipv4]"
# ansible me -m setup -a "filter=ansible_eth1['ipv4']"

Werner

Alright, thanks. I better get facter to work then.

Waaah, reading the docs might make that clear:

2nd note on <http://docs.ansible.com/ansible/setup_module.html&gt;:

The filter option filters only the first level subkey below ansible_facts.

And since the first level subkey is "ansible_eth1", the filter does not
look deeper.

Facter's most facts are not nested (except for e.g. facter_os), so this
works better in the given case.

Werner

Agusti Tomas [19.08.2016 14:54]:

I got facter to install via playbook now on my CentOS 7 box and the first time it runs the playbook it doesn’t find the facter_ipaddress_eth1 but it does find it the second time I run the playbook. Do you know if there should be some waiting time after installing facter?

I am not aware of an after-installation pause. Do you use some cache for
facts, like redis?

There may be a timeout during the collection of facts, as I can see at
my box (not Centos, but SLES), the runtime is either short or quite long
- maybe some facts are hard to detect.

Agusti Tomas [19.08.2016 15:08]:

Nothing complicated, facter_ipaddress_eth1.

Actually I have this in my playbook:

  • name: Check facter
    command: “facter ipaddress_eth1”
    register: result

  • debug:
    var: result

And it prints the right result:

==> buildmaster: TASK [common : install facter] *************************************************
==> buildmaster: ok: [127.0.0.1]
==> buildmaster:
==> buildmaster: TASK [common : Check facter] ***************************************************
==> buildmaster: changed: [127.0.0.1]
==> buildmaster:
==> buildmaster: TASK [common : debug] **********************************************************
==> buildmaster: ok: [127.0.0.1] => {
==> buildmaster: “result”: {
==> buildmaster: “changed”: true,
==> buildmaster: “cmd”: [
==> buildmaster: “facter”,
==> buildmaster: “ipaddress_eth1”
==> buildmaster: ],
==> buildmaster: “delta”: “0:00:01.062820”,
==> buildmaster: “end”: “2016-08-19 13:38:14.678980”,
==> buildmaster: “rc”: 0,
==> buildmaster: “start”: “2016-08-19 13:38:13.616160”,
==> buildmaster: “stderr”: “”,
==> buildmaster: “stdout”: “192.168.20.200”,
==> buildmaster: “stdout_lines”: [
==> buildmaster: “192.168.20.200”
==> buildmaster: ],
==> buildmaster: “warnings”:
==> buildmaster: }
==> buildmaster: }

Real funny stuff going on here.

Forgot to mention I run Check facter just after this:

  • name: install facter
    yum:
    name: facter
    state: latest

So no much time to gather facts.

Just run gather facts after you installed facter like this

- setup:

Thanks Kai, however I didn’t fully catch that. You mean

- name: install facter
yum:
name: facter
state: latest

- setup:

Like this?