No description
  • Jinja 62.7%
  • Python 29.8%
  • Shell 7.5%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Richard Megginson 08b8884aa1
test: check invalid inputs for correct error message (#397)
In tests_invalid_input we need to check that the role returns the correct error
message to ensure that the role is failing in the correct way, with an error that
is meaningful and actionable for the end user.

Assisted-by: Codex model GPT-6-Sol medium

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
2026-11-04 12:32:22 -07:00
.github build(deps): bump Jakuje/check-ansible-action (#396) 2026-11-03 10:06:10 -07:00
.ostree feat: Add new options from OpenSSH 10.0 (#312) 2025-06-19 06:34:26 -06:00
defaults feat: Add SLE16 support and keep the SLE15 crypto policy (#393) 2026-11-03 10:03:24 -07:00
examples fix: rename var sshd -> sshd_config and debug output (#299) 2024-10-24 17:59:04 +01:00
handlers Configure selection between socket and service 2025-08-05 18:08:31 +02:00
library feat: Write roles fingerprints to /var/log/sysroles.jsonl (#381) 2026-08-10 07:11:43 -06:00
meta ci: fix make_options_list to use the spec parameter to read_options (#399) 2026-11-03 14:52:28 -07:00
tasks feat: Add SLE16 support and keep the SLE15 crypto policy (#393) 2026-11-03 10:03:24 -07:00
templates feat: Add SLE16 support and keep the SLE15 crypto policy (#393) 2026-11-03 10:03:24 -07:00
tests test: check invalid inputs for correct error message (#397) 2026-11-04 12:32:22 -07:00
vars feat: Add SLE16 support and keep the SLE15 crypto policy (#393) 2026-11-03 10:03:24 -07:00
.ansible-lint fix: rename var sshd -> sshd_config and debug output (#299) 2024-10-24 17:59:04 +01:00
.commitlintrc.js Move commitlint.config.js to hidden .commitlintrc.js 2023-06-15 11:33:51 +02:00
.gitignore ci: Use supported ansible-lint action; run ansible-lint against the collection 2024-01-08 10:56:53 -07:00
.markdownlint.yaml ci: Use supported ansible-lint action; run ansible-lint against the collection 2024-01-08 10:56:53 -07:00
.pandoc_template.html5 ci: Add markdownlint, test_converting_readme, and build_docs workflows 2023-08-29 15:29:17 +02:00
.pre-commit-config.yaml feat: Ubuntu noble (#290) 2024-06-21 09:12:02 +01:00
.README.html docs(changelog): version v0.32.0 [citest_skip] (#365) 2026-05-06 12:53:17 -06:00
.sanity-ansible-ignore-2.14.txt feat: add role fingerprints to syslog (#361) 2026-04-30 08:17:04 -06:00
.sanity-ansible-ignore-2.16.txt feat: add role fingerprints to syslog (#361) 2026-04-30 08:17:04 -06:00
.sanity-ansible-ignore-2.17.txt feat: add role fingerprints to syslog (#361) 2026-04-30 08:17:04 -06:00
.sanity-ansible-ignore-2.18.txt feat: add role fingerprints to syslog (#361) 2026-04-30 08:17:04 -06:00
.sanity-ansible-ignore-2.19.txt feat: add role fingerprints to syslog (#361) 2026-04-30 08:17:04 -06:00
.sanity-ansible-ignore-2.20.txt feat: add role fingerprints to syslog (#361) 2026-04-30 08:17:04 -06:00
.sanity-ansible-ignore-2.21.txt feat: add role fingerprints to syslog (#361) 2026-04-30 08:17:04 -06:00
.sanity-ansible-ignore-2.22.txt feat: add role fingerprints to syslog (#361) 2026-04-30 08:17:04 -06:00
.yamllint.yml feat: Ubuntu noble (#290) 2024-06-21 09:12:02 +01:00
CHANGELOG.md docs(changelog): version v0.34.0 [citest_skip] (#382) 2026-08-10 07:45:34 -06:00
CODE_OF_CONDUCT.md feat: Ubuntu noble (#290) 2024-06-21 09:12:02 +01:00
contributing.md docs: Fix spelling issues + fix reported issues (#274) 2024-01-29 17:55:43 +00:00
LICENSE Use LGPL license 2014-12-26 10:09:34 +00:00
README-ostree.md ci: ensure testing with INJECT_FACTS_AS_VARS=false (#348) 2026-01-08 14:12:11 -07:00
README.md feat: Add SLE16 support and keep the SLE15 crypto policy (#393) 2026-11-03 10:03:24 -07:00
tox.ini ci: tox.ini is not executable (#351) 2026-01-29 11:42:24 -07:00

OpenSSH Server

Ansible Lint Ansible Galaxy

This role configures the OpenSSH daemon. It:

  • By default configures the SSH daemon with the normal OS defaults.
  • Works across a variety of UN*X distributions
  • Can be configured by dict or simple variables
  • Supports Match sets
  • Supports all sshd_config options. Templates are programmatically generated. (see meta/make_option_lists)
  • List-valued options are rendered using the correct sshd_config syntax for that option (comma-separated, space-separated, or repeated keyword lines)
  • Tests the sshd_config before reloading sshd.

WARNING Misconfiguration of this role can lock you out of your server! Please test your configuration and its interaction with your users configuration before using in production!

WARNING Digital Ocean allows root with passwords via SSH on Debian and Ubuntu. This is not the default assigned by this module - it will set PermitRootLogin without-password which will allow access via SSH key but not via simple password. If you need this functionality, be sure to set sshd_PermitRootLogin yes for those hosts.

NOTE The sshd service is reloaded/restarted automatically, only if the role is invoked using roles keyword. Using include_role won't trigger handlers as described in the Ansible 'taskify includes' proposal. To work around this, call meta: flush_handlers as detailed in the official Ansible documentation. If you need to invoke the handlers in this case, use meta: flush_handlers.

NOTE Due to the change #390 the role will render lists differently for some list-valued parameters which will cause the role to report changed: true with no specified changes when first run after upgrading the role. This is expected behavior and the role will report changed: false for subsequent runs if nothing changes.

Requirements

Tested on:

  • Ubuntu precise, trusty, xenial, bionic, focal, jammy, noble
    • Run tests on Ubuntu latest
  • Debian 11 (Bullseye), 12 (Bookworm), 13 (Trixie)
    • Run tests on Debian
  • EL 6, 7, 8, 9, 10 derived distributions
    • Run tests on CentOS
  • All Fedora
    • Run tests on Fedora latest
  • Latest Alpine
    • Run tests on Alpine
  • FreeBSD 10.1
  • OpenBSD 6.0
  • AIX 7.1, 7.2
  • OpenWrt 21.03
  • NetBSD 10.1
  • SUSE Linux Enterprise 15 SP7 and 16, openSUSE Leap 16

It will likely work on other flavours and more direct support via suitable vars/ files is welcome.

Optional requirements

If you want to use advanced functionality of this role that can configure firewall and selinux for you, which is mostly useful when custom port is used, the role requires additional collections which are specified in meta/collection-requirements.yml. These are not automatically installed. If you want to manage rpm-ostree systems, additional collections are required. On SUSE, community.general is also needed to install packages. You must install them like this:

ansible-galaxy install -vv -r meta/collection-requirements.yml

For more information, see sshd_manage_firewall and sshd_manage_selinux options below, and the rpm-ostree section. This additional functionality is supported only on Red Hat based Linux, and sshd_manage_selinux also on SLES 16 and openSUSE Leap 16.

Role variables

Primary role variables

Unconfigured, this role will provide a sshd_config that matches the OS default, minus the comments and in a different order.

sshd_enable

If set to false, the role will be completely disabled. Defaults to true.

sshd_enable_fingerprint

If set to true, the role records begin and success message strings in the system log via syslog (visible in journalctl on systemd hosts). Defaults to false.

sshd_skip_defaults

If set to true, don't apply default values. This means that you must have a complete set of configuration defaults via either the sshd dict, or sshd_Key variables. Defaults to false unless sshd_config_namespace is set or sshd_config_file points to a drop-in directory to avoid recursive include.

sshd_manage_service

If set to false, the service/daemon won't be managed at all, i.e. will not try to enable on boot or start or reload the service. Defaults to true unless: Running inside a docker container (it is assumed ansible is used during build phase) or AIX (Ansible service module does not currently support enabled for AIX)

sshd_systemd_unit

Selection among service and socket, which is used in systemd to handle the sshd connection. Only one can be active at a time, otherwise both sshd and systemd will try to bind the same port and one of them will fail. Default depends on OS. Most of them use service, but recent Ubuntu switched to using socket by default.

sshd_allow_reload

If set to false, a reload of sshd service won't happen on change. This can help with troubleshooting. You'll need to manually reload sshd if you want to apply the changed configuration. Defaults to true.

sshd_allow_restart

Some changes, for example of the sysconfig and environment files require the full restart of the service. If set to false, a restart of sshd service won't happen on these changes. This can help with troubleshooting. You'll need to manually restart sshd service if you want to apply the changed configuration. Defaults to true (except on AIX where the reload is handled by specific restart command and this option does not have any effect).

sshd_socket_allow_restart

The systemd can run sshd either as a service or a socket, handling part of the network communication (default depends on the OS). After making changes to the socket unit or to the configuration defining the ports and addresses the systemd listens to, the socket unit needs to be restarted. If set to false, a restart of sshd socket won't happen on these changes. This can help with troubleshooting. You'll need to manually restart sshd socket if you want to apply the changed configuration. Defaults to true.

sshd_allow_daemon_reload

The systemd daemon needs to be reloaded in the following cases:

  • When the systemd is used to manage the service and/or socket
  • When the ports or listen addresses are changed to non-default values and they are automatically picked up by the sshd-socket-generator (such as in Ubuntu 24.04).

If set to false, a the systemd daemon won't be reloaded on these changes. This can help with troubleshooting. You'll need to manually reload the systemd daemon if you want to apply the changed configuration. Defaults to true.

sshd_install_service

If set to true, the role will install service files for the ssh service. Defaults to false.

The templates for the service files to be used are pointed to by the variables

  • sshd_service_template_service (default: templates/sshd.service.j2)
  • sshd_service_template_at_service (default: templates/sshd@.service.j2)
  • sshd_service_template_socket (default: templates/sshd.socket.j2)

Using these variables, you can use your own custom templates. With the above default templates, the name of the installed ssh service will be provided by the sshd_service variable.

sshd_manage_firewall

If set to true, the SSH port(s) will be opened in firewall. Note, this works only on Red Hat based OS. The default is false.

NOTE: sshd_manage_firewall is limited to adding ports. It cannot be used for removing ports. If you want to remove ports, you will need to use the firewall system role directly.

sshd_manage_selinux

If set to true, the selinux will be configured to allow sshd listening on the given SSH port(s). Note, this works only on Red Hat based OS, SLES 16 and openSUSE Leap 16. The default is false.

NOTE: sshd_manage_selinux is limited to adding policy. It cannot be used for removing policy. If you want to remove ports, you will need to use the selinux system role directly.

sshd_config

A dict containing configuration. e.g.

sshd_config:
  Compression: delayed
  ListenAddress:
    - 0.0.0.0

Note: This variable was previous called sshd. sshd is can still be used but is deprecated and will be removed in a future release.

sshd_<OptionName>

Simple variables can be used rather than a dict. Simple values override dict values. e.g.:

sshd_Compression: off

In all cases, booleans are correctly rendered as yes and no in sshd configuration. Lists can be used for multiline configuration items. e.g.

sshd_ListenAddress:
  - 0.0.0.0
  - '::'

Renders as:

ListenAddress 0.0.0.0
ListenAddress ::

Some options are instead rendered as a single line, with the list items joined by a separator, to match the syntax sshd_config expects for that option. For example Ciphers, MACs and similar algorithm lists are comma-separated:

Ciphers:
  - aes256-ctr
  - aes192-ctr
  - aes128-ctr

Renders as:

Ciphers aes256-ctr,aes192-ctr,aes128-ctr

while options such as AllowUsers, DenyUsers and AuthorizedKeysFile are space-separated:

AllowUsers:
  - alice
  - bob

Renders as:

AllowUsers alice bob

AuthenticationMethods is an oddball, expecting a space-separated list of alternative lists of required authentication methods, where the methods within one set are comma-separated (method1,method2 method3). As a list of lists is clumsy to write in YAML, each alternative can be written as a string in sshd's own comma-separated syntax:

AuthenticationMethods:
  - publickey,password
  - publickey

or as an inner list of methods, in which case the role does the comma-joining:

AuthenticationMethods: [["publickey", "password"], ["publickey"]]

The same nested lists can be written in block style, which is clumsy to write and read, and tends to upset linters:

AuthenticationMethods:
  - - publickey
    - password
  - - publickey

A null member of an inner list is left out.

Finally, the whole value can be given as a single string, exactly as it would appear in sshd_config:

AuthenticationMethods: publickey,password publickey

All of these examples render as:

AuthenticationMethods publickey,password publickey

The same applies to the other options rendered on a single line: a string in the syntax sshd expects is passed through unchanged, so Ciphers: aes256-ctr,aes128-ctr and AllowUsers: alice bob work as well as the list forms shown above.

Which options use which rendering is fixed per-option (see meta/make_option_lists for the full list).

OpenSSH's list syntax allows the FIRST member of the list to be prefixed with +, -, or ^, influencing the semantics of the complete list. When joining lists, for example from global, group, and host variables, this might cause surprising results when merging lists from different parts of your inventory.

sshd_match, sshd_match_1 through sshd_match_9

A list of dicts or just a dict for a Match section. Note, that these variables do not override match blocks as defined in the sshd dict. All of the sources will be reflected in the resulting configuration file. The use of sshd_match_* variant is deprecated and no longer recommended.

In a match block, the option is spelled X11UseLocalhost, like everywhere else. The misspelling X11UseLocalHost is accepted as well, for compatibility. If both are given in the same block, the correct spelling is used.

sshd_backup

When set to false, the original sshd_config file is not backed up. Default is true.

sshd_sysconfig

On RHEL-based systems, sysconfig is used for configuring more details of sshd service. If set to true, this role will manage also the /etc/sysconfig/sshd configuration file based on the following configurations. Default is false.

sshd_sysconfig_override_crypto_policy

In RHEL8-based systems, this can be used to override system-wide crypto policy by setting to true. Without this option, changes to ciphers, MACs, public key algorithms will have no effect on the resulting service in RHEL8. Defaults to false.

sshd_sysconfig_use_strong_rng

In RHEL-based systems (before RHEL9), this can be used to force sshd to reseed openssl random number generator with the given amount of bytes as an argument. The default is 0, which disables this functionality. It is not recommended to turn this on if the system does not have hardware random number generator.

sshd_config_file

The path where the openssh configuration produced by this role should be saved. This is useful mostly when generating configuration snippets to Include from drop-in directory (default in Fedora and RHEL9).

When this path points to a drop-in directory (like /etc/ssh/sshd_config.d/00-custom.conf), the main configuration file (defined with the variable sshd_main_config_file) is checked to contain a proper Include directive.

sshd_main_config_file

When the system is using drop-in directory, this option can be used to set a path to the main configuration file and let you configure only the drop-in configuration file using sshd_config_file. This is useful in cases when you need to configure second independent sshd service with different configuration file. This is also the file used in the service file.

On systems without drop-in directory, it defaults to None. Otherwise it defaults to /etc/ssh/sshd_config. When the sshd_config_file is set outside of the drop in directory (its parent directory is not sshd_main_config_file ~ '.d'), this variable is ignored.

On SLES 16 and openSUSE Leap 16, a missing /etc/ssh/sshd_config is created as a copy of /usr/etc/ssh/sshd_config; an existing one is kept. Package updates to /usr/etc/ssh/sshd_config do not reach the copy.

sshd_config_namespace

By default (null), the role defines whole content of the configuration file including system defaults. You can use this variable to invoke this role from other roles or from multiple places in a single playbook as an alternative to using a drop-in directory. The sshd_skip_defaults is ignored and no system defaults are used in this case.

When this variable is set, the role places the configuration that you specify to configuration snippets in a existing configuration file under the given namespace. You need to select different namespaces when invoking the role several times.

Note that limitations of the openssh configuration file still apply. For example, only the first option specified in a configuration file is effective for most of the variables.

Technically, the role places snippets in Match all blocks, unless they contain other match blocks, to ensure they are applied regardless of the previous match blocks in the existing configuration file. This allows configuring any non-conflicting options from different roles invocations.

sshd_config_owner, sshd_config_group, sshd_config_mode

Use these variables to set the ownership and permissions for the openssh configuration file that this role produces.

sshd_verify_hostkeys

By default (auto), this list contains all the host keys that are present in the produced configuration file. If there are none, the OpenSSH default list will be used after excluding non-FIPS approved keys in FIPS mode. The paths are checked for presence and new keys are generated if they are missing. Additionally, permissions and file owners are set to sane defaults. This is useful if the role is used in deployment stage to make sure the service is able to start on the first attempt.

To disable this check, set this to empty list.

sshd_hostkey_owner, sshd_hostkey_group, sshd_hostkey_mode

Use these variables to set the ownership and permissions for the host keys from the above list.

Secondary role variables

These variables are used by the role internals and can be used to override the defaults that correspond to each supported platform. They are not tested and generally are not needed as the role will determine them from the OS type.

sshd_packages

Use this variable to override the default list of packages to install.

sshd_binary

The path to the openssh executable

sshd_service

The name of the openssh service. By default, this variable contains the name of the ssh service that the target platform uses. But it can also be used to set the name of the custom ssh service when the sshd_install_service variable is used.

sshd_sftp_server

Default path to the sftp server binary.

Variables Exported by the Role

sshd_has_run

This variable is set to true after the role was successfully executed.

Configure SSH certificate authentication

To configure SSH certificate authentication on your SSH server, you need to provide at least the trusted user CA key, which will be used to validate client certificates against. This is done with the sshd_trusted_user_ca_keys_list variable.

If you need to map some of the authorized principals to system users, you can do that using the sshd_principals variable.

Additional variables

sshd_trusted_user_ca_keys_list

List of the trusted user CA public keys in OpenSSH (one-line) format (mandatory).

sshd_trustedusercakeys_directory_owner, shsd_trustedusercakeys_directory_group, sshd_trustedusercakeys_directory_mode

Use these variables to set the ownership and permissions for the Trusted User CA Keys directory. Defaults are respectively root, root and 0755.

sshd_trustedusercakeys_file_owner, shsd_trustedusercakeys_file_group, sshd_trustedusercakeys_file_mode

Use these variables to set the ownership and permissions for the Trusted User CA Keys file. Defaults are respectively root, root and 0640.

sshd_principals

A dict containing principals for users in the os (optional). e.g.

sshd_principals:
  admin:
    - frontend-admin
    - backend-admin
  somelinuxuser:
    - some-principal-defined-in-certificate

sshd_authorizedprincipals_directory_owner, shsd_authorizedprincipals_directory_group, sshd_authorizedprincipals_directory_mode

Use these variables to set the ownership and permissions for the Authorized Principals directory. Defaults are respectively root, root and 0755.

sshd_authorizedprincipals_file_owner, shsd_authorizedprincipals_file_group, sshd_authorizedprincipals_file_mode

Use these variables to set the ownership and permissions for the Authorized Principals file. Defaults are respectively root, root and 0644.

Additional configuration

The SSH server needs this information stored in files so in addition to the above variables, respective configuration options TrustedUserCAKeys (mandatory) and AuthorizedPrincipalsFile (optional) need to be present the sshd dictionary when invoking the role. For example:

sshd_config:
  TrustedUserCAKeys: /etc/ssh/path-to-trusted-user-ca-keys/trusted-user-ca-keys.pub
  AuthorizedPrincipalsFile: "/etc/ssh/path-to-auth-principals/auth_principals/%u"

To learn more about SSH Certificates, here is a nice tutorial to pure SSH certificates, from wikibooks.

To understand principals and to set up SSH certificates with Vault, this is a well-explained tutorial from Hashicorp.

Dependencies

None

For tests, the ansible.posix collection is required for the mount role to emulate FIPS mode.

Example Playbook

DANGER! This example is to show the range of configuration this role provides. Running it will likely break your SSH access to the server!

---
- hosts: all
  vars:
    sshd_skip_defaults: true
    sshd_config:
      Compression: true
      ListenAddress:
        - "0.0.0.0"
        - "::"
      GSSAPIAuthentication: false
      Match:
        - Condition: "Group user"
          GSSAPIAuthentication: true
    sshd_UsePrivilegeSeparation: false
    sshd_match:
        - Condition: "Group xusers"
          X11Forwarding: true
  roles:
    - role: willshersystems.sshd

Results in:

# Ansible managed: ...
Compression yes
GSSAPIAuthentication no
UsePrivilegeSeparation no
Match Group user
  GSSAPIAuthentication yes
Match Group xusers
  X11Forwarding yes

Since Ansible 2.4, the role can be invoked using include_role keyword, for example:

---
- hosts: all
  become: true
  tasks:
  - name: "Configure sshd"
    include_role:
      name: willshersystems.sshd
    vars:
      sshd_skip_defaults: true
      sshd_config:
        Compression: true
        ListenAddress:
          - "0.0.0.0"
          - "::"
        GSSAPIAuthentication: false
        Match:
          - Condition: "Group user"
            GSSAPIAuthentication: true
      sshd_UsePrivilegeSeparation: false
      sshd_match:
          - Condition: "Group xusers"
            X11Forwarding: true

You can just add a configuration snippet with the sshd_config_namespace option:

---
- hosts: all
  tasks:
  - name: Configure sshd to accept some useful environment variables
    include_role:
      name: willshersystems.sshd
    vars:
      sshd_config_namespace: accept-env
      sshd_config:
        # there are some handy environment variables to accept
        AcceptEnv:
          LANG
          LS_COLORS
          EDITOR

The following snippet will be added to the default configuration file (if not yet present):

# BEGIN sshd system role managed block: namespace accept-env
Match all
  AcceptEnv LANG LS_COLORS EDITOR
# END sshd system role managed block: namespace accept-env

More example playbooks can be found in examples/ directory.

Template Generation

The sshd_config.j2 and sshd_config_snippet.j2 templates are programmatically generated by the scripts in meta. New options should be added to the options_body and/or options_match. For a list-valued option, give the way it is rendered as the second column in options_body; options_match uses the same one for that option.

To regenerate the templates, from within the meta/ directory run: ./make_option_lists

rpm-ostree

See README-ostree.md

License

LGPLv3

Authors

Matt Willsher matt@willsher.systems

© 2014,2015 Willsher Systems Ltd.

Jakub Jelen jjelen@redhat.com

© 2020 - 2024 Red Hat, Inc.