1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- name: Secure the system
  hosts: "{{ ansible_limit | default([]) }}"
  become: true
  tasks:

    - name: Disable SSH password logins
      ansible.builtin.lineinfile:
        path: /etc/ssh/sshd_config
        regexp: '^(#\s*)?{{ item }} '
        line: "{{ item }} no"
      loop:
        - KbdInteractiveAuthentication
        - PasswordAuthentication
        - UsePAM
      notify: Restart sshd

    - name: Allow sudo without password
      ansible.builtin.lineinfile:
        path: /etc/sudoers
        regexp: '^%sudo'
        line: '%sudo ALL=(ALL:ALL) NOPASSWD:ALL'
        validate: 'visudo -cf %s'

  handlers:
    - name: Restart sshd
      ansible.builtin.systemd:
        name: sshd
        state: restarted

# vim: ft=yaml.ansible