# Accessing IP address of a virtual network interface

**URL:** <https://forum.ansible.com/t/accessing-ip-address-of-a-virtual-network-interface/13804>\
**Category:** Ansible Project\
**Created:** [October 30, 2013, 10:37am UTC](https://forum.ansible.com/t/accessing-ip-address-of-a-virtual-network-interface/13804 "2013-10-30T10:37:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillem\_Liarte](https://avatars.discourse-cdn.com/v4/letter/g/e95f7d/32.png) [@Guillem\_Liarte](https://forum.ansible.com/u/Guillem_Liarte)\
**Post date:** [October 30, 2013, 10:37am UTC](https://forum.ansible.com/t/accessing-ip-address-of-a-virtual-network-interface/13804/1 "2013-10-30T10:37:56Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [October 30, 2013, 11:59am UTC](https://forum.ansible.com/t/accessing-ip-address-of-a-virtual-network-interface/13804/2 "2013-10-30T11:59:52Z")

</div>

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:

&nbsp;&nbsp;&nbsp;&nbsp;{{ hostvars[inventory\_hostname]['ansible\_eth0:0']['address'] }}

Roundabout, but does the job.
