tripleo_sshd: update for standalone roles

The role is refactored into install, config, and run tasks. The role's
tasks/main.yml imports all 3 to maintain backwards compatibility.

Change-Id: I9fa9488846485fc4a881e38de2e3fab6664242d2
Signed-off-by: James Slagle <jslagle@redhat.com>
This commit is contained in:
James Slagle 2022-05-17 08:50:33 -04:00
parent 7898061a31
commit 212936fb33
12 changed files with 316 additions and 114 deletions

View File

@ -2,3 +2,6 @@ export TRIPLEO_ANSIBLE_MOLECULE_COMMAND="/bin/sleep infinity"
export TRIPLEO_ANSIBLE_MOLECULE_VOLUMES="[]"
export TRIPLEO_ANSIBLE_MOLECULE_IMAGE="centos/centos:stream9"
export TRIPLEO_ANSIBLE_MOLECULE_REGISTRY="quay.io"
# Role specific vars
export TRIPLEO_ANSIBLE_SSHD_MOLECULE_VOLUMES="['/sys/fs/cgroup:/sys/fs/cgroup:rw']"

View File

@ -24,7 +24,9 @@
tripleo_sshd_package_state: present
tripleo_sshd_motd_enabled: false
tripleo_sshd_message_of_the_day: ''
tripleo_sshd_banner_enabled: false
tripleo_sshd_banner_text: ''
# SSH configuration options
tripleo_sshd_password_authentication: 'no'

View File

@ -2,6 +2,23 @@
driver:
name: podman
platforms:
- name: centos
hostname: centos
image: ${TRIPLEO_ANSIBLE_MOLECULE_IMAGE:-"ubi9/ubi-init"}
registry:
url: ${TRIPLEO_ANSIBLE_MOLECULE_REGISTRY:-"registry.access.redhat.com"}
dockerfile: ../default/Dockerfile.j2
pkg_extras: python*setuptools
command: "/sbin/init"
volumes: ${TRIPLEO_ANSIBLE_SSHD_MOLECULE_VOLUMES:-['/sys/fs/cgroup:/sys/fs/cgroup:rw','/etc/ci/mirror_info.sh:/etc/ci/mirror_info.sh:ro','/etc/pki/rpm-gpg:/etc/pki/rpm-gpg:O','/opt/yum.repos.d:/etc/yum.repos.d:O','/etc/dnf/vars:/etc/dnf/vars:O']}
privileged: true
environment: &env
http_proxy: "{{ lookup('env', 'http_proxy') }}"
https_proxy: "{{ lookup('env', 'https_proxy') }}"
ulimits: &ulimit
- host
provisioner:
name: ansible
inventory:
@ -23,6 +40,3 @@ scenario:
- check
- verify
- destroy
verifier:
name: testinfra

View File

@ -0,0 +1,35 @@
# Molecule managed
# Copyright 2019 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
{% if item.registry is defined %}
FROM {{ item.registry.url }}/{{ item.image }}
{% else %}
FROM {{ item.image }}
{% endif %}
RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates && apt-get clean; \
elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install sudo python*-devel python*-dnf bash {{ item.pkg_extras | default('') }} && dnf clean all; \
elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl python-setuptools bash {{ item.pkg_extras | default('') }} && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \
elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml {{ item.pkg_extras | default('') }} && zypper clean -a; \
elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates {{ item.pkg_extras | default('') }}; \
elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates {{ item.pkg_extras | default('') }} && xbps-remove -O; fi
{% for pkg in item.easy_install | default([]) %}
# install pip for centos where there is no python-pip rpm in default repos
RUN easy_install {{ pkg }}
{% endfor %}

View File

@ -2,6 +2,25 @@
driver:
name: podman
log: true
platforms:
- name: centos
hostname: centos
image: ${TRIPLEO_ANSIBLE_MOLECULE_IMAGE:-"ubi9/ubi-init"}
registry:
url: ${TRIPLEO_ANSIBLE_MOLECULE_REGISTRY:-"registry.access.redhat.com"}
dockerfile: Dockerfile.j2
pkg_extras: python*setuptools
command: "/sbin/init"
volumes: ${TRIPLEO_ANSIBLE_SSHD_MOLECULE_VOLUMES:-['/sys/fs/cgroup:/sys/fs/cgroup:rw','/etc/ci/mirror_info.sh:/etc/ci/mirror_info.sh:ro','/etc/pki/rpm-gpg:/etc/pki/rpm-gpg:O','/opt/yum.repos.d:/etc/yum.repos.d:O','/etc/dnf/vars:/etc/dnf/vars:O']}
privileged: true
environment: &env
http_proxy: "{{ lookup('env', 'http_proxy') }}"
https_proxy: "{{ lookup('env', 'https_proxy') }}"
ulimits: &ulimit
- host
provisioner:
name: ansible
inventory:
@ -23,6 +42,3 @@ scenario:
- check
- verify
- destroy
verifier:
name: testinfra

View File

@ -0,0 +1,40 @@
---
- name: Verify
hosts: all
tasks:
- name: load_vars tripleo_sshd
include_role:
name: tripleo_sshd
tasks_from: load_vars.yml
- name: Gather package facts
ansible.builtin.package_facts:
- name: Install the OpenSSH server
debug:
msg: "testing if {{ item }} installed"
failed_when: item not in ansible_facts['packages']
loop: "{{ tripleo_sshd_packages }}"
- name: Generate sshd host keys
shell: ls /etc/ssh/ssh_host_*
register: host_keys
failed_when: (host_keys.stdout | length) == 0
- name: Gather service facts
ansible.builtin.service_facts:
- debug:
var: ansible_facts['services']
- name: Enable sshd
debug:
msg: "test if sshd enabled"
failed_when: ansible_facts['services']['sshd.service']['status'] != 'enabled'
- name: Start sshd
debug:
msg: "test if sshd started"
failed_when: ansible_facts['services']['sshd.service']['state'] != 'running'

View File

@ -2,6 +2,23 @@
driver:
name: podman
platforms:
- name: centos
hostname: centos
image: ${TRIPLEO_ANSIBLE_MOLECULE_IMAGE:-"ubi9/ubi-init"}
registry:
url: ${TRIPLEO_ANSIBLE_MOLECULE_REGISTRY:-"registry.access.redhat.com"}
dockerfile: ../default/Dockerfile.j2
pkg_extras: python*setuptools systemd
command: "/sbin/init"
volumes: ${TRIPLEO_ANSIBLE_SSHD_MOLECULE_VOLUMES:-['/sys/fs/cgroup:/sys/fs/cgroup:rw','/etc/ci/mirror_info.sh:/etc/ci/mirror_info.sh:ro','/etc/pki/rpm-gpg:/etc/pki/rpm-gpg:O','/opt/yum.repos.d:/etc/yum.repos.d:O','/etc/dnf/vars:/etc/dnf/vars:O']}
privileged: true
environment: &env
http_proxy: "{{ lookup('env', 'http_proxy') }}"
https_proxy: "{{ lookup('env', 'https_proxy') }}"
ulimits: &ulimit
- host
provisioner:
name: ansible
inventory:
@ -23,6 +40,3 @@ scenario:
- check
- verify
- destroy
verifier:
name: testinfra

View File

