# Pick a variable based on if another variable is set

**URL:** <https://forum.ansible.com/t/pick-a-variable-based-on-if-another-variable-is-set/30179>\
**Category:** Ansible Project\
**Created:** [April 24, 2019, 7:02pm UTC](https://forum.ansible.com/t/pick-a-variable-based-on-if-another-variable-is-set/30179 "2019-04-24T19:02:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![IP\_Freely](https://avatars.discourse-cdn.com/v4/letter/i/e19adc/32.png) [@IP\_Freely](https://forum.ansible.com/u/IP_Freely)\
**Post date:** [April 24, 2019, 7:02pm UTC](https://forum.ansible.com/t/pick-a-variable-based-on-if-another-variable-is-set/30179/1 "2019-04-24T19:02:49Z")

</div>

I want to add a var of testing to my playbook, and when that var is set to true i want var1 to be used but if its nor set to true, i want it to use var2. Is this possible?

below is a non working example but I hope it illustrates what i want it to do.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 24, 2019, 7:40pm UTC](https://forum.ansible.com/t/pick-a-variable-based-on-if-another-variable-is-set/30179/2 "2019-04-24T19:40:12Z")

</div>

You are already in template mode so you can't use {{ }} inside {% %}, so delete the {{ }} and it will work

{% if testing == 'true' %}{{ slack\_slacktesting }}{% else %}{{ slack\_devops }}{% endif %}

There are some shorter version you could use:

{{ slack\_slacktesting if testing == 'true' else slack\_devops }}

or

{{ (testing == 'true') | ternary(slack\_slacktesting, slack\_devops) }}
