2018-08-25 23:39:06 +02:00
|
|
|
---
|
2020-03-29 15:51:27 +02:00
|
|
|
- name: OS is supported
|
|
|
|
meta: end_host
|
2020-09-14 17:48:04 +02:00
|
|
|
when:
|
2020-03-29 15:51:27 +02:00
|
|
|
- not __sshd_os_supported|bool
|
2018-08-25 23:39:06 +02:00
|
|
|
|
|
|
|
- name: Install ssh packages
|
|
|
|
package:
|
2018-09-08 10:13:51 +02:00
|
|
|
name: "{{ sshd_packages }}"
|
2018-08-25 23:39:06 +02:00
|
|
|
state: present
|
|
|
|
|
2020-10-06 21:11:35 +02:00
|
|
|
- name: Sysconfig configuration
|
|
|
|
template:
|
|
|
|
src: sysconfig.j2
|
|
|
|
dest: "/etc/sysconfig/sshd"
|
|
|
|
owner: "root"
|
|
|
|
group: "root"
|
|
|
|
mode: "600"
|
|
|
|
backup: "{{ sshd_backup }}"
|
|
|
|
when:
|
|
|
|
- sshd_sysconfig|bool
|
2020-11-23 15:24:52 +01:00
|
|
|
- __sshd_sysconfig_supports_use_strong_rng or __sshd_sysconfig_supports_crypto_policy
|
2020-10-06 21:11:35 +02:00
|
|
|
notify: reload_sshd
|
|
|
|
|
2020-10-23 21:10:00 +02:00
|
|
|
- name: Make sure hostkeys are available and have expected permissions
|
2020-11-05 17:02:33 +01:00
|
|
|
vars: &share_vars
|
2020-10-23 21:10:00 +02:00
|
|
|
# This mimics the macro body_option() in sshd_config.j2
|
|
|
|
# The explicit to_json filter is needed for Python 2 compatibility
|
2020-11-05 17:02:33 +01:00
|
|
|
__sshd_hostkeys_from_config: >-
|
|
|
|
{% if sshd_HostKey is defined %}
|
|
|
|
{{ sshd_HostKey | to_json }}
|
|
|
|
{% elif sshd['HostKey'] is defined %}
|
|
|
|
{{ sshd['HostKey'] | to_json }}
|
|
|
|
{% elif __sshd_defaults['HostKey'] is defined and not sshd_skip_defaults %}
|
|
|
|
{{ __sshd_defaults['HostKey'] | to_json }}
|
|
|
|
{% else %}
|
|
|
|
[]
|
|
|
|
{% endif %}
|
2020-10-23 21:10:00 +02:00
|
|
|
__sshd_verify_hostkeys: >-
|
|
|
|
{% if not sshd_verify_hostkeys %}
|
|
|
|
[]
|
|
|
|
{% elif sshd_verify_hostkeys == 'auto' %}
|
2020-11-21 00:23:30 +01:00
|
|
|
{% if sshd_HostKey is string %}
|
|
|
|
[ {{ __sshd_hostkeys_from_config }} ]
|
|
|
|
{% else %}
|
|
|
|
{{ __sshd_hostkeys_from_config }}
|
|
|
|
{% endif %}
|
2020-10-23 21:10:00 +02:00
|
|
|
{% else %}
|
|
|
|
{{ sshd_verify_hostkeys | to_json }}
|
|
|
|
{% endif %}
|
|
|
|
block:
|
|
|
|
- name: Make sure hostkeys are available
|
2021-04-07 20:12:03 +02:00
|
|
|
shell: |
|
|
|
|
set -eu
|
|
|
|
if set -o | grep pipefail 2>&1 /dev/null ; then
|
|
|
|
set -o pipefail
|
|
|
|
fi
|
2020-10-23 21:10:00 +02:00
|
|
|
{% if sshd_sysconfig %}
|
2021-04-07 20:12:03 +02:00
|
|
|
source /etc/sysconfig/sshd
|
2020-10-23 21:10:00 +02:00
|
|
|
{% endif %}
|
|
|
|
ssh-keygen -q -t {{ item | regex_search('(rsa|dsa|ecdsa|ed25519)') }} -f {{ item }} -C '' -N ''
|
|
|
|
args:
|
|
|
|
creates: "{{ item }}"
|
|
|
|
loop: "{{ __sshd_verify_hostkeys | from_json | list }}"
|
2021-04-07 20:12:03 +02:00
|
|
|
changed_when: false
|
2020-10-23 21:10:00 +02:00
|
|
|
|
|
|
|
- name: Make sure private hostkeys have expected permissions
|
|
|
|
file:
|
|
|
|
path: "{{ item }}"
|
|
|
|
owner: "{{ sshd_hostkey_owner }}"
|
|
|
|
group: "{{ sshd_hostkey_group }}"
|
|
|
|
mode: "{{ sshd_hostkey_mode }}"
|
|
|
|
loop: "{{ __sshd_verify_hostkeys | from_json | list }}"
|
|
|
|
|
|
|
|
- name: Apply configuration
|
2020-11-05 17:02:33 +01:00
|
|
|
vars:
|
|
|
|
<<: *share_vars
|
|
|
|
block:
|
|
|
|
- name: Create a temporary hostkey for syntax verification if needed
|
|
|
|
tempfile:
|
|
|
|
state: directory
|
|
|
|
register: sshd_test_hostkey
|
2021-04-07 20:12:03 +02:00
|
|
|
changed_when: false
|
2020-11-05 17:02:33 +01:00
|
|
|
when:
|
|
|
|
- __sshd_hostkeys_from_config | from_json == []
|
|
|
|
- sshd_config_file != "/etc/ssh/sshd_config"
|
|
|
|
|
|
|
|
- name: Generate temporary hostkey
|
2021-04-07 20:12:03 +02:00
|
|
|
command: >
|
|
|
|
ssh-keygen -q -t rsa -f '{{ sshd_test_hostkey.path }}/rsa_key' -C '' -N ''
|
|
|
|
changed_when: false
|
2020-11-05 17:02:33 +01:00
|
|
|
when: sshd_test_hostkey.path is defined
|
|
|
|
|
2020-11-23 10:00:58 +01:00
|
|
|
- name: Make sure sshd runtime directory is present
|
|
|
|
file:
|
|
|
|
path: "{{ __sshd_runtime_directory }}"
|
|
|
|
state: directory
|
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: "{{ __sshd_runtime_directory_mode }}"
|
|
|
|
when:
|
|
|
|
- __sshd_runtime_directory | d(false) | bool
|
|
|
|
|
2021-05-20 16:52:29 +02:00
|
|
|
- name: Create the complete configuration file
|
2020-11-05 17:02:33 +01:00
|
|
|
template:
|
2021-05-20 16:52:29 +02:00
|
|
|
src: sshd_config_snippet.j2
|
2020-11-05 17:02:33 +01:00
|
|
|
dest: "{{ sshd_config_file }}"
|
|
|
|
owner: "{{ sshd_config_owner }}"
|
|
|
|
group: "{{ sshd_config_group }}"
|
|
|
|
mode: "{{ sshd_config_mode }}"
|
|
|
|
validate: >-
|
|
|
|
{% if sshd_test_hostkey is defined and sshd_test_hostkey.path is defined %}
|
|
|
|
{{ sshd_binary }} -t -f %s -h {{ sshd_test_hostkey.path }}/rsa_key
|
|
|
|
{% else %}
|
|
|
|
{{ sshd_binary }} -t -f %s
|
|
|
|
{% endif %}
|
|
|
|
backup: "{{ sshd_backup }}"
|
|
|
|
notify: reload_sshd
|
2021-05-20 16:52:29 +02:00
|
|
|
when: sshd_namespace_append is none
|
|
|
|
|
|
|
|
- name: Update configuration file snippet
|
|
|
|
blockinfile:
|
|
|
|
path: "{{ sshd_config_file }}"
|
|
|
|
owner: "{{ sshd_config_owner }}"
|
|
|
|
group: "{{ sshd_config_group }}"
|
|
|
|
mode: "{{ sshd_config_mode }}"
|
|
|
|
block: |
|
|
|
|
Match all
|
|
|
|
{{ lookup('template', 'sshd_config_snippet.j2') }}
|
|
|
|
create: yes
|
|
|
|
marker: "# {mark} sshd system role managed block: namespace {{ sshd_namespace_append }}"
|
|
|
|
validate: >-
|
|
|
|
{% if sshd_test_hostkey is defined and sshd_test_hostkey.path is defined %}
|
|
|
|
{{ sshd_binary }} -t -f %s -h {{ sshd_test_hostkey.path }}/rsa_key
|
|
|
|
{% else %}
|
|
|
|
{{ sshd_binary }} -t -f %s
|
|
|
|
{% endif %}
|
|
|
|
backup: "{{ sshd_backup }}"
|
|
|
|
notify: reload_sshd
|
|
|
|
when: sshd_namespace_append is not none
|
|
|
|
|
2020-11-05 17:02:33 +01:00
|
|
|
rescue:
|
|
|
|
- name: re-raise the error
|
|
|
|
fail:
|
|
|
|
msg: "{{ ansible_failed_result }}"
|
|
|
|
always:
|
|
|
|
- name: Remove temporary host keys
|
|
|
|
file:
|
|
|
|
path: "{{ sshd_test_hostkey.path }}"
|
|
|
|
state: absent
|
2021-04-07 20:12:03 +02:00
|
|
|
changed_when: false
|
2020-11-05 17:02:33 +01:00
|
|
|
when: sshd_test_hostkey.path is defined
|
2020-10-23 21:10:00 +02:00
|
|
|
|
2018-08-25 23:39:06 +02:00
|
|
|
- name: Install systemd service files
|
|
|
|
block:
|
2019-05-23 19:31:30 +02:00
|
|
|
- name: Install service unit file
|
|
|
|
template:
|
2018-08-25 23:39:06 +02:00
|
|
|
src: "{{ sshd_service_template_service }}"
|
|
|
|
dest: "/etc/systemd/system/{{ sshd_service }}.service"
|
2020-09-18 21:49:22 +02:00
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: "0644"
|
2018-08-25 23:39:06 +02:00
|
|
|
notify: reload_sshd
|
2019-05-23 19:31:30 +02:00
|
|
|
- name: Install instanced service unit file
|
|
|
|
template:
|
2018-08-25 23:39:06 +02:00
|
|
|
src: "{{ sshd_service_template_at_service }}"
|
|
|
|
dest: "/etc/systemd/system/{{ sshd_service }}@.service"
|
2020-09-18 21:49:22 +02:00
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: "0644"
|
2018-08-25 23:39:06 +02:00
|
|
|
notify: reload_sshd
|
2019-05-23 19:31:30 +02:00
|
|
|
- name: Install socket unit file
|
|
|
|
template:
|
2018-08-25 23:39:06 +02:00
|
|
|
src: "{{ sshd_service_template_socket }}"
|
|
|
|
dest: "/etc/systemd/system/{{ sshd_service }}.socket"
|
2020-09-18 21:49:22 +02:00
|
|
|
owner: root
|
|
|
|
group: root
|
|
|
|
mode: "0644"
|
2018-08-25 23:39:06 +02:00
|
|
|
notify: reload_sshd
|
2019-05-23 19:37:09 +02:00
|
|
|
when: sshd_install_service|bool
|
2018-08-25 23:39:06 +02:00
|
|
|
|
|
|
|
- name: Service enabled and running
|
|
|
|
service:
|
|
|
|
name: "{{ sshd_service }}"
|
|
|
|
enabled: true
|
|
|
|
state: started
|
2018-09-11 15:10:58 +02:00
|
|
|
when:
|
2019-05-23 19:37:09 +02:00
|
|
|
- sshd_manage_service|bool
|
2018-09-11 15:10:58 +02:00
|
|
|
- ansible_virtualization_type|default(None) != 'docker'
|
2020-11-23 15:08:09 +01:00
|
|
|
- ansible_virtualization_type|default(None) != 'podman'
|
2021-04-07 20:12:03 +02:00
|
|
|
- ansible_virtualization_type|default(None) != 'VirtualPC' # for Github Actions
|
2018-09-11 15:10:58 +02:00
|
|
|
- ansible_connection != 'chroot'
|
|
|
|
|
|
|
|
# Due to ansible bug 21026, cannot use service module on RHEL 7
|
|
|
|
- name: Enable service in chroot
|
2019-05-23 19:37:58 +02:00
|
|
|
command: systemctl enable {{ sshd_service }} # noqa 303
|
2018-09-11 15:10:58 +02:00
|
|
|
when:
|
|
|
|
- ansible_connection == 'chroot'
|
|
|
|
- ansible_os_family == 'RedHat'
|
|
|
|
- ansible_distribution_major_version|int >= 7
|
2018-08-25 23:39:06 +02:00
|
|
|
|
|
|
|
- name: Register that this role has run
|
2018-09-08 10:13:51 +02:00
|
|
|
set_fact:
|
|
|
|
sshd_has_run: true
|
2018-08-25 23:39:06 +02:00
|
|
|
when: sshd_has_run is not defined
|