looping through list of dictionaries

20 views
Skip to first unread message

Brian Goulet

unread,
Jul 1, 2019, 4:32:52 PM7/1/19
to Ansible Project
my playbook grabs show command output from a cisco switch and then reformats that output into structured data thanks to a parsing script written by someone smarter than me.  i'm pretty new to ansible data structures but I believe this data is a list (ios_vlans) of dictionary objects (vlans) which contains multiple elements, including one that is also a list (ports).    here is sample data 

# Schema Output
#--------------
#
#  {
#     "ansible_facts": {
#         "ios_vlans": {
#             "vlans": [
#                 {
#                     "id": 1,
#                     "name": "default",
#                     "ports": [
#                         "GigabitEthernet0/1",
#                         "GigabitEthernet0/2",
#                         "GigabitEthernet0/3",
#                         "GigabitEthernet1/0",
#                     "status": "active"
#                 },
#                 {
#                     "id": 200,
#                     "name": "vlan200",
#                     "ports": [
#                         ""
#                     ],
#                     "status": "active"
#                 },
#                 {
#                     "id": 300,
#                     "name": "vlan300",
#                     "ports": [
#                         ""
#                     ],
#                     "status": "active"
#                 }
#             ]
#         }
#    },
#

I can see this data in its entirety by referencing ios_vlans.  I can also directly access elemets such as ios_vlans.vlans[1].id by calling them explicitly.  the problem is that I am unable to iterate through the data. I've tried with_items and with_dict and get the error that 'items' is undefined.  here is the play:

---
- name: change port vlan
  hosts: all
  connection: local
  gather_facts: no
  vars:
     vlan_parse_path: ./vlan-parser.yml
 
  tasks:
   
  - name: get vlan info
    ios_command:
      commands: show vlan
    register: vlan_output

  - name: generate fact from vlan output
    set_fact:
      ios_vlans: "{{vlan_output.stdout[0] | parse_cli(vlan_parse_path)}}"

  - name: complete schema output
    debug:
      msg: "{{ios_vlans}}"
     
  - name: first vlan in index
    debug:
      msg: "{{ios_vlans.vlans[0]}}"
     
  - name: second vlan in index
    debug:
      msg: "{{ios_vlans.vlans[1]}}"
     
  - name: output first vlan ID
    debug:
      msg: "{{ios_vlans.vlans[0].id}}"

  - name: output all vlan IDs <<<<<<<this task fails
    debug:
      msg: "{{item.id}}"
      with_items: "{{ios_vlans}}"




Kai Stian Olstad

unread,
Jul 1, 2019, 5:10:04 PM7/1/19
to ansible...@googlegroups.com
On 01.07.2019 22:32, Brian Goulet wrote:
> - name: output all vlan IDs <<<<<<<this task fails
> debug:
> msg: "{{item.id}}"
> with_items: "{{ios_vlans}}"

with_items is a parameter for the task not the module debug, so with_items need to indented at the same level as name and debug.
with_items takes a list, so you need
with_items: "{{ ios_vlans.vlans }}"


--
Kai Stian Olstad

Brian Goulet

unread,
Jul 2, 2019, 9:38:54 AM7/2/19
to Ansible Project
ha!  well that was silly.  beaten by space characters again.  thank you so much Kai.  
Reply all
Reply to author
Forward
0 new messages