---
- hosts: "{{ host_list }}"
  become: yes
  become_method: sudo
  remote_user: "{{ my_user }}"

  tasks:
    - name: SUDO | Group sudo presence
      ansible.builtin.group:
        name: sudo
        state: present

    - name: SUDO | Group wheel absence
      ansible.builtin.group:
        name: wheel
        state: absent

    - name: SUDO | User presence (with groups)
      ansible.builtin.user:
        name: "{{ my_user }}"
        state: present
        groups: sudo
        append: true

    - name: SUDO | Reset ansible connection to apply group update
      meta: reset_connection

    - name: "SSH | Local public key is present for {{ my_user }}"
      ansible.builtin.authorized_key:
        comment: "Managed by Ansible from GitLab @free_zed"
        key: https://gitlab.com/free_zed.keys
        state: present
        user: "{{ my_user }}"

    - name: SSH | Disallow SSH password authentication for root
      copy:
        src: sshd_config
        dest: /etc/ssh/sshd_config

    - name: "SSH | Local public key is present for root"
      ansible.builtin.authorized_key:
        comment: "Managed by Ansible from GitLab @free_zed"
        key: https://gitlab.com/free_zed.keys
        state: present
        user: root

    - name: SSH | Remove no password directive
      ansible.builtin.lineinfile:
        path: /etc/sudoers
        state: absent
        regexp: 'NOPASSWD'
        validate: '/usr/sbin/visudo -cf %s'
