# How to choose the var list based on a condition

**URL:** <https://forum.ansible.com/t/how-to-choose-the-var-list-based-on-a-condition/30310>\
**Category:** Ansible Project\
**Created:** [May 11, 2019, 6:40pm UTC](https://forum.ansible.com/t/how-to-choose-the-var-list-based-on-a-condition/30310 "2019-05-11T18:40:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saranya\_N](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Saranya\_N](https://forum.ansible.com/u/Saranya_N)\
**Post date:** [May 11, 2019, 6:40pm UTC](https://forum.ansible.com/t/how-to-choose-the-var-list-based-on-a-condition/30310/1 "2019-05-11T18:40:28Z")

</div>

I have a list of dictionary variables something like below in vars file.

App01:  
B : value2  
C : value3

App02:  
&nbsp;&nbsp;C : value5  
&nbsp;&nbsp;B : value6

And so on...

When I run my playbook with -e option app=01 or app=App01  
I want to use App01 variables in with\_items in my task .

How can I achieve this?

---

<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:** [May 11, 2019, 6:49pm UTC](https://forum.ansible.com/t/how-to-choose-the-var-list-based-on-a-condition/30310/2 "2019-05-11T18:49:30Z")

</div>

> I have a list of dictionary variables something like below in vars file.
> 
> App01:  
> &nbsp;&nbsp;A : value1  
> &nbsp;&nbsp;B : value2  
> &nbsp;&nbsp;C : value3
> 
> App02:  
> &nbsp;&nbsp;&nbsp;A : value4  
> &nbsp;&nbsp;&nbsp;C : value5  
> &nbsp;&nbsp;&nbsp;B : value6
> 
> And so on...
> 
> When I run my playbook with -e option app=01

"{{ hostvars[inventory\_hostname]['App' ~ app] }}"

> or app=App01

"{{ hostvars[inventory\_hostname][app] }}"

> I want to use App01 variables in with\_items in my task .

App01 and App02 is not a list so you can't use with\_items, but you could use with\_dict instead.

---

<div class="post-metadata">

**Author:** ![Saranya\_N](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Saranya\_N](https://forum.ansible.com/u/Saranya_N)\
**Post date:** [May 12, 2019, 1:00pm UTC](https://forum.ansible.com/t/how-to-choose-the-var-list-based-on-a-condition/30310/3 "2019-05-12T13:00:36Z")

</div>

Hi Kai,

Im using roles/vars file, so how do I achieve it?

App01:  
B : value2  
C : value3

App02:  
C : value5  
B : value6

And so on…

Also passing different values now, when -e app=data01\_kerb something like this, if it has 01 , then choose App01’s dictionary ’  
if app=data02\_syn then choose App02’s dictionary…  
is this possible?  
Im passing app=data01\_try , coz Im trying to derive/set\_fact another variable from this value.

Any suggestions would be helpfull.

or any best way to achive this?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 13, 2019, 2:10pm UTC](https://forum.ansible.com/t/how-to-choose-the-var-list-based-on-a-condition/30310/4 "2019-05-13T14:10:30Z")

</div>

To decide which var to use:  
mydict: {{ '01' in app|ternary('App01', 'App02') }}

Then you can use via:  
lookup('vars', mydict)
