Trouble Configuring Trunk Mode on Cisco Nexus Switch using Ansible

Good morning everyone, I am trying to make the following configurations that would be done manually on a Cisco Nexus switch:

interface Eth1/<INTERFACE-ID-DOWNLINK>
  description <HDNUMBER>
  switchport
  switchport mode trunk
  switchport trunk allowed vlan <VLAN-ID>
  mtu 9216
  no shutdown
  exit

I found the cisco.nxos.nxos_interface module and I was able to define the following functions:

- cisco.nxos.nxos_interfaces:
    config:
      - name: "{{porta}}"
        description: "{{hdnumber}}"
        mtu: 9216
        enabled: true

However, when I tried to use the nxos.nxos_config to set the switchport as trunk, I got the following error:

- cisco.nxos.nxos_config:
    lines:
      - interface {{porta}}                  # Define the interface (replace {{porta}} with the interface name, e.g., Ethernet1/1)
      - switchport mode trunk                 # Set the interface as trunk
      - switchport trunk allowed vlan 10,20   # Set the allowed VLANs on the trunk
      - switchport trunk native vlan 1        # Set the native VLAN

ERROR:

TASK [cisco.nxos.nxos_config] ******************************************************************
fatal: [cloudstorage_primary]: FAILED! => {"changed": false, "msg": "switchport mode trunk\r\r\n                                ^\r\n% Invalid command at '^' marker.\r\n\rTESTE-SPEEDbUMPS(config)# "}

Could someone help me?

Try:

- cisco.nxos.nxos_config:
    lines:
      - switchport mode trunk
      - switchport trunk allowed vlan 10,20
      - switchport trunk native vlan 1
    parents:
      - "interface {{ porta }}"

I prefer network resource modules like cisco.nxos.nxos_interface because it will handle far more edge cases when resolving the intended state of your network device than the *_config modules provide.

1 Like