How to choose the var list based on a condition

17 views
Skip to first unread message

Saranya N

unread,
May 11, 2019, 2:40:28 PM5/11/19
to Ansible Project
I have a list of dictionary variables something like below in vars file.

App01:
A : value1
B : value2
C : value3

App02:
A : value4
C : value5
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?

Kai Stian Olstad

unread,
May 11, 2019, 2:49:41 PM5/11/19
to ansible...@googlegroups.com
On 11.05.2019 20:40, Saranya N wrote:
> I have a list of dictionary variables something like below in vars file.
>
> App01:
> A : value1
> B : value2
> C : value3
>
> App02:
> A : value4
> C : value5
> 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.


--
Kai Stian Olstad

Saranya N

unread,
May 12, 2019, 9:00:36 AM5/12/19
to Ansible Project
Hi Kai,

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

App01:
 A : value1
 B : value2
 C : value3

App02:
  A : value4
  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?

Brian Coca

unread,
May 13, 2019, 10:10:57 AM5/13/19
to Ansible Project
To decide which var to use:
mydict: {{ '01' in app|ternary('App01', 'App02') }}

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


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages