---
- name: Ensure we have loopback 888 for testing
  connection: ansible.netcommon.network_cli
  become: true
  cisco.ios.ios_config:
    src: config.j2

- name: Set test facts shorter_hostname
  ansible.builtin.set_fact:
    shorter_hostname: '{{ inventory_hostname_short | truncate(10, True, "") }}'

- name: Discover IOS l2/l3 switch type
  connection: ansible.netcommon.network_cli
  become: true
  register: result
  cisco.ios.ios_command:
    commands:
      - show version

- name: Set test facts
  ansible.builtin.set_fact:
    switch_type: ""

- name: Set test facts to layer 2
  ansible.builtin.set_fact:
    switch_type: "L2"
  when: '"l2" in result.stdout[0]'

- name: Block to set facts
  when: "'Cisco IOS' in result.stdout[0]"
  block:
    - name: Set test interface to gigabitethernet0/1 as we are on cisco IOS
      ansible.builtin.set_fact:
        test_interface: GigabitEthernet2

    - name: Set test interface 2 to gigabitethernet0/2 as we are on cisco IOS
      ansible.builtin.set_fact:
        test_interface2: GigabitEthernet3

- name: Block to fix configuration
  when: "'Cisco IOS-XE' in result.stdout[0]"
  block:
    - name: Set test interface to gigabitethernet2 as we are on cisco ios-xe
      ansible.builtin.set_fact:
        test_interface: GigabitEthernet2

    - name: Disable autonegotiation on gigabitethernet2
      cisco.ios.ios_config:
        lines:
          - no negotiation auto
        parents: int GigabitEthernet2
      become: true

    - name: Set test interface 2 to gigabitethernet3 as we are on cisco ios-xe
      ansible.builtin.set_fact:
        test_interface2: GigabitEthernet3

    - name: Disable autonegotiation on gigabitethernet3
      cisco.ios.ios_config:
        lines:
          - no negotiation auto
        parents: int GigabitEthernet3
      become: true
