Search code examples
ansiblenetbox

Five-level iteration loop in ansible for netbox


First of all thank for reading my post. I am a neewbie in Ansible and have this devices.yml file

devices:
  test-device1:
    name: Test Device 1
    device_type: test-device-type1
    device_role: router1
    site: UoP
    position: 12
    rack: Lande
    face: Front
    serial: "864kjfhi3y74"
    # primary_ip4: ""
    # description: ""
    # commments: ""
    interfaces:
      - name: GigabitEthernet22
        enabled: true
        mgmt_only: false
        type: 1000Base-t (1GE)
        mode: Access
        untagged_vlan: vlan401
      - name: GigabitEthernet23
        enabled: false
        mgmt_only: false
        type: 1000Base-t (1GE)
        mode: Tagged
        untagged_vlan: vlan401
        tagged_vlans: 
          - vlan1000
          - vlan1015
        lag:
          name: port-channel1
 

  test-device2:
    name: Test Device 2
    device_type: test-device-type2
    device_role: switch1
    site: UoP
    position: 14
    rack: Lande
    face: Front
    # serial: 
    # primary_ip4: 10.1.3.192
    description: Test device 2
    comments: Test
    interfaces:
      - name: ether20
        ip4: 10.1.3.192
        enabled: true
        mgmt_only: true
        type: 1000Base-t (1GE)
        mode: Tagged
        untagged_vlan: vlan401
        tagged_vlans: 
          - vlan1014
        lag:
          name: port-channel1

and am trying to access the tagged_vlans list required from the netbox module (description here

using the following task:

- name: Create interfaces
  netbox.netbox.netbox_device_interface:
    data:
      device: "{{ item.value.name }}"
      name: "{{ item.value.interfaces.0.name }}"
      enabled: "{{ item.value.interfaces.0.enabled }}"
      type: "{{ item.value.interfaces.0.type|default(omit) }}"
      untagged_vlan:
        name: "{{ item.value.interfaces.0.untagged_vlan|default(omit) }}"
      tagged_vlans:
        name: "{{ item.value.interfaces.0.tagged_vlans.0|default(omit) }}"
        site: "{{ item.value.site }}"
      mgmt_only: "{{ item.value.interfaces.0.mgmt_only|default(omit) }}"
      mode: "{{ item.value.interfaces.0.mode|default(omit) }}"
    state: present
  loop: "{{ devices | dict2items }}"

I am expecting the loop to run for each vlan ID described in the tagged_vlans list. See here the error I am getting

..... The request failed with code 400 Bad Request: {'tagged_vlans': ['Expected a list of items but got type "int".']}\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

I think I am providing the list, so what is the problem here ?


Solution

  • With the help of Nikos Kallergis found the answer which is change the datastructure of the tagged_vlans from

    tagged_vlans:
      - vlan1000
      - vlan1015
    

    to

    tagged_vlans:
      - name: vlan1000
      - name: vlan1015
    

    and change the task from

     tagged_vlans:
       name: "{{ item.1.tagged_vlans.0|default(omit) }}"
    

    to

     tagged_vlans: "{{ item.1.tagged_vlans.0|default(omit) }}"
    

    Thanks for the help :)