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

- name: Delete interface ipv4 and ipv6 address(setup)
  ios_l3_interface:
    name: "{{ test_interface }}"
    state: absent
    provider: "{{ cli }}"
  register: result

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

- name: Configure interface ipv4 address
  ios_l3_interface:
    name: "{{ test_interface }}"
    ipv4: 192.168.20.1/24
    state: present
    provider: "{{ cli }}"
  register: result

- assert:
    that:
      - 'result.changed == true'
      - '"interface {{ test_interface }}" in result.commands'
      - '"ip address 192.168.20.1 255.255.255.0" in result.commands'

- name: Configure interface ipv4 address (idempotent)
  ios_l3_interface:
    name: "{{ test_interface }}"
    ipv4: 192.168.20.1/24
    state: present
    provider: "{{ cli }}"
  register: result

- assert: &unchanged
    that:
      - 'result.changed == false'

- name: Assign same ipv4 address to other interface (fail)
  ios_l3_interface:
    name: "{{ test_interface2 }}"
    ipv4: 192.168.20.1/24
    state: present
    provider: "{{ cli }}"
  ignore_errors: yes
  register: result

- assert:
    that:
      - "result.failed == true"
      - "result.msg is defined"

- name: Change interface ipv4 address
  ios_l3_interface:
    name: "{{ test_interface }}"
    ipv4: dhcp
    state: present
    provider: "{{ cli }}"
  register: result

- assert:
    that:
      - 'result.changed == true'
      - '"interface {{ test_interface }}" in result.commands'
      - '"ip address dhcp" in result.commands'

- name: Configure interface ipv6 address
  ios_l3_interface: &ipv6-1
    name: "{{ test_interface }}"
    ipv6: fd5d:12c9:2201:1::1/64
    state: present
    provider: "{{ cli }}"
  register: result

- assert:
    that:
      - 'result.changed == true'
      - '"interface {{ test_interface }}" in result.commands'
      - '"ipv6 address fd5d:12c9:2201:1::1/64" in result.commands'

- name: Configure interface ipv6 address (idempotent)
  ios_l3_interface: *ipv6-1
  register: result

- assert: *unchanged

- name: Configure second ipv6 address on interface
  ios_l3_interface: &ipv6-2
    name: "{{ test_interface }}"
    ipv6: fd5d:12c9:2291:1::1/64
    state: present
    provider: "{{ cli }}"
  register: result

- assert:
    that:
      - 'result.changed == true'
      - '"interface {{ test_interface }}" in result.commands'
      - '"ipv6 address fd5d:12c9:2291:1::1/64" in result.commands'

- name: Ensure first ipv6 address still associated with interface
  ios_l3_interface: *ipv6-1
  register: result

- assert: *unchanged

- name: Ensure second ipv6 address still associated with interface
  ios_l3_interface: *ipv6-2
  register: result

- assert: *unchanged

- name: Assign same ipv6 address to other interface (fail)
  ios_l3_interface:
    name: "{{ test_interface2 }}"
    ipv6: fd5d:12c9:2201:1::1/64
    state: present
    provider: "{{ cli }}"
  ignore_errors: yes
  register: result

- assert:
    that:
      - "result.failed == true"
      - "result.msg is defined"

- name: Change interface ipv6 address
  ios_l3_interface:
    name: "{{ test_interface }}"
    ipv6: dhcp
    state: present
    provider: "{{ cli }}"
  register: result

- assert:
    that:
      - 'result.changed == true'
      - '"interface {{ test_interface }}" in result.commands'
      - '"ipv6 address dhcp" in result.commands'

- name: Delete interface ipv4 and ipv6 address
  ios_l3_interface:
    name: "{{ test_interface }}"
    state: absent
    provider: "{{ cli }}"
  register: result

- assert:
    that:
      - 'result.changed == true'
      - '"interface {{ test_interface }}" in result.commands'
      - '"no ip address" in result.commands'
      - '"no ipv6 address" in result.commands'

- name: Delete interface ipv4 and ipv6 address (idempotent)
  ios_l3_interface:
    name: "{{ test_interface }}"
    state: absent
    provider: "{{ cli }}"
  register: result

- assert: *unchanged

- name: Delete second interface ipv4 and ipv6 address (setup)
  ios_l3_interface:
    name: "{{ test_interface2 }}"
    state: absent
    provider: "{{ cli }}"
  register: result

- name: Configure ipv4 and ipv6 address using aggregate
  ios_l3_interface:
    aggregate:
    - { name: "{{ test_interface }}", ipv4: 192.161.0.1/24, ipv6: "fd5d:12c9:2201:2::2/64" }
    - { name: "{{ test_interface2 }}", ipv4: 192.162.0.2/16, ipv6: "fd5e:12c9:2201:3::3/32" }
    provider: "{{ cli }}"
  register: result

- assert:
    that:
      - 'result.changed == true'
      - '"interface {{ test_interface }}" in result.commands'
      - '"ip address 192.161.0.1 255.255.255.0" in result.commands'
      - '"ipv6 address fd5d:12c9:2201:2::2/64" in result.commands'
      - '"interface {{ test_interface2 }}" in result.commands'
      - '"ip address 192.162.0.2 255.255.0.0" in result.commands'
      - '"ipv6 address fd5e:12c9:2201:3::3/32" in result.commands'

- name:  Configure ipv4 and ipv6 address using aggregate (idempotent)
  ios_l3_interface:
    aggregate:
    - { name: "{{ test_interface }}", ipv4: 192.161.0.1/24, ipv6: "fd5d:12c9:2201:2::2/64" }
    - { name: "{{ test_interface2 }}", ipv4: 192.162.0.2/16, ipv6: "fd5e:12c9:2201:3::3/32" }
    provider: "{{ cli }}"
  register: result

- assert: *unchanged

- name:  Change ipv4 and ipv6 address using aggregate
  ios_l3_interface:
    aggregate:
    - { name: "{{ test_interface }}", ipv4: 193.167.1.1/8, ipv6: "fd5a:12c9:2201:4::4/32" }
    - { name: "{{ test_interface2 }}", ipv4: 192.169.2.2/24, ipv6: "fd5b:12c9:2201:5::5/90" }
    provider: "{{ cli }}"
  register: result

- assert:
    that:
      - 'result.changed == true'
      - '"interface {{ test_interface }}" in result.commands'
      - '"ip address 193.167.1.1 255.0.0.0" in result.commands'
      - '"ipv6 address fd5a:12c9:2201:4::4/32" in result.commands'
      - '"interface {{ test_interface2 }}" in result.commands'
      - '"ip address 192.169.2.2 255.255.255.0" in result.commands'
      - '"ipv6 address fd5b:12c9:2201:5::5/90" in result.commands'


- name:  Delete ipv4 and ipv6 address using aggregate
  ios_l3_interface:
    aggregate:
    - { name: "{{ test_interface }}" }
    - { name: "{{ test_interface2 }}" }
    state: absent
    provider: "{{ cli }}"
  register: result

- assert:
    that:
      - 'result.changed == true'
      - '"interface {{ test_interface }}" in result.commands'
      - '"no ip address" in result.commands'
      - '"no ipv6 address" in result.commands'
      - '"interface {{ test_interface2 }}" in result.commands'
      - '"no ip address" in result.commands'
      - '"no ipv6 address" in result.commands'

- name:  Delete ipv4 and ipv6 address using aggregate (idempotent)
  ios_l3_interface:
    aggregate:
    - { name: "{{ test_interface }}" }
    - { name: "{{ test_interface2 }}" }
    state: absent
    provider: "{{ cli }}"
  register: result

- assert: *unchanged

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