# SPDX-License-Identifier: BSD-3-Clause
---
- name: Ensure state in ["present", "absent"]
  fail:
    msg: "state needs to be present or absent, not '{{ state }}'"
  when: state not in ["present", "absent"]

- name: Ensure type in ["dummy", "tap", "veth"]
  fail:
    msg: "type needs to be dummy, tap or veth, not '{{ type }}'"
  when: type not in ["dummy", "tap", "veth"]

- name: Include the task 'show_interfaces.yml'
  include_tasks: show_interfaces.yml

- name: Install iproute
  package:
    name: iproute
    state: present
    use: "{{ (__network_is_ostree | d(false)) |
      ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
  register: __install_status
  until: __install_status is success
  retries: 6
  delay: 10

# veth
- name: Create veth interface {{ interface }}
  command: "{{ item }}"
  with_items:
    - ip link add {{ interface }} type veth peer name peer{{ interface }}
    - ip link set peer{{ interface }} up
    - ip link set {{ interface }} up
  when: "type == 'veth' and state == 'present' and
         interface not in current_interfaces"
  changed_when: false
- name: Set up veth as managed by NetworkManager
  command: nmcli d set {{ interface }} managed true
  # The variable for `network_provider` is not exists yet,
  # just ignore error for initscripts
  ignore_errors: true  # noqa ignore-errors
  when: "type == 'veth' and state == 'present'"
  changed_when: false

- name: Delete veth interface {{ interface }}
  command: ip link del {{ interface }} type veth
  when: "type == 'veth' and state == 'absent' and
         interface in current_interfaces"
  changed_when: false

# dummy
- name: Create dummy interface {{ interface }}
  command: ip link add "{{ interface }}" type dummy
  when: "type == 'dummy' and state == 'present' and
         interface not in current_interfaces"
  changed_when: false

- name: Delete dummy interface {{ interface }}
  command: ip link del "{{ interface }}" type dummy
  when: "type == 'dummy' and state == 'absent' and
         interface in current_interfaces"
  changed_when: false

# tap
- name: Create tap interface {{ interface }}
  command: ip tuntap add dev {{ interface }} mode tap
  when: "type == 'tap' and state == 'present'
         and interface not in current_interfaces"
  changed_when: false

- name: Delete tap interface {{ interface }}
  command: ip tuntap del dev {{ interface }} mode tap
  when: "type == 'tap' and state == 'absent' and
         interface in current_interfaces"
  changed_when: false