@ -0,0 +1,90 @@
---
# Copyright 2022 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
- name: Import sshd load_vars tasks
import_tasks: load_vars.yml
- name: Run sshd tasks as root
become: true
block:
- name: PasswordAuthentication notice
debug:
msg: >-
Notice - The option `tripleo_sshd_password_authentication` is set to
"{{ tripleo_sshd_password_authentication }}" but `PermitRootLogin` is
undefined. While this may be perfectly valid, the sshd_config options
should be reviewed to ensure general user access is functional and
meeting expectations.
when:
- tripleo_sshd_password_authentication != 'no'
- not ('PermitRootLogin' in tripleo_sshd_server_options)
- name: PasswordAuthentication duplication notice
debug:
msg: >-
WARNING - The PasswordAuthentication has been configured in
`tripleo_sshd_server_options` but the values are different.
The `tripleo_sshd_password_authentication` value will be used.
when:
- ('PasswordAuthentication' in tripleo_sshd_server_options)
- tripleo_sshd_password_authentication != tripleo_sshd_server_options['PasswordAuthentication']
- name: Motd duplication notice
debug:
msg: >-
WARNING - The Banner or PrintMotd has been configured in
`tripleo_sshd_server_options`. These options may be ignored and
configured using values from `tripleo_sshd_banner_enabled` and
`tripleo_sshd_motd_enabled`
when:
- ('Banner' in tripleo_sshd_server_options or 'PrintMotd' in tripleo_sshd_server_options)
- name: Configure the banner text
copy:
content: "{{ tripleo_sshd_banner_text }}"
dest: /etc/issue
when:
- tripleo_sshd_banner_enabled | bool
- name: Configure the motd banner
copy:
content: "{{ tripleo_sshd_message_of_the_day }}"
dest: /etc/motd
when:
- tripleo_sshd_motd_enabled | bool
- name: Update sshd configuration options from vars
set_fact:
tripleo_sshd_server_options: |-
{% set _ = tripleo_sshd_server_options.__setitem__('PasswordAuthentication', tripleo_sshd_password_authentication) %}
{% if tripleo_sshd_banner_enabled %}
{% set _ = tripleo_sshd_server_options.__setitem__('Banner', '/etc/issue') %}
{% endif %}
{% if tripleo_sshd_motd_enabled %}
{% set _ = tripleo_sshd_server_options.__setitem__('PrintMotd', 'yes') %}
{% endif %}
{{ tripleo_sshd_server_options }}
- name: Adjust ssh server configuration
template:
dest: /etc/ssh/sshd_config
src: sshd_config_block.j2
validate: '/usr/sbin/sshd -T -f %s'
register: _sshd_config_result
- name: Set sshd config changed fact
set_fact:
_sshd_config_result_changed: _sshd_config_result.changed

View File

@ -0,0 +1,34 @@
---
# Copyright 2022 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
- name: Import sshd load_vars tasks
import_tasks: load_vars.yml
- name: Run sshd tasks as root
become: true
block:
- name: Install the OpenSSH server
package:
name: "{{ tripleo_sshd_packages }}"
state: "{{ tripleo_sshd_package_state }}"
register: _sshd_install_result
# NOTE(mwhahaha): we need this here because in order to validate our generated
# config, we need to ensure the host keys exist
- name: Generate sshd host keys
shell: ssh-keygen -A
when:
- _sshd_install_result.changed

View File

@ -0,0 +1,15 @@
---
- name: Gather variables for each operating system
include_vars: "{{ item }}"
with_first_found:
- skip: true
files:
- "{{ ansible_facts['distribution'] | lower }}-{{ ansible_facts['distribution_version'] | lower }}.yml"
- "{{ ansible_facts['distribution'] | lower }}-{{ ansible_facts['distribution_major_version'] | lower }}.yml"
- "{{ ansible_facts['os_family'] | lower }}-{{ ansible_facts['distribution_major_version'] | lower }}.yml"
- "{{ ansible_facts['distribution'] | lower }}.yml"
- "{{ ansible_facts['os_family'] | lower }}-{{ ansible_facts['distribution_version'].split('.')[0] }}.yml"
- "{{ ansible_facts['os_family'] | lower }}.yml"
tags:
- always

View File

