---
- name: Set platform/version specific variables
  include_role:
    name: linux-system-roles.vpn
    tasks_from: set_vars
    public: true

- name: Get service facts
  service_facts:

- name: Stop ipsec service
  service:
    name: ipsec
    state: stopped
    enabled: false
  when: ansible_facts.services['ipsec.service'] is defined

- name: Uninstall packages
  package:
    name: "{{ item }}"
    state: absent
  loop: "{{ __vpn_packages | d([]) | difference(do_not_remove) | list }}"
  vars:
    # many packages depend on iproute - cannot remove
    do_not_remove: [iproute]
  when: not __vpn_is_ostree | d(false)

- name: Clean up files
  vars:
    tunnels: "{{ __vpn_connections_fixed | rejectattr('opportunistic') |
      selectattr('hosts', 'contains', inventory_hostname) | map(attribute='hosts') |
      map('dict2items') | flatten | map(attribute='key') |
      flatten | reject('match', '^' ~ inventory_hostname ~ '$') | unique | list }}"
  block:
    - name: Remove ipsec.conf files
      file:
        state: absent
        path: "/etc/ipsec.d/{{ inventory_hostname }}-to-{{ item }}.conf"
      with_items: "{{ tunnels }}"

    - name: Remove ipsec.secrets files
      file:
        state: absent
        path: "/etc/ipsec.d/{{ inventory_hostname }}-to-{{ item }}.secrets"
      with_items: "{{ tunnels }}"

- name: Stop firewall
  service:
    name: firewalld
    state: stopped
  when: vpn_manage_firewall | bool
