title: How to iterate through subelements of complicated ansible JSON variable
body: <p>I want to use ansible task, to create subnets in azure, I have following playbook</p>
  <pre><code>---
  - hosts: localhost
    vars:
      &quot;vnets&quot;: [
        {
          &quot;vnet_aks_re1&quot;: [
            {
              &quot;region&quot;: &quot;region1&quot;,
              &quot;resource_group_key&quot;: &quot;aks_spoke_re1&quot;,
              &quot;specialsubnets&quot;: [
                {
                  &quot;AzureFirewallSubnet&quot;: [
                    {
                      &quot;cidr&quot;: [
                        &quot;10.100.83.128/26&quot;
                      ],
                      &quot;name&quot;: &quot;AzureFirewallSubnet&quot;
                    }
                  ],
                  &quot;GatewaySubnet&quot;: [
                    {
                      &quot;cidr&quot;: [
                        &quot;10.100.83.224/27&quot;
                      ],
                      &quot;name&quot;: &quot;GatewaySubnet&quot;
                    }
                  ]
                }
              ],
              &quot;subnets&quot;: [
                {
                  &quot;AzureBastionSubnet&quot;: [
                    {
                      &quot;cidr&quot;: [
                        &quot;10.100.83.32/27&quot;
                      ],
                      &quot;name&quot;: &quot;AzureBastionSubnet&quot;,
                      &quot;nsg_key&quot;: &quot;azure_bastion_nsg&quot;
                    }
                  ],
                  &quot;aks_ingress&quot;: [
                    {
                      &quot;cidr&quot;: [
                        &quot;10.100.82.0/24&quot;
                      ],
                      &quot;name&quot;: &quot;aks_ingress&quot;,
                      &quot;nsg_key&quot;: &quot;azure_kubernetes_cluster_nsg&quot;,
                      &quot;route_table_key&quot;: &quot;default_to_firewall_re1&quot;
                    }
                  ],
                  &quot;aks_nodepool_system&quot;: [
                    {
                      &quot;cidr&quot;: [
                        &quot;10.100.80.0/24&quot;
                      ],
                      &quot;name&quot;: &quot;aks_nodepool_system&quot;,
                      &quot;nsg_key&quot;: &quot;azure_kubernetes_cluster_nsg&quot;,
                      &quot;route_table_key&quot;: &quot;default_to_firewall_re1&quot;
                    }
                  ],
                  &quot;aks_nodepool_user1&quot;: [
                    {
                      &quot;cidr&quot;: [
                        &quot;10.100.81.0/24&quot;
                      ],
                      &quot;name&quot;: &quot;aks_nodepool_user1&quot;,
                      &quot;nsg_key&quot;: &quot;azure_kubernetes_cluster_nsg&quot;,
                      &quot;route_table_key&quot;: &quot;default_to_firewall_re1&quot;
                    }
                  ],
                  &quot;application_gateway&quot;: [
                    {
                      &quot;cidr&quot;: [
                        &quot;10.100.83.96/27&quot;
                      ],
                      &quot;name&quot;: &quot;agw&quot;,
                      &quot;nsg_key&quot;: &quot;application_gateway&quot;
                    }
                  ],
                  &quot;jumpbox&quot;: [
                    {
                      &quot;cidr&quot;: [
                        &quot;10.100.83.64/28&quot;
                      ],
                      &quot;name&quot;: &quot;jumpbox&quot;,
                      &quot;nsg_key&quot;: &quot;azure_kubernetes_cluster_nsg&quot;,
                      &quot;route_table_key&quot;: &quot;default_to_firewall_re1&quot;
                    }
                  ],
                  &quot;private_endpoints&quot;: [
                    {
                      &quot;cidr&quot;: [
                        &quot;10.100.83.0/27&quot;
                      ],
                      &quot;enforce_private_link_endpoint_network_policies&quot;: true,
                      &quot;name&quot;: &quot;private_endpoints&quot;
                    }
                  ]
                }
              ],
              &quot;vnet&quot;: [
                {
                  &quot;address_space&quot;: [
                    &quot;10.100.80.0/22&quot;
                  ],
                  &quot;name&quot;: &quot;aks&quot;
                }
              ]
            }
          ],
          &quot;vnet_hub_re1&quot;: [
            {
              &quot;region&quot;: &quot;region1&quot;,
              &quot;resource_group_key&quot;: &quot;vnet_hub_re1&quot;,
              &quot;specialsubnets&quot;: [
                {
                  &quot;AzureFirewallSubnet&quot;: [
                    {
                      &quot;cidr&quot;: [
                        &quot;100.64.101.0/26&quot;
                      ],
                      &quot;name&quot;: &quot;AzureFirewallSubnet&quot;
                    }
                  ],
                  &quot;GatewaySubnet&quot;: [
                    {
                      &quot;cidr&quot;: [
                        &quot;100.64.100.0/27&quot;
                      ],
                      &quot;name&quot;: &quot;GatewaySubnet&quot;
                    }
                  ]
                }
              ],
              &quot;subnets&quot;: [
                {
                  &quot;AzureBastionSubnet&quot;: [
                    {
                      &quot;cidr&quot;: [
                        &quot;100.64.101.64/26&quot;
                      ],
                      &quot;name&quot;: &quot;AzureBastionSubnet&quot;,
                      &quot;nsg_key&quot;: &quot;azure_bastion_nsg&quot;
                    }
                  ],
                  &quot;jumpbox&quot;: [
                    {
                      &quot;cidr&quot;: [
                        &quot;100.64.102.0/27&quot;
                      ],
                      &quot;name&quot;: &quot;jumpbox&quot;,
                      &quot;nsg_key&quot;: &quot;jumpbox&quot;
                    }
                  ],
                  &quot;private_endpoints&quot;: [
                    {
                      &quot;cidr&quot;: [
                        &quot;100.64.103.128/25&quot;
                      ],
                      &quot;enforce_private_link_endpoint_network_policies&quot;: true,
                      &quot;name&quot;: &quot;private_endpoints&quot;
                    }
                  ]
                }
              ],
              &quot;vnet&quot;: [
                {
                  &quot;address_space&quot;: [
                    &quot;100.64.100.0/22&quot;
                  ],
                  &quot;name&quot;: &quot;vnet_hub_re1&quot;
                }
              ]
            }
          ]
        }
    ]
    tasks:
      - name: Dbg variable
        debug:
          msg: |
            Debug:
            --------------------------------
            Items:             {{ item }}
            ================================
        with_items:
          - &quot;{{ vnets[0] | dict2items | json_query('[].value|[*]|[].subnets')  }}&quot;
  
      # this would be expected result
      - name: Create a subnet
        azure_rm_subnet:
          resource_group: &quot;{{ item.resource_group_key }}&quot;
          virtual_network_name: &quot;{{ item.vnet[0].name }}&quot;
          name: &quot;{{ combine(item.subnets[*].name,  item.specialsubnets[*].name }}&quot;
          address_prefix_cidr: &quot;{{ combine(item.subnets[*].cidr,  item.specialsubnets[*].cidr }}&quot;
        with_items:
          - &quot;{{ vnets[0] | dict2items | json_query('[].value|[*]')  }}&quot;
  
  </code></pre>
  <p>In JSON variable vnet and subnet keys can have any value, so I can not access them by name, but use * to iterate through all of them. Meanwhile subnets, vnets and specialsubnets are fixed names.</p>
  <p>To create and azure subnet I need not only content of subnets and specialsubnets, but also region and resource_group_key from the same level.</p>
  <p>I could not find a way how to iterate through all elements of subnets and specialsubnets, using with_items, there are always two items printed, no meter what I put in json_query.</p>
  <p>Expected result would be that loop creates 13 subnets</p>
  <pre><code># Hub  VNET with 5 subnets
  - name: Create a subnet
    azure_rm_subnet:
      resource_group: &quot;vnet_hub_re1&quot;
      virtual_network_name: &quot;vnet_hub_re1&quot;
      name: &quot;GatewaySubnet&quot;
      address_prefix_cidr: &quot;[&quot;100.64.100.0/27&quot;]&quot;
  
  - name: Create a subnet
    azure_rm_subnet:
      resource_group: &quot;vnet_hub_re1&quot;
      virtual_network_name: &quot;vnet_hub_re1&quot;
      name: &quot;AzureFirewallSubnet&quot;
      address_prefix_cidr: &quot;[&quot;100.64.101.0/26&quot;]&quot;
  
  - name: Create a subnet
    azure_rm_subnet:
      resource_group: &quot;vnet_hub_re1&quot;
      virtual_network_name: &quot;vnet_hub_re1&quot;
      name: &quot;AzureBastionSubnet&quot;
      address_prefix_cidr: &quot;[&quot;100.64.101.64/26&quot;]&quot;
  
  - name: Create a subnet
    azure_rm_subnet:
      resource_group: &quot;vnet_hub_re1&quot;
      virtual_network_name: &quot;vnet_hub_re1&quot;
      name: &quot;jumpbox&quot;
      address_prefix_cidr: &quot;[&quot;100.64.102.0/27&quot;]&quot;
  
  - name: Create a subnet
    azure_rm_subnet:
      resource_group: &quot;vnet_hub_re1&quot;
      virtual_network_name: &quot;vnet_hub_re1&quot;
      name: &quot;private_endpoints&quot;
      address_prefix_cidr: &quot;[&quot;100.64.103.128/25&quot;]&quot;
  
  # Spoke vnet with 8 subnets
  
  - name: Create a subnet
    azure_rm_subnet:
      resource_group: &quot;aks_spoke_re1&quot;
      virtual_network_name: &quot;aks&quot;
      name: &quot;AzureFirewallSubnet&quot;
      address_prefix_cidr: &quot;[&quot;10.100.83.128/26&quot;]&quot;
  
  - name: Create a subnet
    azure_rm_subnet:
      resource_group: &quot;aks_spoke_re1&quot;
      virtual_network_name: &quot;aks&quot;
      name: &quot;GatewaySubnet&quot;
      address_prefix_cidr: &quot;[&quot;10.100.83.224/27&quot;]&quot;
  
  - name: Create a subnet
    azure_rm_subnet:
      resource_group: &quot;aks_spoke_re1&quot;
      virtual_network_name: &quot;aks&quot;
      name: &quot;AzureBastionSubnet&quot;
      address_prefix_cidr: &quot;[&quot;10.100.83.32/27&quot;]&quot;
  
  - name: Create a subnet
    azure_rm_subnet:
      resource_group: &quot;aks_spoke_re1&quot;
      virtual_network_name: &quot;aks&quot;
      name: &quot;aks_ingress&quot;
      address_prefix_cidr: &quot;[&quot;10.100.82.0/24&quot;]&quot;
  
  - name: Create a subnet
    azure_rm_subnet:
      resource_group: &quot;aks_spoke_re1&quot;
      virtual_network_name: &quot;aks&quot;
      name: &quot;aks_nodepool_system&quot;
      address_prefix_cidr: &quot;[&quot;10.100.80.0/24&quot;]&quot;
  
  - name: Create a subnet
    azure_rm_subnet:
      resource_group: &quot;aks_spoke_re1&quot;
      virtual_network_name: &quot;aks&quot;
      name: &quot;aks_nodepool_user1&quot;
      address_prefix_cidr: &quot;[&quot;10.100.81.0/24&quot;]&quot;
  
  - name: Create a subnet
    azure_rm_subnet:
      resource_group: &quot;aks_spoke_re1&quot;
      virtual_network_name: &quot;aks&quot;
      name: &quot;agw&quot;
      address_prefix_cidr: &quot;[&quot;10.100.83.96/27&quot;]&quot;
  
  - name: Create a subnet
    azure_rm_subnet:
      resource_group: &quot;aks_spoke_re1&quot;
      virtual_network_name: &quot;aks&quot;
      name: &quot;jumpbox&quot;
      address_prefix_cidr: &quot;[&quot;10.100.83.64/28&quot;]&quot;
  
  - name: Create a subnet
    azure_rm_subnet:
      resource_group: &quot;aks_spoke_re1&quot;
      virtual_network_name: &quot;aks&quot;
      name: &quot;private_endpoints&quot;
      address_prefix_cidr: &quot;[&quot;10.100.83.0/27&quot;]&quot;
  </code></pre>