diff --git a/playbooks/openssh.yml b/playbooks/openssh.yml new file mode 100644 index 0000000..53ee911 --- /dev/null +++ b/playbooks/openssh.yml @@ -0,0 +1,7 @@ +--- +- name: Ensure openssh is installed and configured + hosts: "{{ openssh_target | default('openssh') }}" + become: "{{ openssh_become | default(true) }}" + gather_facts: "{{ openssh_gather_facts | default(true) }}" + roles: + - role: finallycoffee.base.openssh diff --git a/roles/openssh/defaults/main/main.yml b/roles/openssh/defaults/main/main.yml new file mode 100644 index 0000000..f7b0409 --- /dev/null +++ b/roles/openssh/defaults/main/main.yml @@ -0,0 +1,2 @@ +--- +openssh_sshd_config_file: "/etc/ssh/sshd_config" diff --git a/roles/openssh/defaults/main/sshd.yml b/roles/openssh/defaults/main/sshd.yml new file mode 100644 index 0000000..9c9eaf2 --- /dev/null +++ b/roles/openssh/defaults/main/sshd.yml @@ -0,0 +1,33 @@ +--- +openssh_sshd_enable: true +openssh_sshd_config_pubkey_authentication: true +openssh_sshd_config_password_authentication: false +openssh_sshd_config_challenge_response_authentication: false +openssh_sshd_config_permit_root_login: false + +# Limits +openssh_sshd_config_max_sessions: ~ +openssh_sshd_config_max_startups: ~ + +# Hardening +openssh_sshd_config_protocol: 2 +openssh_sshd_config_x11_forwarding: false +openssh_sshd_config_allow_agent_forwarding: false +openssh_sshd_config_allow_tcp_forwarding: false + +openssh_sshd_default_config: + PubkeyAuthentication: "{{ openssh_sshd_config_pubkey_authentication }}" + PasswordAuthentication: "{{ openssh_sshd_config_password_authentication }}" + ChallengeResponseAuthentication: >-2 + {{ openssh_sshd_config_challenge_response_authentication }} + PermitRootLogin: "{{ openssh_sshd_config_permit_root_login }}" + MaxSessions: "{{ openssh_sshd_config_max_sessions }}" + MaxStartups: "{{ openssh_sshd_config_max_startups }}" + Protocol: "{{ openssh_sshd_config_protocol }}" + X11Forwarding: "{{ openssh_sshd_config_x11_forwarding }}" + AllowAgentForwarding: "{{ openssh_sshd_config_allow_agent_forwarding }}" + AllowTcpForwarding: "{{ openssh_sshd_config_allow_tcp_forwarding }}" + +openssh_sshd_merged_config: >-2 +{{ openssh_sshd_default_config | default({}, true) + | combine(openssh_sshd_config | default({}, true)) }} diff --git a/roles/openssh/tasks/configure-sshd.yml b/roles/openssh/tasks/configure-sshd.yml new file mode 100644 index 0000000..5456f2e --- /dev/null +++ b/roles/openssh/tasks/configure-sshd.yml @@ -0,0 +1,25 @@ +--- +- name: Configure sshd + ansible.builtin.lineinfile: + path: "{{ openssh_sshd_config_file }}" + regexp: "{{ openssh_sshd_config_regexp }}" + line: "{{ openssh_sshd_config_line }}" + state: present + validate: "sshd -Tf %s" + loop: "{{ openssh_sshd_merged_config | dict2items }}" + loop_control: + loop_var: "tuple" + label: "{{ tuple.key }}" + vars: + openssh_sshd_config_regexp: "^\\s*#?\\s*{{ tuple.key }}" + openssh_sshd_config_line: >-2 + {{ openssh_sshd_config_line_commented }}{{ tuple.key }} {{ openssh_sshd_config_value }} + openssh_sshd_config_value_is_none: "{{ tuple.value is none }}" + openssh_sshd_config_line_commented: >-2 + {{ openssh_sshd_config_value_is_none | ternary('#', '') }} + openssh_sshd_config_value: >-2 + {{ (tuple.value is boolean) | ternary( + tuple.value | ternary('yes', 'no'), + tuple.value + ) + }} diff --git a/roles/openssh/tasks/install.yml b/roles/openssh/tasks/install.yml new file mode 100644 index 0000000..cd21505 --- /dev/null +++ b/roles/openssh/tasks/install.yml @@ -0,0 +1,2 @@ +--- + diff --git a/roles/openssh/tasks/main.yml b/roles/openssh/tasks/main.yml new file mode 100644 index 0000000..28a3fbf --- /dev/null +++ b/roles/openssh/tasks/main.yml @@ -0,0 +1,8 @@ +--- +- name: Ensure openssh is {{ openssh_state }} + ansible.builtin.include_tasks: + file: "install.yml" + +- name: Ensure sshd is configured + ansible.builtin.include_tasks: + file: "configure-sshd.yml"