# Conditional Variables

**URL:** <https://forum.ansible.com/t/conditional-variables/13261>\
**Category:** Ansible Project\
**Created:** [August 6, 2013, 2:51pm UTC](https://forum.ansible.com/t/conditional-variables/13261 "2013-08-06T14:51:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Drew\_Michel](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@Drew\_Michel](https://forum.ansible.com/u/Drew_Michel)\
**Post date:** [August 6, 2013, 2:51pm UTC](https://forum.ansible.com/t/conditional-variables/13261/1 "2013-08-06T14:51:20Z")

</div>

Hi All,

I just started using Ansible and I’m in the process of writing my first playbook.

I’m trying to configure HAProxy and depending on where I’m creating the instance, Vagrant vs EC2 I want to use a different network interface. eth0 vs eth1

What is the best way to tackle this situation. I want to avoid creating conditionals in the template.

Should I use external\_vars and have two separate playbooks?

Thanks,  
Drew

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [August 7, 2013, 1:55am UTC](https://forum.ansible.com/t/conditional-variables/13261/2 "2013-08-07T01:55:17Z")

</div>

I would probably consider something basic like passing in a “-e mode=vagrant” or “-e mode=ec2” on the playbook option line, and then doing

when: mode == ‘vagrant’

or

when: mode == ‘ec2’

or better yet, maybe defining this variable in your inventory file at group level.

Will something like that work, perhaps?

–Michael

---

<div class="post-metadata">

**Author:** ![Drew\_Michel](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@Drew\_Michel](https://forum.ansible.com/u/Drew_Michel)\
**Post date:** [August 8, 2013, 1:11pm UTC](https://forum.ansible.com/t/conditional-variables/13261/3 "2013-08-08T13:11:19Z")

</div>

Thanks Mike, those both sound like two good solutions.

For the time being I created two different playbook’s with var’s defined within. As I try to collapse the two playbooks I’ll try using variables within the inventory file

Drew
