ansible-lint: fix base role
[noc.git] / ansible / roles / base / tasks / 01ssh.yml
1 ---
2 - name: generate list of users allowed to login via ssh
3   set_fact:
4     sshd_allowusers: >-
5       {{ [ 'root' ] | union(user_groups.noc)
6                     | union(sshd_allowusers_group | default([]))
7                     | union(sshd_allowusers_host  | default([])) }}
8
9 - name: only allow pubkey auth for root
10   lineinfile:
11     dest: /etc/ssh/sshd_config
12     regexp: "^PermitRootLogin"
13     line: "PermitRootLogin without-password"
14   notify: restart ssh
15
16 - name: limit allowed users (1/2)
17   when: sshd_allowgroup is not defined
18   lineinfile:
19     dest: /etc/ssh/sshd_config
20     regexp: "^#?AllowUsers"
21     line: "AllowUsers {{ ' '.join(sshd_allowusers) }}"
22   notify: restart ssh
23
24 - block:
25     - name: "limit allowed users (2/2): Make sure AllowUsers is not in sshd_config"
26       lineinfile:
27         dest: /etc/ssh/sshd_config
28         regexp: "^AllowUsers"
29         state: absent
30       notify: restart ssh
31
32     - name: "limit allowed users (2/2): Set AllowGroups in sshd_config"
33       lineinfile:
34         dest: /etc/ssh/sshd_config
35         regexp: "^#?AllowGroups"
36         line: AllowGroups {{ sshd_allowgroup }}
37       notify: restart ssh
38
39     - name: "limit allowed users (2/2): Add allowed users to ssh group"
40       user:
41         name: "{{ item }}"
42         groups: "{{ sshd_allowgroup }}"
43         append: True
44       with_items: "{{ sshd_allowusers }}"
45
46   when: sshd_allowgroup is defined
47
48 - name: Set authorized keys for root user
49   authorized_key:
50     user: root
51     key: "{{ ssh_users_root | user_ssh_keys(users) | join('\n') }}"
52     exclusive: yes