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

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

Hi Yassen,

You have to use the hostvars dictionary, like this:

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

Anand: you nailed it, works great! Thank you!
-Y.