How to choose between ansible_eth0 and ansible_eth1 depending on a parameter?

103 views
Skip to first unread message

Yassen Damyanov

unread,
Jun 18, 2015, 5:49:17 AM6/18/15
to ansible...@googlegroups.com
My firewall-oriented roles need to use different network info when executed on different machines, depending on which interface is the external one and which is the internal one. Thus I need to be able to set e.g.:

ext_if: eth0
int_if
: eth1

and from here, ip network information to be taken from ansible_eth0 for the external interface and from ansible_eth1 for the internal. And if on another machine they are different, e.g. eth1 is the external and eth2 is the internal, I would only need to change the above to

ext_if: eth1
int_if
: eth2

In shell syntax that would be like ansible_${ext_if} / ansible_${int_if} ...

How would you guys do that with ansible?

Any input much appreciated, thanks in advance!

-Yassen

Anand Buddhdev

unread,
Jun 18, 2015, 1:02:20 PM6/18/15
to ansible...@googlegroups.com
Hi Yassen,

You have to use the hostvars dictionary, like this:

{{ hostvars[inventory_hostname]['ansible_'+ext_if].ipv4.address }}

Yassen Damyanov

unread,
Jun 18, 2015, 2:36:03 PM6/18/15
to ansible...@googlegroups.com


Anand: you nailed it, works great! Thank you!
-Y.
Reply all
Reply to author
Forward
0 new messages