3

I'm trying to get a routing table from a Cisco ACI. I'm getting duplicate routes. Is there a way to sort them out?

example:

        "10.127.32.70/32",
        "10.127.32.70/32",
        "10.127.32.70/32",
        "10.127.32.70/32",
        "10.127.32.70/32",
        "10.127.32.70/32"

after duplicate filter:

        "10.127.32.70/32"
  - name: Get Route table (vars tenant and VRF)
    aci_rest:
      <<: *aci_login
      path: api/node/class/uribv4Nexthop.json?query-target-filter=wcard(uribv4Nexthop.dn,"sys/uribv4/dom\-C_Lab:LAB_VRF/db\-rt")
      method: "get"
    register: response 

#  - debug:
#      var: response

  - name: Create a subnet list item
    set_fact:
      array_content: "{{ item['uribv4Nexthop']['attributes']['addr'] }}" 
    with_items: 
      - "{{ response['imdata'] }}"
    register: array_of_contents  
  
  - name: Make a list with all the subnet items
    set_fact: 
         data_struc: "{{ array_of_contents.results | map(attribute='ansible_facts.array_content') | list }}"

  - debug:
      var: data_struc
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
Muller
  • 57
  • 5

1 Answers1

3

Use filter unique, e.g.

    - debug:
        msg: "{{ data_struc|unique }}"

gives

  msg:
  - 10.127.32.70/32

Q: "Get addresses with /24 only"

A: Use filter ipaddr, e.g.

    - set_fact:
        prefix_24: "{{ prefix_24|d([]) + [item] }}"
      loop: "{{ data_struc|unique }}"
      when: item|ipaddr('prefix') == 24
      vars:
        data_struc:
          - 10.127.32.70/32
          - 10.127.32.71/24
          - 10.127.32.71/24
          - 10.127.32.72/32

gives

  prefix_24:
  - 10.127.32.71/24
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63