ansible-roles/evolinux-users/tasks/ssh.yml
Patrick Marchand cf9ea7415a Normalize conditions and check Match User statement better
No need for two facts if one invalidates the other.
2019-09-24 09:03:08 -04:00

106 lines
3 KiB
YAML

---
- name: verify AllowGroups directive
command: "grep -E '^AllowGroups' /etc/ssh/sshd_config"
changed_when: False
failed_when: False
check_mode: no
register: grep_allowgroups_ssh
- debug:
var: grep_allowgroups_ssh
verbosity: 1
- name: verify AllowUsers directive
command: "grep -E '^AllowUsers' /etc/ssh/sshd_config"
changed_when: False
failed_when: False
check_mode: no
register: grep_allowusers_ssh
- debug:
var: grep_allowusers_ssh
verbosity: 1
- assert:
that: "not (grep_allowusers_ssh.rc == 0 and grep_allowgroups_ssh.rc == 0)"
msg: "We can't deal with AllowUsers and AllowGroups at the same time"
- set_fact:
# If "AllowGroups is present" or "AllowUsers is absent and Debian 10+",
ssh_allowgroups: "{{ (grep_allowgroups_ssh.rc == 0) or (grep_allowusers_ssh.rc != 0 and (ansible_distribution_major_version | version_compare('10', '>='))) }}"
- debug:
var: ssh_allowgroups
verbosity: 1
- include: ssh_allowgroups.yml
vars:
- allow_groups_present: "{{ grep_allowgroups_ssh.rc == 0 }}"
when:
- ssh_allowgroups
- name: "Add AllowUsers sshd directive with all users"
lineinfile:
dest: /etc/ssh/sshd_config
line: "\nAllowUsers {{ evolinux_users|map(attribute='name')|join(',') }}"
insertafter: 'Subsystem'
validate: '/usr/sbin/sshd -t -f %s'
notify: reload sshd
when:
- grep_allowusers_ssh.rc != 0
- not ssh_allowgroups
- name: "Append '{{ item.name }}' to AllowUsers sshd directive"
replace:
dest: /etc/ssh/sshd_config
regexp: '^(AllowUsers ((?!\b{{ item.name }}\b).)*)$'
replace: '\1 {{ item.name }}'
validate: '/usr/sbin/sshd -t -f %s'
with_dict: "{{ evolinux_users }}"
notify: reload sshd
when:
- grep_allowusers_ssh.rc == 0
- not ssh_allowgroups
- name: "verify Match User directive"
command: "grep -E '^Match User' /etc/ssh/sshd_config"
changed_when: False
failed_when: False
check_mode: no
register: grep_matchuser_ssh
when: not ssh_allowgroups
- name: "Add Match User sshd directive with all users"
lineinfile:
dest: /etc/ssh/sshd_config
line: "\nMatch User {{ evolinux_users|map(attribute='name')|join(',') }}\n PasswordAuthentication no"
insertafter: "# END EVOLINUX PASSWORD RESTRICTIONS BY ADDRESS"
validate: '/usr/sbin/sshd -t -f %s'
notify: reload sshd
when:
- grep_matchuser_ssh != 0
- not ssh_allowgroups
- name: "Append '{{ item.name }}' to Match User's sshd directive"
replace:
dest: /etc/ssh/sshd_config
regexp: '^(Match User ((?!{{ item.name }}).)*)$'
replace: '\1,{{ item.name }}'
validate: '/usr/sbin/sshd -t -f %s'
with_dict: "{{ evolinux_users }}"
notify: reload sshd
when:
- grep_matchuser_ssh.rc == 0
- not ssh_allowgroups
- name: disable root login
replace:
dest: /etc/ssh/sshd_config
regexp: '^PermitRootLogin (yes|without-password|prohibit-password)'
replace: "PermitRootLogin no"
notify: reload sshd
when: evolinux_root_disable_ssh
- meta: flush_handlers