# How to choose between ansible\_eth0 and ansible\_eth1 depending on a parameter?

**URL:** <https://forum.ansible.com/t/how-to-choose-between-ansible-eth0-and-ansible-eth1-depending-on-a-parameter/18537>\
**Category:** Ansible Project\
**Created:** [June 18, 2015, 9:49am UTC](https://forum.ansible.com/t/how-to-choose-between-ansible-eth0-and-ansible-eth1-depending-on-a-parameter/18537 "2015-06-18T09:49:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yassen\_Damyanov](https://avatars.discourse-cdn.com/v4/letter/y/9fc29f/32.png) [@Yassen\_Damyanov](https://forum.ansible.com/u/Yassen_Damyanov)\
**Post date:** [June 18, 2015, 9:49am UTC](https://forum.ansible.com/t/how-to-choose-between-ansible-eth0-and-ansible-eth1-depending-on-a-parameter/18537/1 "2015-06-18T09:49:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@Anand\_Buddhdev](https://forum.ansible.com/u/Anand_Buddhdev)\
**Post date:** [June 18, 2015, 4:56pm UTC](https://forum.ansible.com/t/how-to-choose-between-ansible-eth0-and-ansible-eth1-depending-on-a-parameter/18537/2 "2015-06-18T16:56:26Z")

</div>

Hi Yassen,

You have to use the hostvars dictionary, like this:

{{ hostvars[inventory\_hostname][‘ansible\_’+ext\_if].ipv4.address }}

---

<div class="post-metadata">

**Author:** ![Yassen\_Damyanov](https://avatars.discourse-cdn.com/v4/letter/y/9fc29f/32.png) [@Yassen\_Damyanov](https://forum.ansible.com/u/Yassen_Damyanov)\
**Post date:** [June 18, 2015, 6:36pm UTC](https://forum.ansible.com/t/how-to-choose-between-ansible-eth0-and-ansible-eth1-depending-on-a-parameter/18537/3 "2015-06-18T18:36:03Z")

</div>

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