How to loop over hashes having an undefined number of sub-elements?

22 views
Skip to first unread message

jean-christophe manciot

unread,
Oct 21, 2016, 2:31:58 PM10/21/16
to Ansible Project
Hi everyone,

This is a tricky question which I have been unable to solve so far despîte multiple attempts, maybe it is not feasible with Ansible.
I pull some facts from a networking device, some of them end up in {{ facts.ansible_facts.ansible_net_neighbors }}:
"ansible_net_neighbors": {
        "Gi0/0": [
            {
                "host": "IOSv_L2_10.actionmystique.net",
                "port": "Gi0/1"
            }
        ],
        "Gi0/3": [
            {
                "host": "IOSv_Leaf_13.actionmystique.net",
                "port": "Gi0/1.10"
            },
            {
                "host": "IOSv_Leaf_13.actionmystique.net",
                "port": "Gi0/1.20"
            },
            {
                "host": "IOSv_Leaf_13.actionmystique.net",
                "port": "Gi0/1"
            }
        ],
        "Gi0/4": [
            {
                "host": "IOSv_Leaf_14.actionmystique.net",
                "port": "Gi0/1"
            },
            {
                "host": "IOSv_Leaf_14.actionmystique.net",
                "port": "Gi0/1.20"
            },
            {
                "host": "IOSv_Leaf_14.actionmystique.net",
                "port": "Gi0/1.10"
            }
        ],
...
The number & names of ansible_net_neighbors sub-elements "Gin/p" (which stand for GigabitEthernet n/p) are dynamic: we have no clue in advance what they'll be.
Same characteristic for the number of host/port duo(s) inside each "Gin/p"

So, how can I loop over all those sub-elements? The tricky part for me is the fact that there is no name for the field "Gin/p".
I have unsuccessfully tried the following for instance:
- name: Updating the description of all interfaces
  ios_config:
        provider: "{{ connections.ssh }}"
        parents: 
          - "interface {{ item }}"
        lines:
          - "description Connected to {{ item.host }} on its port {{ item.port }}"
  with_dict: "{{ facts.ansible_facts.ansible_net_neighbors }}"
  register: result

Any ingenious trick in mind?
Reply all
Reply to author
Forward
0 new messages