---
- debug: msg="START connection={{ ansible_connection }} ios_l2_interface aggregate test"

- name: Setup - Ensure interfaces are switchport
  ios_config:
    lines:
      - switchport
    parents:
      - "interface {{ item }}"
  loop:
    - "{{ test_interface }}"
    - "{{ test_interface2 }}"

- name: "Setup vlans"
  ios_vlan:
    aggregate:
      - vlan_id: 6
      - vlan_id: 15
    provider: "{{ cli }}"

- name: Setup - Remove interface aggregate before testing
  ios_l2_interface:
    aggregate:
      - { name: "{{ test_interface }}", mode: access, access_vlan: 6 }
      - { name: "{{ test_interface2 }}", mode: access, access_vlan: 15 }
    state: absent
    provider: "{{ cli }}"

- block:
  - name: Configure interface for access_vlan aggregate
    ios_l2_interface: &conf_agg
      aggregate:
        - { name: "{{ test_interface }}", mode: access, access_vlan: 6 }
        - { name: "{{ test_interface2 }}", mode: access, access_vlan: 15 }
      provider: "{{ cli }}"
    register: result

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

  - name: Configure interface for access_vlan aggregate(Idempotence)
    ios_l2_interface: *conf_agg
    register: result

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

  - name: Remove interface aggregate
    ios_l2_interface: &rm_agg
      aggregate:
        - { name: "{{ test_interface }}", mode: access, access_vlan: 6 }
        - { name: "{{ test_interface2 }}", mode: access, access_vlan: 15 }
      provider: "{{ cli }}"
      state: absent
    register: result

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

  - name: Remove interface aggregate(Idempotence)
    ios_l2_interface: *rm_agg
    register: result

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

  always:
  - name: "remove vlans"
    ios_vlan:
      aggregate:
        - vlan_id: 6
        - vlan_id: 15
      state: absent
      provider: "{{ cli }}"
    ignore_errors: yes

- debug: msg="END connection={{ ansible_connection }} ios_l2_interface aggregate test"
