# 2 levels nested loop with variables

**URL:** <https://forum.ansible.com/t/2-levels-nested-loop-with-variables/33598>\
**Category:** Ansible Project\
**Created:** [August 13, 2020, 12:27am UTC](https://forum.ansible.com/t/2-levels-nested-loop-with-variables/33598 "2020-08-13T00:27:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nic\_H](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nic\_H](https://forum.ansible.com/u/Nic_H)\
**Post date:** [August 13, 2020, 12:27am UTC](https://forum.ansible.com/t/2-levels-nested-loop-with-variables/33598/1 "2020-08-13T00:27:22Z")

</div>

Hi, I have been looking into _nested_ , _subelements_ and other _lookups_ with no luck to make this working , using ansible 2.8.  
Basically , I’d like to construct a loop than can itself iterate over other variables (list or dict).

**iam\_managed\_policy:**  
**policy\_name: “{{ item.name }}”**  
**policy: “{{ item.policy }}”**  
**state: present**  
**loop:**  
**- { name: “rds-{{mysql\_privilege}}-{{regions}}”, policy: “{{ lookup(‘template’, template.json.j2’) }}” }**  
**vars:**  
**mysql\_privilege:**  
**- rw**  
**- ro**  
**regions:**  
**- us-west-1**  
**- us-west-2**

but it end up with such result:  
“Couldn’t create policy rds-prod-[u’rw’, u’ro’] - [u’ca-central-1’, u’us-west-1’]”

What i d like is each loop would build a 2 dimensions array with the vars elements ( rw-us-west-1 , rw-us-west-2, ro-us-west-1, ro-us-west-2)

Is that even possible to achieve with ansible ?  
thanks

---

<div class="post-metadata">

**Author:** ![Nic\_H](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nic\_H](https://forum.ansible.com/u/Nic_H)\
**Post date:** [August 13, 2020, 6:00pm UTC](https://forum.ansible.com/t/2-levels-nested-loop-with-variables/33598/2 "2020-08-13T18:00:58Z")

</div>

so, I have been trying with lookup(‘dict’) but still not quite close to the expected result  
I get: " An unhandled exception occurred while running the lookup plugin ‘dict’. Error was a \<class ‘ansible.errors.AnsibleError’\>, original message: with\_dict expects a dict"  
her is the last code :

iam\_managed\_policy:  
policy\_name: “{{ item.name }}”  
policy: “{{ item.policy }}”  
state: present  
loop:

- { name: "rds-{{ lookup(‘dict’, mysql\_privilege[0].value ) }} ", policy: “{{ lookup(‘template’, ‘template.json.j2’) }}” }

# - { name: "rds-{{ lookup(‘nested’, mysql\_privilege[role] ) }} ", policy: “{{ lookup(‘template’, ‘template.json.j2’) }}” }

vars:  
mysql\_privilege:  
role:

- rw
- ro  
regions:
- us-west-1
- us-west-2

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [August 13, 2020, 6:38pm UTC](https://forum.ansible.com/t/2-levels-nested-loop-with-variables/33598/3 "2020-08-13T18:38:45Z")

</div>

> so, I have been trying with lookup('dict') but still not quite close to the expected result  
> I get: " An unhandled exception occurred while running the lookup plugin 'dict'. Error was a \<class  
> 'ansible.errors.AnsibleError'\>, original message: with\_dict expects a dict"  
> her is the last code :
> 
> iam\_managed\_policy:  
> policy\_name: "{{ item.name }}"  
> policy: "{{ item.policy }}"  
> state: present  
> loop:  
> - { name: "rds-{{ lookup('dict', mysql\_privilege[0].value ) }} ", policy: "{{ lookup('template', 'template.json.j2') }}" }  
> # - { name: "rds-{{ lookup('nested', mysql\_privilege[role] ) }} ", policy: "{{ lookup('template', 'template.json.j2') }}" }  
> vars:  
> mysql\_privilege:  
> role:  
> - rw  
> - ro  
> regions:  
> - us-west-1  
> - us-west-2

What are the possible values for policy\_name? The value for the "policy" seems to the same lookup so you don't need in  
the loop.

I'm pretty sure that you are running into brick walls for a possibly simple problem.

So please explain / give examples how the policy\_name is constructed.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Nic\_H](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nic\_H](https://forum.ansible.com/u/Nic_H)\
**Post date:** [August 17, 2020, 6:52pm UTC](https://forum.ansible.com/t/2-levels-nested-loop-with-variables/33598/4 "2020-08-17T18:52:14Z")

</div>

Thanks Racke, I should have had some context. policy\_name can vary, actually in the real task I have other policies with other names, hence the use of a loop for [item.name](http://item.name/) and item.policy .  
Just this specific one named rds\_foo\_bar needs the nested arguments from my vars list(or dict I am not sure) .  
Although I can create a dedicate task just for that policy if that is too complicated, I just thought I could do some nested loop over variables.  
Interesting facts, with nested it actually loop over each character in the key:value  
_Couldn’t create policy rds-dev-[[u’r’, u’e’, u’g’, u’i’, u’o’, u’n’, u’s’], [u’r’, u’o’, u’l’, u’e’]]_

the actual play looks like:

iam\_managed\_policy:  
policy\_name: “{{ [item.name](http://item.name/) }}”  
policy: “{{ item.policy }}”  
state: present  
loop:

- { name: "rds-prod-{{ lookup(‘dict’, mysql\_privilege[0].value ) }} ", policy: “{{ lookup(‘template’, rds-prod.json.j2) }}” }

# - { name: “rds-dev-{{ lookup(‘nested’,mysql\_privilege )”, policy: “{{ lookup(‘template’, rds\_dev.json.j2) }}” }

# - { name: cloudwatch, policy: “{{ lookup(‘template’,‘cloudwatch.json.j2’) }}” }

# other policies with name and policy

vars:  
mysql\_privilege:  
role:

- rw
- ro

rds\_region:

- us-west-1
- us-west-2

And rds-prod.json.j2 also reuses the variables from mysql\_privilege dict as:  
…

“Action”: [  
“rds-db:connect”  
],  
“Resource”: “arn:aws:rds-db:{{ rds\_region }}{{ aws\_id }}:dbuser:\*/username-{{ role }}”

…

---

<div class="post-metadata">

**Author:** ![Nic\_H](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@Nic\_H](https://forum.ansible.com/u/Nic_H)\
**Post date:** [August 17, 2020, 9:15pm UTC](https://forum.ansible.com/t/2-levels-nested-loop-with-variables/33598/5 "2020-08-17T21:15:11Z")

</div>

so basically I _just_ need to loop over extra variables in an inline way to construct the unique name and the unique policy template, then, the usual loop for the module will create the resources in aws.

- should using **Lookup** a good start for inline loop ?
- as **nested** and **subelements** can’t seem to achieve this, what other solution I can explore ?

cheers

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 17, 2020, 10:38pm UTC](https://forum.ansible.com/t/2-levels-nested-loop-with-variables/33598/6 "2020-08-17T22:38:57Z")

</div>

For several levels of loops I often split things out to separate files, which are then included with ‘include\_tasks’ in a loop.

It helps to name the files properly, ie according to their function.
