Files
ansible-playbooks/roles/sshd/tasks/main.yml
T
Clément Désiles ea93eb8c59 fix(sshd): make authorized_keys fallback readable by the session user
The fallback was created via cp as root:root 0600 and relied on a separate
chown task to fix ownership. When that chown did not take, sshd could not
read /etc/ssh/authorized_keys/<user> in the unprivileged session, so pubkey
auth failed whenever the encrypted home was locked.

Replace the cp + chown pair with a single idempotent copy that keeps the
file root-owned but world-readable (0644). It contains only public keys, so
0644 is safe and passes StrictModes.
2026-09-10 21:21:06 +02:00

89 lines
2.2 KiB
YAML

---
- name: Load OS-specific variables
ansible.builtin.include_vars: "{{ item }}"
with_first_found:
- "vars/{{ ansible_facts['os_family'] }}.yml"
- "vars/debian.yml"
- name: Install OpenSSH
ansible.builtin.package:
name: "{{ ssh_package_name }}"
state: present
- name: Install UFW
ansible.builtin.package:
name: ufw
state: present
- name: Enable SSH
ansible.builtin.service:
name: "{{ ssh_service_name }}"
enabled: true
- name: Allow SSH incoming connections
community.general.ufw:
rule: allow
port: "{{ ssh_port }}"
proto: tcp
from: "{{ item.src }}"
direction: in
comment: "{{ item.comment }}"
loop: "{{ ssh_allowed_networks }}"
# TODO
# - name: Add SSH public key to authorized_keys
# authorized_key:
# user: "{{ item }}"
# state: present
# key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
# comment: "{{ lookup('env', 'USER') | default('ansible') }}@{{ lookup('pipe', 'hostname -s') }}"
# loop: "{{ ssh_users.split() }}"
- name: Authorized keys fallback (when home cannot be mounted)
when: ssh_authorized_keys_fallback_enabled
block:
- name: Create the directory
ansible.builtin.file:
path: "{{ ssh_authorized_keys_fallback_dir }}"
state: directory
owner: root
group: root
mode: "0755"
- name: Backup authorized_keys out of HOME dir (if unavailable at startup)
ansible.builtin.copy:
src: "/home/{{ item }}/.ssh/authorized_keys"
dest: "{{ ssh_authorized_keys_fallback_dir }}/{{ item }}"
remote_src: true
owner: root
group: root
mode: "0644"
loop: "{{ ssh_users.split() }}"
- name: Remove motd on Debian
ansible.builtin.file:
path: /etc/motd
state: absent
when: ansible_facts['os_family'] == 'Debian'
- name: Hardening sshd_config
ansible.builtin.template:
src: templates/sshd_config.j2
dest: "{{ sshd_config }}"
owner: root
group: root
mode: "0600"
validate: "{{ sshd_binary }} -t -f %s"
notify: Restart SSH service
- name: Enable UFW
community.general.ufw:
state: enabled
- name: Enable UFW service at startup
ansible.builtin.systemd:
name: ufw
enabled: true
state: started