Variable within variable

I have the following and there may be several vlans per switch.

“cisco_ios.vlans”: {

“35”: {

“id”: “35”,
“mtu”: “1500”,
“name”: “CC_internal_subnet_35”,
“ports”: [
“GigabitEthernet1/0/1”,
“GigabitEthernet1/0/2”,
“GigabitEthernet1/0/20”
],
“status”: “active”
},
“499”: {
“id”: “499”,
“mtu”: “1500”,
“name”: “Lync_VoIP_Burnside1”,
“ports”: [
“”
],
“status”: “active”
}
}

I want to be able to reference the data in here. Example, I am looking to loop through al *.ports. I’m trying to do the following which does not work

vars:
old_vlan: 35
new_vlan: 45

  • name: Change vlan old_vlan to new_vlan for all ports in list
    debug:
    msg: “Old VLAN is {{ old_vlan }} and port {{ item }} needs to be changed”
    loop: ‘{{ cisco_ios.vlans[“{{ old_vlan }}”].ports }}’

The following work: loop: ‘{{ cisco_ios.vlans[“35”].ports }}’
but how do I substitute 35 with the variable old_vlan which contains 35?

I have the following and there may be several vlans per switch.
     "cisco_ios.vlans": {
         "35": {
             "id": "35",
             "mtu": "1500",
             "name": "CC_internal_subnet_35",
             "ports": [
                 "GigabitEthernet1/0/1",
                 "GigabitEthernet1/0/2",
                 "GigabitEthernet1/0/20"
             ],
             "status": "active"
         },
         "499": {
             "id": "499",
             "mtu": "1500",
             "name": "Lync_VoIP_Burnside1",
             "ports": [
                 ""
             ],
             "status": "active"
         }
     }

Your vlans are string because of the quotes around 35 and 499.

I want to be able to reference the data in here. Example, I am looking to
loop through al *.ports. I'm trying to do the following which does not work

  vars:
    old_vlan: 35
    new_vlan: 45

You are here making variables with int.

   - name: Change vlan old_vlan to new_vlan for all ports in list
     debug:
       msg: "Old VLAN is {{ old_vlan }} and port {{ item }} needs to be
changed"
     loop: '{{ cisco_ios.vlans["{{ old_vlan }}"].ports }}'

You can't have nested {{ }}, if you are in template mode you are in template mode so just loose the inner {{ }}.
But since your data is a string and you variable is a int you would need to filter you int's to a string

  loop: '{{ cisco_ios.vlans[old_vlan | string].ports }}'

Or change old_vlan to a strings and do

  loop: '{{ cisco_ios.vlans[old_vlan].ports }}'