@ -1,5 +1,5 @@
---
# Copyright 2019 Red Hat, Inc.
# Copyright 2022 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
@ -14,110 +14,14 @@
# License for the specific language governing permissions and limitations
# under the License.
# found within the "vars/" path. If no OS files are found the task will skip.
- name: Gather variables for each operating system
include_vars: "{{ item }}"
with_first_found:
- skip: true
files:
- "{{ ansible_facts['distribution'] | lower }}-{{ ansible_facts['distribution_version'] | lower }}.yml"
- "{{ ansible_facts['distribution'] | lower }}-{{ ansible_facts['distribution_major_version'] | lower }}.yml"
- "{{ ansible_facts['os_family'] | lower }}-{{ ansible_facts['distribution_major_version'] | lower }}.yml"
- "{{ ansible_facts['distribution'] | lower }}.yml"
- "{{ ansible_facts['os_family'] | lower }}-{{ ansible_facts['distribution_version'].split('.')[0] }}.yml"
- "{{ ansible_facts['os_family'] | lower }}.yml"
tags:
- always
- name: Import sshd load_vars tasks
import_tasks: load_vars.yml
- name: Run sshd tasks as root
become: true
block:
- name: Install the OpenSSH server
package:
name: "{{ tripleo_sshd_packages }}"
state: "{{ tripleo_sshd_package_state }}"
register: _sshd_install_result
- name: Import sshd install tasks
import_tasks: install.yml
# NOTE(mwhahaha): we need this here because in order to validate our generated
# config, we need to ensure the host keys exists which happens on initial
# startup
- name: Start sshd
systemd:
name: sshd
state: restarted
enabled: true
when:
- _sshd_install_result.changed
- name: Import sshd configure tasks
import_tasks: configure.yml
- name: PasswordAuthentication notice
debug:
msg: >-
Notice - The option `tripleo_sshd_password_authentication` is set to
"{{ tripleo_sshd_password_authentication }}" but `PermitRootLogin` is
undefined. While this may be perfectly valid, the sshd_config options
should be reviewed to ensure general user access is functional and
meeting expectations.
when:
- (tripleo_sshd_password_authentication != 'no') and
not ('PermitRootLogin' in tripleo_sshd_server_options)
- name: PasswordAuthentication duplication notice
debug:
msg: >-
WARNING - The PasswordAuthentication has been configured in
`tripleo_sshd_server_options` but the values are different.
The `tripleo_sshd_password_authentication` value will be used.
when:
- ('PasswordAuthentication' in tripleo_sshd_server_options and
tripleo_sshd_password_authentication != tripleo_sshd_server_options['PasswordAuthentication'])
- name: Motd duplication notice
debug:
msg: >-
WARNING - The Banner or PrintMotd has been configured in
`tripleo_sshd_server_options`. These options may be ignored and
configured using values from `tripleo_sshd_banner_enabled` and
`tripleo_sshd_motd_enabled`
when:
- ('Banner' in tripleo_sshd_server_options or 'PrintMotd' in tripleo_sshd_server_options)
- name: Configure the banner text
copy:
content: "{{ tripleo_sshd_banner_text }}"
dest: /etc/issue
when:
- tripleo_sshd_banner_enabled | bool
- name: Configure the motd banner
copy:
content: "{{ tripleo_sshd_message_of_the_day }}"
dest: /etc/motd
when:
- tripleo_sshd_motd_enabled | bool
- name: Update sshd configuration options from vars
set_fact:
tripleo_sshd_server_options: |-
{% set _ = tripleo_sshd_server_options.__setitem__('PasswordAuthentication', tripleo_sshd_password_authentication) %}
{% if tripleo_sshd_banner_enabled %}
{% set _ = tripleo_sshd_server_options.__setitem__('Banner', '/etc/issue') %}
{% endif %}
{% if tripleo_sshd_motd_enabled %}
{% set _ = tripleo_sshd_server_options.__setitem__('PrintMotd', 'yes') %}
{% endif %}
{{ tripleo_sshd_server_options }}
- name: Adjust ssh server configuration
template:
dest: /etc/ssh/sshd_config
src: sshd_config_block.j2
validate: '/usr/sbin/sshd -T -f %s'
register: _sshd_config_result
- name: Restart sshd
systemd:
name: sshd
state: restarted
enabled: true
when:
- _sshd_config_result.changed
- name: Import sshd run tasks
import_tasks: run.yml

View File

@ -0,0 +1,35 @@
---
# Copyright 2022 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
- name: Run sshd tasks as root
become: true
block:
- name: Enable sshd
systemd:
name: sshd
enabled: true
- name: Start sshd
systemd:
name: sshd
state: started
- name: Restart sshd due to config change
systemd:
name: sshd
state: reloaded
when:
- _sshd_config_result_changed | default(false)