X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=ansible%2Froles%2Fbase%2Ftasks%2Fmain.yaml;h=c62d223a5f90ac3fd5903ec5f50d35642a92c702;hb=41dfd44f1520728da65e43abc6fddfb59b2fed78;hp=7f60b4e0f6adca460eedf8a3be232f66d0dc97af;hpb=84aa5a99e4426e4e24031187f727af92ef540562;p=noc.git diff --git a/ansible/roles/base/tasks/main.yaml b/ansible/roles/base/tasks/main.yaml index 7f60b4e..c62d223 100644 --- a/ansible/roles/base/tasks/main.yaml +++ b/ansible/roles/base/tasks/main.yaml @@ -1,4 +1,9 @@ --- +- set_fact: + sshd_allowusers: >- + {{ [ 'root' ] | union(sshd_allowusers_group | default([])) + | union(sshd_allowusers_host | default([])) }} + - name: only allow pubkey auth for root lineinfile: dest: /etc/ssh/sshd_config @@ -6,20 +11,42 @@ line: "PermitRootLogin without-password" notify: restart ssh -- name: limit allowed users (1/2) +- name: limit allowed users (1/3) lineinfile: dest: /etc/ssh/sshd_config - regexp: "^AllowUsers" - line: "AllowUsers {{ ' '.join([ 'root' ] | union(sshd_allowusers_group | default([])) | union(sshd_allowusers_host | default([]))) }}" - when: sshd_allowusers_set | default(true) + regexp: "^#?AllowUsers" + line: "AllowUsers {{ ' '.join(sshd_allowusers) }}" + when: sshd_allowusers_set is defined and sshd_allowgroup is not defined notify: restart ssh -- name: limit allowed users (2/2) +- block: + - name: "limit allowed users (2/3): Make sure AllowUsers is not in sshd_config" + lineinfile: + dest: /etc/ssh/sshd_config + regexp: "^AllowUsers" + state: absent + + - name: "limit allowed users (2/3): Set AllowGroups in sshd_config" + lineinfile: + dest: /etc/ssh/sshd_config + regexp: "^#?AllowGroups" + line: AllowGroups {{ sshd_allowgroup }} + + - name: "limit allowed users (2/3): Add allowed users to ssh group" + user: + name: "{{ item }}" + groups: "{{ sshd_allowgroup }}" + append: True + with_items: "{{ sshd_allowusers }}" + + when: sshd_allowgroup is defined + +- name: limit allowed users (3/3) lineinfile: dest: /etc/ssh/sshd_config - regexp: "^AllowUsers" + regexp: "^Allow(Users|Groups)" state: absent - when: not sshd_allowusers_set | default(true) + when: sshd_allowusers_set is not defined and sshd_allowgroup is not defined notify: restart ssh - name: Set authorized keys for root user