# Ansible Nested Loop Question

**URL:** <https://forum.ansible.com/t/ansible-nested-loop-question/35418>\
**Category:** Ansible Project\
**Created:** [June 26, 2021, 2:55pm UTC](https://forum.ansible.com/t/ansible-nested-loop-question/35418 "2021-06-26T14:55:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![L\_Jankok](https://avatars.discourse-cdn.com/v4/letter/l/90db22/32.png) [@L\_Jankok](https://forum.ansible.com/u/L_Jankok)\
**Post date:** [June 26, 2021, 2:55pm UTC](https://forum.ansible.com/t/ansible-nested-loop-question/35418/1 "2021-06-26T14:55:03Z")

</div>

Hi there,

I am using the following tasks to iterate over all policy-maps (outer loop) and I would also like to iterate over all class-maps into each policy-map (inner-loop)

- name: Gather all policy-maps

ansible.utils.cli\_parse:

command: show policy-map

parser:

name: ansible.netcommon.pyats

set\_fact: qos

- name: Print all policy-maps

ansible.builtin.debug:

msg: “no policy-map {{ item.key }}”

msg: “no class-map {{ item.xxxx }}”. \<— I also want to iterate over the class-maps

loop: “{{ qos.policy\_map|dict2items }}”

After applying the dict2items the following is produced.

ok: [cat3850] =\> (item={‘key’: ‘policy3’, ‘value’: {‘class’: {‘example3’: {}, ‘example4’: {‘set’: ‘dscp af12’}, ‘class-default’: {‘set’: ‘ip dscp af12’}}}}) =\> {

“ansible\_loop\_var”: “item”,

“changed”: false,

“item”: {

“key”: “policy3”, \<— right now iterating over this works

“value”: {

“class”: {. \<— I also want to iterate over the class

“class-default”: { \<— I want to match only the class name

“set”: "ip dscp af12”. \<— I don’t want to match this

},

“example3”: {}, \<— And also match this class name

“example4”: {. \<— And also match this class name and so on.

“set”: "dscp af12”. \<— I don’t want to match this

}

}

}

},

“msg”: "no policy-map policy3”. \<— this is want I want for each class name too

}

Thank you,

Lucius

---

<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:** [June 26, 2021, 6:27pm UTC](https://forum.ansible.com/t/ansible-nested-loop-question/35418/2 "2021-06-26T18:27:52Z")

</div>

What I usually do in these cases is including a separate task file for  
the "item" at hand.

Pseudo code:

main.yml:

- Do stuff  
- include\_tasks: policymap.yml  
&nbsp;&nbsp;loop: "{{ policymaps }}"  
&nbsp;&nbsp;loop\_control:  
&nbsp;&nbsp;&nbsp;&nbsp;loop\_var: policymap

policymap.yml:

- #other task for the policymap  
- include\_tasks: classmap.yml  
&nbsp;&nbsp;loop: "{{ policymap.classmaps }}"  
&nbsp;&nbsp;loop\_control:  
&nbsp;&nbsp;&nbsp;&nbsp;loop\_var: classmap

classmap.yml:  
- do stuff with "{{ classmap }}"...

You can descend down into as many nested levels as needed by adding a  
task file for each level.  
It may seem a bit clunky at first, but I find it quite clear to  
understand as it's all very explicit and the files themselves are  
small.

Dick

---

<div class="post-metadata">

**Author:** ![L\_Jankok](https://avatars.discourse-cdn.com/v4/letter/l/90db22/32.png) [@L\_Jankok](https://forum.ansible.com/u/L_Jankok)\
**Post date:** [June 27, 2021, 11:13am UTC](https://forum.ansible.com/t/ansible-nested-loop-question/35418/3 "2021-06-27T11:13:42Z")

</div>

Yes, modularising is best practice, thanks for the tip.
