loop help

35 views
Skip to first unread message

Tony Wong

unread,
Jan 29, 2024, 11:30:36 AM1/29/24
to ansible...@googlegroups.com
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 }}",
"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 }}",
"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...

Todd Lewis

unread,
Jan 29, 2024, 11:58:02 AM1/29/24
to ansible...@googlegroups.com, uto...@gmail.com
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.
- 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.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CALmkhkqs%3DENRapvNm%2BXSos2NBORVg4KnmzB%3DZ89OK0kiLwjRDg%40mail.gmail.com.

-- 
Todd

Todd Lewis

unread,
Jan 29, 2024, 12:37:32 PM1/29/24
to Ansible Project
Of course that should be "ansible.builtin.include_tasks:", not "ansible.builtin.include:".
Reply all
Reply to author
Forward
0 new messages