Accessing IP address of a virtual network interface

1,759 views
Skip to first unread message

Guillem Liarte

unread,
Oct 30, 2013, 6:37:56 AM10/30/13
to ansible...@googlegroups.com
Hello all,

using ansible-1.3.3-1

I have a similar problem as the one described here:

https://github.com/ansible/ansible/issues/3916

From Centos 6.4 I see:

eth0      Link encap:Ethernet  HWaddr 00:19:99:D4:35:C9 
          inet addr:94.242.252.25  Bcast:94.242.252.255  Mask:255.255.255.0
          inet6 addr: fe80::219:99ff:fed4:35c9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5012966 errors:0 dropped:0 overruns:0 frame:0
          TX packets:916710 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:744461467 (709.9 MiB)  TX bytes:85277660 (81.3 MiB)
          Interrupt:18 Memory:fb900000-fb920000

eth0:0    Link encap:Ethernet  HWaddr 00:19:99:D4:35:C9 
          inet addr:94.242.252.36  Bcast:94.242.252.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:18 Memory:fb900000-fb920000

From setup module:

    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "94.242.252.25",
            "94.242.252.36"
        ],


...

"ansible_eth0": {
            "active": true,
            "device": "eth0",
            "ipv4": {
                "address": "94.242.252.25",
                "netmask": "255.255.255.0",
                "network": "94.242.252.0"
            },

 "ansible_eth0:0": {
            "active": true,
            "device": "eth0",
            "ipv4": {
                "address": "94.242.252.36",
                "netmask": "255.255.255.0",
                "network": "94.242.252.0"
            },


In a playbook, if I get to eth0's IP address like that:

 {{ ansible_eth0['ipv4']['address'] }}    << it is OK.

But if I try to get to eth0:0's  

{{ ansible_eth0:0['ipv4']['address'] }} <<  NOT OK.

{'msg': "expected token 'variable_end', got ':'", 'failed': True}

It seems to get upset with the colon.


As an alternative way to get around this one can use:

 {{ ansible_all_ipv4_addresses[0] }}
 {{ ansible_all_ipv4_addresses[1] }}

Which in this case works as expected. While this is OK in most configurations, it may be desirable to be able to address the IP address from a certain interface in some cases, not necessarily from a list, as this may be subject to change, while interface addresses can be 'locked' to a MAC.

Is there a workaround to get to eth0:X at the moment?

Many thanks.

C. Morgan Hamill

unread,
Oct 30, 2013, 7:59:52 AM10/30/13
to ansible-project
Excerpts from Guillem Liarte's message of 2013-10-30 06:37:56 -0400:
> {{ ansible_eth0:0['ipv4']['address'] }} << NOT OK.

I believe this should work for now:

{{ hostvars[inventory_hostname]['ansible_eth0:0']['address'] }}

Roundabout, but does the job.
--
Morgan Hamill
Reply all
Reply to author
Forward
0 new messages