- name: Initialize VPS
  hosts: "{{ chosen_host | default('staging0.test') }}"
  remote_user: "{{ chosen_user | default('senpai') }}"
  vars_files:
    - vars/surge_settings.yml
    - vars/git_aliases.yml # REQUIRED
    - vars/vim_settings.yml # REQUIRED
    - vars/podpose_settings.yml # REQUIRED
    - vars/certbot_settings.yml # REQUIRED
    - vars/config@{{ inventory_hostname | default('vps') }}.yml
  tasks:
    - name: Installing requisite packages
      ansible.builtin.include_role:
        name: init-server
        tasks_from: userspace@install-pkgs
        handlers_from: userspace
    - name: Disabling root user shell login
      become: true
      ansible.builtin.user:
        name: root
        shell: /sbin/nologin
      tags:
        - disable_root_shell
    - name: Disable login for root user altogether
      become: true
      ansible.builtin.user:
        name: root
        password: "'*'"
      tags:
        - disable_root_login
    # - name: Debugging
    #   ansible.builtin.debug:
    #     msg: "{{ pkgs | dict2items(key_name='pkg_group', value_name='pkgs') | map(attribute='pkgs') | list |  map(attribute='userspace', default='no_userspace') | list | flatten | reject('search', 'no_userspace') | list }}"
    # - name: Prematurely ending play
    #   ansible.builtin.meta: end_play
    - name: Reorganizing userspace package groups into single list
      ansible.builtin.set_fact:
        all_userspace_pkgs: "{{ pkgs | dict2items(key_name='pkg_group', value_name='pkgs') | map(attribute='pkgs') | list |  map(attribute='userspace', default='no_userspace') | list | flatten | reject('search', 'no_userspace') | list }}"
    - name: Reorganizing core package groups into single list
      ansible.builtin.set_fact:
        all_core_pkgs: "{{ pkgs | dict2items(key_name='pkg_group', value_name='pkgs') | map(attribute='pkgs') | list | map(attribute='core', default='no_core') | list | flatten | reject('search', 'no_core') | list }}"
    - name: Configuring aliases for using git
      when: "'git' in all_core_pkgs"
      community.general.git_config:
        name: "alias.{{ item[0] }}"
        scope: global
        value: "{{ item[1] }}"
      loop: "{{ git_aliases }}"
    - name: Opening port 51820
      become: true
      ansible.builtin.iptables:
        chain: INPUT
        protocol: "{{ item }}"
        destination_port: 51820
        jump: ACCEPT
        comment: Open up port 51820
      loop:
        - udp
        - tcp
    - name: Opening port 443
      become: true
      ansible.builtin.iptables:
        chain: INPUT
        protocol: "{{ item }}"
        destination_port: 443
        jump: ACCEPT
        comment: Open up port 443
      loop:
        - udp
        - tcp
    - name: Opening ports
      become: true
      ansible.builtin.iptables:
        chain: INPUT
        protocol: tcp
        destination_port: "{{ item }}"
        jump: ACCEPT
        comment: "Open up port {{ (item | string) }}"
      loop:
        - 80
        - 465
        - 587
        - 995
        - 993
