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

4,226 views
Skip to first unread message

Barry Morrison

unread,
Apr 29, 2015, 3:04:57 PM4/29/15
to ansible...@googlegroups.com
 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?

Timothy Appnel

unread,
Apr 29, 2015, 4:06:13 PM4/29/15
to ansible...@googlegroups.com
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?

Barry Morrison

unread,
Apr 29, 2015, 4:12:05 PM4/29/15
to ansible...@googlegroups.com
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.

Raj Desikavinayagompillai

unread,
Apr 29, 2015, 4:37:21 PM4/29/15
to ansible...@googlegroups.com
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. 

Igor I

unread,
Jul 5, 2016, 8:44:29 AM7/5/16
to Ansible Project
$ 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
}


Agusti Tomas

unread,
Aug 18, 2016, 12:41:38 PM8/18/16
to Ansible Project
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?

Dick Davies

unread,
Aug 19, 2016, 5:41:30 AM8/19/16
to ansible list
If facter (a Puppet utility) is installed on the node, ansible picks
up those facts along with its own.
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/6fb5e1aa-782d-47ce-8936-a58d1443baa0%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Dick Davies

unread,
Aug 19, 2016, 5:48:43 AM8/19/16
to ansible list
If facter (a Puppet utility) is installed on the node, ansible picks
up those facts along with its own.

On 18 August 2016 at 16:16, Agusti Tomas <agusti...@gmail.com> wrote:

Agusti Tomas

unread,
Aug 19, 2016, 6:13:43 AM8/19/16
to ansible list
Thanks Dick,  looks like I'll have to install facter on my CentOS box. 

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/OdzrSElDlAg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

Agustí Tomàs

Werner Flamme

unread,
Aug 19, 2016, 6:56:30 AM8/19/16
to ansible...@googlegroups.com
The difference might be a non-existing interface ;)

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]:
--


Agusti Tomas

unread,
Aug 19, 2016, 8:13:54 AM8/19/16
to ansible...@googlegroups.com
Werner,

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

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/OdzrSElDlAg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

Agustí Tomàs

Werner Flamme

unread,
Aug 19, 2016, 8:52:16 AM8/19/16
to ansible...@googlegroups.com
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

Agusti Tomas

unread,
Aug 19, 2016, 8:54:45 AM8/19/16
to ansible...@googlegroups.com
Alright, thanks. I better get facter to work then.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/OdzrSElDlAg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

Agustí Tomàs

Werner Flamme

unread,
Aug 19, 2016, 9:05:16 AM8/19/16
to ansible...@googlegroups.com
Waaah, reading the docs might make that clear:

2nd note on <http://docs.ansible.com/ansible/setup_module.html>:

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]:

Agusti Tomas

unread,
Aug 19, 2016, 9:08:18 AM8/19/16
to ansible...@googlegroups.com
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?


For more options, visit https://groups.google.com/d/optout.
--

Agustí Tomàs

Werner Flamme

unread,
Aug 19, 2016, 9:26:34 AM8/19/16
to ansible...@googlegroups.com
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]:

Agusti Tomas

unread,
Aug 19, 2016, 9:39:47 AM8/19/16
to ansible...@googlegroups.com
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.


For more options, visit https://groups.google.com/d/optout.
--

Agustí Tomàs

Agusti Tomas

unread,
Aug 19, 2016, 9:40:35 AM8/19/16
to ansible...@googlegroups.com
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.
--

Agustí Tomàs

Kai Stian Olstad

unread,
Aug 19, 2016, 9:53:52 AM8/19/16
to ansible...@googlegroups.com
On 19. aug. 2016 15:40, Agusti Tomas wrote:
> 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:

--
Kai Stian Olstad

Agusti Tomas

unread,
Aug 19, 2016, 9:57:30 AM8/19/16
to ansible...@googlegroups.com
Thanks Kai, however I didn't fully catch that. You mean

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

- setup:

Like this?

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/OdzrSElDlAg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

Agustí Tomàs

Kai Stian Olstad

unread,
Aug 19, 2016, 10:07:21 AM8/19/16
to ansible...@googlegroups.com
On 19. aug. 2016 15:57, Agusti Tomas wrote:
> Thanks Kai, however I didn't fully catch that. You mean
>
> *- name: install facter*
> * yum:*
> * name: facter*
> * state: latest*
>
> *- setup:*
>
> *Like this?*

Yes, it will then rerun facts after facter is installed so you will get
facters fact in the current run.

If gather_facts is true, you will run gather facts two time every time
you run the play.
If this is not desired you could also do it like this

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

- setup:
when: installed_facter | changed

--
Kai Stian Olstad

Agusti Tomas

unread,
Aug 19, 2016, 10:16:20 AM8/19/16
to ansible...@googlegroups.com
Thanks Kai, that did the trick.

Unrelated to this thread, I really need some help with this other problem I'm having: https://groups.google.com/d/topic/ansible-project/IuZPsZ4CBS8/discussion so if any of the helpful souls in this thread can have a look, I'd appreciate it.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/OdzrSElDlAg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--

Agustí Tomàs

Reply all
Reply to author
Forward
0 new messages