# loop help

**URL:** <https://forum.ansible.com/t/loop-help/38667>\
**Category:** Ansible Project\
**Created:** [January 29, 2024, 4:29pm UTC](https://forum.ansible.com/t/loop-help/38667 "2024-01-29T16:29:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [January 29, 2024, 4:29pm UTC](https://forum.ansible.com/t/loop-help/38667/1 "2024-01-29T16:29:58Z")

</div>

can i get assistance with creating a loop for creating both A and B side.

- name: “Configure Domain VLAN Policy A”  
vars:  
api\_info: &api\_info  
api\_private\_key: “{{ api\_private\_key }}”  
api\_key\_id: “{{ api\_key\_id }}”  
api\_uri: “{{ api\_uri | default(omit) }}”  
validate\_certs: “{{ validate\_certs | default(omit) }}”  
state: “{{ state | default(omit) }}”

cisco.intersight.intersight\_rest\_api:  
\<\<: \*api\_info  
resource\_path: /fabric/EthNetworkPolicies  
query\_params:  
$filter: “Name eq ‘{{ name\_of\_vlan\_domain\_policy\_A }}’”  
api\_body:  
Name: “{{ name\_of\_vlan\_domain\_policy\_A }}”  
Description: “{{ description\_of\_vlan\_domain\_policy\_A }}”  
Organization:  
Moid: “{{ intersight\_org.api\_response.Moid }}”  
Tags:

- Key: “configmode”  
Value: “ansible”

- Key: “prefix”  
Value: “”  
register: domain\_vlan\_policy\_A

- name: “Configure Domain VLAN Policy B”  
cisco.intersight.intersight\_rest\_api:  
\<\<: \*api\_info  
resource\_path: /fabric/EthNetworkPolicies  
query\_params:  
$filter: “Name eq ‘{{ name\_of\_vlan\_domain\_policy\_B }}’”  
api\_body:  
Name: “{{ name\_of\_vlan\_domain\_policy\_B }}”  
Description: “{{ description\_of\_vlan\_domain\_policy\_B }}”  
Organization:  
Moid: “{{ intersight\_org.api\_response.Moid }}”  
Tags:

- Key: “configmode”  
Value: “ansible”

- Key: “prefix”  
Value: “”  
register: domain\_vlan\_policy\_B

- name: Print return information from the previous tasks  
ansible.builtin.debug:  
var: item.api\_response.Moid  
loop:

- “{{ domain\_vlan\_policy\_A }}”

- “{{ domain\_vlan\_policy\_B }}”

# Get Moid for domain\_vlan\_policy\_A

- name: “Get Moid for Domain VLAN Policy A”  
cisco.intersight.intersight\_rest\_api:  
\<\<: \*api\_info  
resource\_path: /fabric/EthNetworkPolicies  
query\_params:  
$filter: “Name eq ‘{{ name\_of\_vlan\_domain\_policy\_A }}’”  
register: domain\_vlan\_policy\_A

# Create VLANs for Domain VLAN Policy A

- name: “Create VLANs for Domain VLAN Policy A”  
cisco.intersight.intersight\_rest\_api:  
\<\<: \*api\_info  
resource\_path: “/bulk/Requests”  
api\_body: |  
{  
“Verb”: “POST”,  
“Uri”: “/v1/fabric/Vlans”,  
“Requests”: [  
{% for vlan in vlan\_configs\_A %}  
{  
“ObjectType”: “bulk.RestSubRequest”,  
“Body”: {  
“Name”: “{{ [vlan.name](http://vlan.name) }}”,  
“VlanId”: {{ vlan.vlan\_id }},  
“SharingType”: “None”,  
“PrimaryVlanId”: 0,  
“MulticastPolicy”: “{{ multicast\_policy.api\_response.Moid }}”,  
“AutoAllowOnUplinks”: false,  
“IsNative”: false,  
“EthNetworkPolicy”: “{{ domain\_vlan\_policy\_A.api\_response.Moid }}”  
}  
}{% if not loop.last %},{% endif %}  
{% endfor %}  
]  
}  
register: bulk\_vlan\_info\_A

- name: Print return information for VLANs created with Domain VLAN Policy A  
ansible.builtin.debug:  
var: bulk\_vlan\_info\_A.api\_response

# Get Moid for domain\_vlan\_policy\_B

- name: “Get Moid for Domain VLAN Policy B”  
cisco.intersight.intersight\_rest\_api:  
\<\<: \*api\_info  
resource\_path: /fabric/EthNetworkPolicies  
query\_params:  
$filter: “Name eq ‘{{ name\_of\_vlan\_domain\_policy\_B }}’”  
register: domain\_vlan\_policy\_B

# Create VLANs for Domain VLAN Policy B

- name: “Create VLANs for Domain VLAN Policy B”  
cisco.intersight.intersight\_rest\_api:  
\<\<: \*api\_info  
resource\_path: “/bulk/Requests”  
api\_body: |  
{  
“Verb”: “POST”,  
“Uri”: “/v1/fabric/Vlans”,  
“Requests”: [  
{% for vlan in vlan\_configs\_B %}  
{  
“ObjectType”: “bulk.RestSubRequest”,  
“Body”: {  
“Name”: “{{ [vlan.name](http://vlan.name) }}”,  
“VlanId”: {{ vlan.vlan\_id }},  
“SharingType”: “None”,  
“PrimaryVlanId”: 0,  
“MulticastPolicy”: “{{ multicast\_policy.api\_response.Moid }}”,  
“AutoAllowOnUplinks”: false,  
“IsNative”: false,  
“EthNetworkPolicy”: “{{ domain\_vlan\_policy\_B.api\_response.Moid }}”  
}  
}{% if not loop.last %},{% endif %}  
{% endfor %}  
]  
}  
register: bulk\_vlan\_info\_B

- name: Print return information for VLANs created with Domain VLAN Policy B  
ansible.builtin.debug:  
var: bulk\_vlan\_info\_B.api\_response

# Continue with other tasks as needed…

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [January 29, 2024, 4:57pm UTC](https://forum.ansible.com/t/loop-help/38667/2 "2024-01-29T16:57:44Z")

</div>

You’ve got to fix a couple of things first.

- You’re re-registering domain\_vlan\_policy\_A and domain\_vlan\_policy\_B, which looks like a mistake to me. Are you using the same variable name because you know you won’t need the old values again, or is that really an error? I’m asking because it may affect how you set up your loops.
- Your template is looping over vlan\_configs\_A / vlan\_configs\_B, but I don’t see where those variables are being set.  
Ignoring that, the basic idea is you’d put a loop: on an include\_tasks: or import\_tasks: tasks with a vars: section that sets the domain policy name and description, and do away with all the “\_A” and “\_B” in the included file’s tasks. That included file would be basically what you posted, but with the issues above addressed.

```auto
- name: Include configure domain policy tasks
  ansible.builtin.include:
    file: what_you_posted_originally_almost.yml
  vars:
    domain_policy_name: "{{ item.name }}"
    domain_policy_desc: "{{ item.desc }}"
  loop:
    - {name: '{{ name_of_vlan_comain_policy_A }}',
       desc: '{{ description_of_vlan_domain_policy_A }}'}
    - {name: '{{ name_of_vlan_comain_policy_B }}',
       desc: '{{ description_of_vlan_domain_policy_B }}'}

```

That’s nearly close enough to get you started I think.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [January 29, 2024, 5:37pm UTC](https://forum.ansible.com/t/loop-help/38667/3 "2024-01-29T17:37:32Z")

</div>

Of course that should be “ansible.builtin.include\_tasks:”, not “ansible.builtin.include:”.
