community.crypto.openssh_keypair – Generate OpenSSH private and public keys
Note
This plugin is part of the community.crypto collection (version 1.9.8).
You might already have this collection installed if you are using the ansible
package.
It is not included in ansible-core
.
To check whether it is installed, run ansible-galaxy collection list
.
To install it, use: ansible-galaxy collection install community.crypto
.
To use it in a playbook, specify: community.crypto.openssh_keypair
.
Synopsis
This module allows one to (re)generate OpenSSH private and public keys. It uses ssh-keygen to generate keys. One can generate
rsa
,dsa
,rsa1
,ed25519
orecdsa
private keys.
Requirements
The below requirements are needed on the host that executes this module.
ssh-keygen (if backend=openssh)
cryptography >= 2.6 (if backend=cryptography and OpenSSH < 7.8 is installed)
cryptography >= 3.0 (if backend=cryptography and OpenSSH >= 7.8 is installed)
Parameters
Parameter |
Comments |
---|---|
The attributes the resulting file or directory should have. To get supported flags look at the man page for chattr on the target system. This string should contain the attributes in the same order as the one displayed by lsattr. The |
|
Selects between the
Choices:
|
|
Provides a new comment to the public key. |
|
Should the key be regenerated even if it already exists Choices:
|
|
Name of the group that should own the file/directory, as would be fed to chown. |
|
The permissions the resulting file or directory should have. For those used to /usr/bin/chmod remember that modes are actually octal numbers. You must either add a leading zero so that Ansible’s YAML parser knows it is an octal number (like Giving Ansible a number without following one of these rules will end up with a decimal number which will have unexpected results. As of Ansible 1.8, the mode may be specified as a symbolic mode (for example, If If Specifying |
|
Name of the user that should own the file/directory, as would be fed to chown. |
|
Passphrase used to decrypt an existing private key or encrypt a newly generated private key. Passphrases are not supported for type=rsa1. Can only be used when backend=cryptography, or when backend=auto and a required |
|
Name of the files containing the public and private key. The file containing the public key will have the extension |
|
Used when a backend=cryptography to select a format for the private key at the provided path. The only valid option currently is For OpenSSH < 7.8 private keys will be in PKCS1 format except ed25519 keys which will be in OpenSSH format. For OpenSSH >= 7.8 all private key types will be in the OpenSSH format. Choices:
|
|
Allows to configure in which situations the module is allowed to regenerate private keys. The module will always generate a new key if the destination file does not exist. By default, the key will be regenerated when it does not match the module’s options, except when the key cannot be read or the passphrase does not match. Please note that this changed for Ansible 2.10. For Ansible 2.9, the behavior was as if If set to If set to If set to If set to If set to Note that adjusting the comment and the permissions can be changed without regeneration. Therefore, even for Choices:
|
|
The level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the When set to |
|
The role part of the SELinux file context. When set to |
|
The type part of the SELinux file context. When set to |
|
The user part of the SELinux file context. By default it uses the When set to |
|
Specifies the number of bits in the private key to create. For RSA keys, the minimum size is 1024 bits and the default is 4096 bits. Generally, 2048 bits is considered sufficient. DSA keys must be exactly 1024 bits as specified by FIPS 186-2. For ECDSA keys, size determines the key length by selecting from one of three elliptic curve sizes: 256, 384 or 521 bits. Attempting to use bit lengths other than these three values for ECDSA keys will cause this module to fail. Ed25519 keys have a fixed length and the size will be ignored. |
|
Whether the private and public keys should exist or not, taking action if the state is different from what is stated. Choices:
|
|
The algorithm used to generate the SSH private key. Choices:
|
|
Influence when to use atomic operation to prevent data corruption or inconsistent reads from the target file. By default this module uses atomic operations to prevent data corruption or inconsistent reads from the target files, but sometimes systems are configured or just broken in ways that prevent this. One example is docker mounted files, which cannot be updated atomically from inside the container and can only be written in an unsafe manner. This option allows Ansible to fall back to unsafe methods of updating files when atomic operations fail (however, it doesn’t force Ansible to perform unsafe writes). IMPORTANT! Unsafe writes are subject to race conditions and can lead to data corruption. Choices:
|
Notes
Note
In case the ssh key is broken or password protected, the module will fail. Set the force option to
yes
if you want to regenerate the keypair.Supports
check_mode
.In the case a custom
mode
,group
,owner
, or other file attribute is provided it will be applied to both key files.
Examples
- name: Generate an OpenSSH keypair with the default values (4096 bits, rsa)
community.crypto.openssh_keypair:
path: /tmp/id_ssh_rsa
- name: Generate an OpenSSH keypair with the default values (4096 bits, rsa) and encrypted private key
community.crypto.openssh_keypair:
path: /tmp/id_ssh_rsa
passphrase: super_secret_password
- name: Generate an OpenSSH rsa keypair with a different size (2048 bits)
community.crypto.openssh_keypair:
path: /tmp/id_ssh_rsa
size: 2048
- name: Force regenerate an OpenSSH keypair if it already exists
community.crypto.openssh_keypair:
path: /tmp/id_ssh_rsa
force: True
- name: Generate an OpenSSH keypair with a different algorithm (dsa)
community.crypto.openssh_keypair:
path: /tmp/id_ssh_dsa
type: dsa
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
---|---|
Path to the generated SSH private key file. Returned: changed or success Sample: “/tmp/id_ssh_rsa” |
|
The fingerprint of the key. Returned: changed or success Sample: “SHA256:r4YCZxihVjedH2OlfjVGI6Y5xAYtdCwk8VxKyzVyYfM” |
|
The public key of the generated SSH private key. Returned: changed or success Sample: “ssh-rsa AAAAB3Nza(…omitted…)veL4E3Xcw== test_key” |
|
Size (in bits) of the SSH private key. Returned: changed or success Sample: 4096 |
|
Algorithm used to generate the SSH private key. Returned: changed or success Sample: “rsa” |
Authors
David Kainz (@lolcube)