---
- debug: msg="START cli/basic.yaml on connection={{ ansible_connection }}"

- set_fact: switch_type="{{ switch_type }}"

- block:

  - name: setup - remove config used in test(part1)
    ios_config:
      lines:
        - no interface port-channel 20
      provider: "{{ cli }}"
    ignore_errors: yes

  - name: setup - remove config used in test(part2)
    ios_config:
      lines:
        - no interface port-channel 5
      provider: "{{ cli }}"
    ignore_errors: yes

  - name: setup - remove config used in test(part3)
    ios_config:
      lines:
        - no channel-group 20 mode active
      provider: "{{ cli }}"
      parents: "{{ item }}"
    loop:
      - interface GigabitEthernet0/1
      - interface GigabitEthernet0/2

  - name: create linkagg
    ios_linkagg: &create
      group: 20
      state: present
      provider: "{{ cli }}"
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'interface port-channel 20' in result.commands"

  - name: create linkagg(Idempotence)
    ios_linkagg: *create
    register: result

  - assert:
      that:
        - "result.changed == false"

  - name: set link aggregation group to members
    ios_linkagg: &configure_member
      group: 20
      mode: active
      members:
        - GigabitEthernet0/1
        - GigabitEthernet0/2
      provider: "{{ cli }}"
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'interface GigabitEthernet0/1' in result.commands"
        - "'channel-group 20 mode active' in result.commands"
        - "'interface GigabitEthernet0/2' in result.commands"
        - "'channel-group 20 mode active' in result.commands"

  - name: set link aggregation group to members(Idempotence)
    ios_linkagg: *configure_member
    register: result

  - assert:
      that:
        - "result.changed == false"

  - name: remove link aggregation group from member
    ios_linkagg: &remove_member
      group: 20
      mode: active
      members:
        - GigabitEthernet0/2
      provider: "{{ cli }}"
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'interface GigabitEthernet0/1' in result.commands"
        - "'no channel-group 20 mode active' in result.commands"

  - name: remove link aggregation group from member(Idempotence)
    ios_linkagg: *remove_member
    register: result

  - assert:
      that:
        - "result.changed == false"

  - name: remove linkagg
    ios_linkagg: &remove
      group: 20
      state: absent
      provider: "{{ cli }}"
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'no interface port-channel 20' in result.commands"

  - name: remove linkagg(Idempotence)
    ios_linkagg: *remove
    register: result

  - assert:
      that:
        - "result.changed == false"

  - name: create aggregate of linkagg definitions
    ios_linkagg: &create_agg
      aggregate:
        - { group: 5 }
        - { group: 20, mode: active, members: ['GigabitEthernet0/1'] }
      provider: "{{ cli }}"
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'interface port-channel 5' in result.commands"
        - "'interface port-channel 20' in result.commands"
        - "'interface GigabitEthernet0/1' in result.commands"
        - "'channel-group 20 mode active' in result.commands"

  - name: create aggregate of linkagg definitions(Idempotence)
    ios_linkagg: *create_agg
    register: result

  - assert:
      that:
        - "result.changed == false"

  - name: teardown(part1)
    ios_config:
      lines:
        - no interface port-channel 20
      provider: "{{ cli }}"
    ignore_errors: yes

  - name: teardown(part2)
    ios_config:
      lines:
        - no interface port-channel 5
      provider: "{{ cli }}"
    ignore_errors: yes

  - name: teardown(part3)
    ios_config:
      lines:
        - no channel-group 20 mode active
      provider: "{{ cli }}"
      parents: "{{ item }}"
    loop:
      - interface GigabitEthernet0/1
      - interface GigabitEthernet0/2

  when: switch_type == 'L2'

- debug: msg="END cli/basic.yaml on connection={{ ansible_connection }}"
