Looping through a dictionary

Hi,
I have a dictionary and I need to set through it to extract data but cannot figure out how to do it.

Dictionary looks like:

`

`
“int_output”: {

`

`

“interfaces”: [
{
“ipaddress”: “unassigned”,
“method”: “NVRAM”,
“name”: “Vlan1”,
“ok”: “YES”,
“protocol”: “down”,
“status”: “administratively down”
},
{
“ipaddress”: “172.31.35.8”,
“method”: “NVRAM”,
“name”: “Vlan99”,
“ok”: “YES”,
“protocol”: “up”,
“status”: “up”
},
{
“ipaddress”: “unassigned”,
“method”: “NVRAM”,
“name”: “Vlan252”,
“ok”: “YES”,
“protocol”: “down”,
“status”: “administratively down”
}

]
}

I’ve tried using with_item and now trying with with_dict but cannot figure out how to make it work.

  • name: display output
    debug:
    var: “Name is {{ item.name }} and status is {{ item.status }}”
    with_dict: “{{ int_output.interfaces[0] }}”

The above gives:

TASK [display output] ***********************************************************************************************************
fatal: [burnside-poc-sw8.gw.mcgill.ca]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was : ‘dict object’ has no attribute ‘name’\n\nThe error appears to have been in ‘/show_int_brief.yml’: line 32, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n \n - name: display output\n ^ here\n”}

Also tried:

  • name: display output
    debug:
    var: “Name is {{ item.name }} and status is {{ item.status }}”
    with_dict: int_output.interfaces[0]

Above gives:

TASK [display output] ******************************************************************************************************************
fatal: [burnside-poc-sw8.gw.mcgill.ca]: FAILED! => {“msg”: “with_dict expects a dict”}

Any ideas good be appreciated.

int_output.interfaces is a list and you need to use with_items.

  - name: display output
    debug:
      var: "Name is {{ item.name }} and status is {{ item.status }}"
    with_items: "{{ int_output.interfaces }}"

Thanks it worked.
I get all this extra debug info. Is there a way to display only the 2nd line: “msg”: “Name is Vlan1 and status is administratively down”?

TASK [display output] ******************************************************************************************************************
ok: [burnside-poc-sw8.gw.mcgill.ca] => (item={‘status’: u’administratively down’, ‘protocol’: u’down’, ‘name’: u’Vlan1’, ‘ok’: u’YES’, ‘ipaddress’: u’unassigned’, ‘method’: u’NVRAM’}) => {
“msg”: “Name is Vlan1 and status is administratively down”
}
ok: [burnside-poc-sw8.gw.mcgill.ca] => (item={‘status’: u’up’, ‘protocol’: u’up’, ‘name’: u’Vlan99’, ‘ok’: u’YES’, ‘ipaddress’: u’172.31.35.8’, ‘method’: u’NVRAM’}) => {
“msg”: “Name is Vlan99 and status is up”
}
ok: [burnside-poc-sw8.gw.mcgill.ca] => (item={‘status’: u’administratively down’, ‘protocol’: u’down’, ‘name’: u’Vlan252’, ‘ok’: u’YES’, ‘ipaddress’: u’unassigned’, ‘method’: u’NVRAM’}) => {
“msg”: “Name is Vlan252 and status is administratively down”
}

Sure, but it depend on what of the information make you want to have that line.
Is it because the name is Vlan1 or is it because the status is administratively down or the combination?

The fist element in the list is int_output.interfaces.0 and the second is int_output.interfaces.1 and the third is int_output.interfaces.2, but you can choose which element based on it values, so it depends on your requirement.

As Kia Stan Olstad already said, you definitely need to be using with_items, because int_output.interfaces is a list (according to the information you have supplied).

To make sure that you are referencing the correct variable, add a debug statement immediately before your "display output’ task that just outputs int_output:

  • debug:
    var: int_output

The error you are seeing always means that the variable you are using does not contain what you think it contains, that you are using the wrong variable, or that you are incorrectly dereferencing the contents of the variable. The above debug statement should help you decide which.

Regards, K.