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}}"




-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/d52cb332-29ff-4f11-bd46-b75d7b0578a2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to