24.3. Using the sshd RHEL system role for non-exclusive configuration
By default, applying the sshd RHEL system role overwrites the entire configuration. This may be problematic if you have previously adjusted the configuration with a different playbook. You can use the non-exclusive configuration to apply changes only to selected configuration options.
You can apply a non-exclusive configuration:
- In RHEL 8 and earlier by using a configuration snippet.
-
In RHEL 9 and later by using files in a drop-in directory. The default configuration file is already placed in the drop-in directory as
/etc/ssh/sshd_config.d/00-ansible_system_role.conf.
Prerequisites
- You have prepared the control node and the managed nodes.
- You are logged in to the control node as a user who can run playbooks on the managed nodes.
-
The account you use to connect to the managed nodes has
sudopermissions for these nodes.
Procedure
Create a playbook file, for example,
~/playbook.yml, with the following content:For managed nodes that run RHEL 8 or earlier:
--- - name: Non-exclusive sshd configuration hosts: managed-node-01.example.com tasks: - name: Configure SSHD to accept environment variables ansible.builtin.include_role: name: redhat.rhel_system_roles.sshd vars: sshd_config_namespace: <my_application> sshd_config: # Environment variables to accept AcceptEnv: LANG LS_COLORS EDITORFor managed nodes that run RHEL 9 or later:
- name: Non-exclusive sshd configuration hosts: managed-node-01.example.com tasks: - name: Configure sshd to accept environment variables ansible.builtin.include_role: name: redhat.rhel_system_roles.sshd vars: sshd_config_file: /etc/ssh/sshd_config.d/<42-my_application>.conf sshd_config: # Environment variables to accept AcceptEnv: LANG LS_COLORS EDITORThe settings specified in the example playbooks include the following:
sshd_config_namespace: <my_application>- The role places the configuration that you specify in the playbook to configuration snippets in the existing configuration file under the given namespace. You need to select a different namespace when running the role from a different context.
sshd_config_file: /etc/ssh/sshd_config.d/<42-my_application>.conf-
In the
sshd_config_filevariable, define the.conffile into which thesshdsystem role writes the configuration options. Use a two-digit prefix, for example42-to specify the order in which the configuration files will be applied. AcceptEnv:Controls which environment variables the OpenSSH server (
sshd) will accept from a client:-
LANG: defines the language and locale settings. -
LS_COLORS: defines the displaying color scheme for thelscommand in the terminal. -
EDITOR: specifies the default text editor for the command-line programs that need to open an editor.
-
For details about the role variables and the OpenSSH configuration options used in the playbook, see the
/usr/share/ansible/roles/rhel-system-roles.sshd/README.mdfile and thesshd_config(5)manual page on the control node.
Validate the playbook syntax:
$ ansible-playbook --syntax-check ~/playbook.ymlNote that this command only validates the syntax and does not protect against a wrong but valid configuration.
Run the playbook:
$ ansible-playbook ~/playbook.yml
Verification
Verify the configuration on the SSH server:
For managed nodes that run RHEL 8 or earlier:
# cat /etc/ssh/sshd_config ... # BEGIN sshd system role managed block: namespace <my_application> Match all AcceptEnv LANG LS_COLORS EDITOR # END sshd system role managed block: namespace <my_application>For managed nodes that run RHEL 9 or later:
# cat /etc/ssh/sshd_config.d/42-my_application.conf # Ansible managed # AcceptEnv LANG LS_COLORS EDITOR