Looping through a dictionary

23 views
Skip to first unread message

Spiro Mitsialis

unread,
Jan 8, 2019, 10:32:43 AM1/8/19
to Ansible Project
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.

Kai Stian Olstad

unread,
Jan 8, 2019, 11:13:04 AM1/8/19
to ansible...@googlegroups.com
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 }}"



--
Kai Stian Olstad


Spiro Mitsialis

unread,
Jan 8, 2019, 11:53:44 AM1/8/19
to Ansible Project
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"

Kai Stian Olstad

unread,
Jan 8, 2019, 3:36:05 PM1/8/19
to ansible...@googlegroups.com
On Tuesday, 8 January 2019 17:53:44 CET Spiro Mitsialis wrote:
> 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"?

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.


--
Kai Stian Olstad


Karl Auer

unread,
Jan 8, 2019, 4:30:52 PM1/8/19
to ansible-project
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.

--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d2f0f045-3228-40e9-ada1-d5f0dc5dd7a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Karl Auer

Email  : ka...@2pisoftware.com
Website: http://2pisoftware.com


GPG/PGP : 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
Previous: F0AB 6C70 A49D 1927 6E05 81E7 AD95 268F 2AB6 40EA

Reply all
Reply to author
Forward
0 new messages