openssl_csr – Generate OpenSSL Certificate Signing Request (CSR)

New in version 2.4.

Synopsis

  • This module allows one to (re)generate OpenSSL certificate signing requests.
  • It uses the pyOpenSSL python library to interact with openssl. This module supports the subjectAltName, keyUsage, extendedKeyUsage, basicConstraints and OCSP Must Staple extensions.
  • Please note that the module regenerates existing CSR if it doesn’t match the module’s options, or if it seems to be corrupt. If you are concerned that this could overwrite your existing CSR, consider using the backup option.
  • The module can use the cryptography Python library, or the pyOpenSSL Python library. By default, it tries to detect which one is available. This can be overridden with the select_crypto_backend option. Please note that the PyOpenSSL backend was deprecated in Ansible 2.9 and will be removed in Ansible 2.13.”

Requirements

The below requirements are needed on the host that executes this module.

  • Either cryptography >= 1.3
  • Or pyOpenSSL >= 0.15

Parameters

Parameter Choices/Defaults Comments
attributes
string
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 = operator is assumed as default, otherwise + or - operators need to be included in the string.

aliases: attr
authority_cert_issuer
list / elements=string
added in 2.9
Names that will be present in the authority cert issuer field of the certificate signing request.
Values must be prefixed by their options. (i.e., email, URI, DNS, RID, IP, dirName, otherName and the ones specific to your CA)
Example: DNS:ca.example.org
If specified, authority_key_identifier must also be specified.
Please note that commercial CAs ignore this value, respectively use a value of their own choice. Specifying this option is mostly useful for self-signed certificates or for own CAs.
Note that this is only supported if the cryptography backend is used!
The AuthorityKeyIdentifier will only be added if at least one of authority_key_identifier, authority_cert_issuer and authority_cert_serial_number is specified.
authority_cert_serial_number
integer
added in 2.9
The authority cert serial number.
Note that this is only supported if the cryptography backend is used!
Please note that commercial CAs ignore this value, respectively use a value of their own choice. Specifying this option is mostly useful for self-signed certificates or for own CAs.
The AuthorityKeyIdentifier will only be added if at least one of authority_key_identifier, authority_cert_issuer and authority_cert_serial_number is specified.
authority_key_identifier
string
added in 2.9
The authority key identifier as a hex string, where two bytes are separated by colons.
Example: 00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22:33
If specified, authority_cert_issuer must also be specified.
Please note that commercial CAs ignore this value, respectively use a value of their own choice. Specifying this option is mostly useful for self-signed certificates or for own CAs.
Note that this is only supported if the cryptography backend is used!
The AuthorityKeyIdentifier will only be added if at least one of authority_key_identifier, authority_cert_issuer and authority_cert_serial_number is specified.
backup
boolean
added in 2.8
    Choices:
  • no ←
  • yes
Create a backup file including a timestamp so you can get the original CSR back if you overwrote it with a new one by accident.
basic_constraints
list / elements=string
added in 2.5
Indicates basic constraints, such as if the certificate is a CA.

aliases: basicConstraints
basic_constraints_critical
boolean
added in 2.5
    Choices:
  • no
  • yes
Should the basicConstraints extension be considered as critical.

aliases: basicConstraints_critical
common_name
string
The commonName field of the certificate signing request subject.

aliases: CN, commonName
country_name
string
The countryName field of the certificate signing request subject.

aliases: C, countryName
create_subject_key_identifier
boolean
added in 2.9
    Choices:
  • no ←
  • yes
Create the Subject Key Identifier from the public key.
Please note that commercial CAs can ignore the value, respectively use a value of their own choice instead. Specifying this option is mostly useful for self-signed certificates or for own CAs.
Note that this is only supported if the cryptography backend is used!
digest
string
Default:
"sha256"
The digest used when signing the certificate signing request with the private key.
email_address
string
The emailAddress field of the certificate signing request subject.

aliases: E, emailAddress
extended_key_usage
list / elements=string
Additional restrictions (e.g. client authentication, server authentication) on the allowed purposes for which the public key may be used.

aliases: extKeyUsage, extendedKeyUsage
extended_key_usage_critical
boolean
    Choices:
  • no
  • yes
Should the extkeyUsage extension be considered as critical.

aliases: extKeyUsage_critical, extendedKeyUsage_critical
force
boolean
    Choices:
  • no ←
  • yes
Should the certificate signing request be forced regenerated by this ansible module.
group
string
Name of the group that should own the file/directory, as would be fed to chown.
key_usage
list / elements=string
This defines the purpose (e.g. encipherment, signature, certificate signing) of the key contained in the certificate.

aliases: keyUsage
key_usage_critical
boolean
    Choices:
  • no
  • yes
Should the keyUsage extension be considered as critical.

aliases: keyUsage_critical
locality_name
string
The localityName field of the certificate signing request subject.

aliases: L, localityName
mode
string
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 0644 or 01777) or quote it (like '644' or '1777') so Ansible receives a string and can do its own conversion from string into number.
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, u+rwx or u=rw,g=r,o=r).
As of Ansible 2.6, the mode may also be the special string preserve.
When set to preserve the file will be given the same permissions as the source file.
ocsp_must_staple
boolean
added in 2.5
    Choices:
  • no
  • yes
Indicates that the certificate should contain the OCSP Must Staple extension (https://tools.ietf.org/html/rfc7633).

aliases: ocspMustStaple
ocsp_must_staple_critical
boolean
added in 2.5
    Choices:
  • no
  • yes
Should the OCSP Must Staple extension be considered as critical
Note that according to the RFC, this extension should not be marked as critical, as old clients not knowing about OCSP Must Staple are required to reject such certificates (see https://tools.ietf.org/html/rfc7633#section-4).

aliases: ocspMustStaple_critical
organization_name
string
The organizationName field of the certificate signing request subject.

aliases: O, organizationName
organizational_unit_name
string
The organizationalUnitName field of the certificate signing request subject.

aliases: OU, organizationalUnitName
owner
string
Name of the user that should own the file/directory, as would be fed to chown.
path
path / required
The name of the file into which the generated OpenSSL certificate signing request will be written.
privatekey_passphrase
string
The passphrase for the private key.
This is required if the private key is password protected.
privatekey_path
path
The path to the private key to use when signing the certificate signing request.
Required if state is present.
select_crypto_backend
string
added in 2.8
    Choices:
  • auto ←
  • cryptography
  • pyopenssl
Determines which crypto backend to use.
The default choice is auto, which tries to use cryptography if available, and falls back to pyopenssl.
If set to pyopenssl, will try to use the pyOpenSSL library.
If set to cryptography, will try to use the cryptography library.
Please note that the pyopenssl backend has been deprecated in Ansible 2.9, and will be removed in Ansible 2.13. From that point on, only the cryptography backend will be available.
selevel
string
Default:
"s0"
The level part of the SELinux file context.
This is the MLS/MCS attribute, sometimes known as the range.
When set to _default, it will use the level portion of the policy if available.
serole
string
The role part of the SELinux file context.
When set to _default, it will use the role portion of the policy if available.
setype
string
The type part of the SELinux file context.
When set to _default, it will use the type portion of the policy if available.
seuser
string
The user part of the SELinux file context.
By default it uses the system policy, where applicable.
When set to _default, it will use the user portion of the policy if available.
state
string
    Choices:
  • absent
  • present ←
Whether the certificate signing request should exist or not, taking action if the state is different from what is stated.
state_or_province_name
string
The stateOrProvinceName field of the certificate signing request subject.

aliases: ST, stateOrProvinceName
subject
dictionary
added in 2.5
Key/value pairs that will be present in the subject name field of the certificate signing request.
If you need to specify more than one value with the same key, use a list as value.
subject_alt_name
list / elements=string
SAN extension to attach to the certificate signing request.
This can either be a 'comma separated string' or a YAML list.
Values must be prefixed by their options. (i.e., email, URI, DNS, RID, IP, dirName, otherName and the ones specific to your CA)
Note that if no SAN is specified, but a common name, the common name will be added as a SAN except if useCommonNameForSAN is set to false.

aliases: subjectAltName
subject_alt_name_critical
boolean
    Choices:
  • no
  • yes
Should the subjectAltName extension be considered as critical.

aliases: subjectAltName_critical
subject_key_identifier
string
added in 2.9
The subject key identifier as a hex string, where two bytes are separated by colons.
Example: 00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22:33
Please note that commercial CAs ignore this value, respectively use a value of their own choice. Specifying this option is mostly useful for self-signed certificates or for own CAs.
Note that this option can only be used if create_subject_key_identifier is no.
Note that this is only supported if the cryptography backend is used!
unsafe_writes
boolean
    Choices:
  • no ←
  • yes
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.
use_common_name_for_san
boolean
added in 2.8
    Choices:
  • no
  • yes ←
If set to yes, the module will fill the common name in for subject_alt_name with DNS: prefix if no SAN is specified.

aliases: useCommonNameForSAN
version
integer
Default:
1
The version of the certificate signing request.
The only allowed value according to RFC 2986 is 1.

Notes

Note

  • If the certificate signing request already exists it will be checked whether subjectAltName, keyUsage, extendedKeyUsage and basicConstraints only contain the requested values, whether OCSP Must Staple is as requested, and if the request was signed by the given private key.

See Also

See also

openssl_certificate – Generate and/or check OpenSSL certificates
The official documentation on the openssl_certificate module.
openssl_dhparam – Generate OpenSSL Diffie-Hellman Parameters
The official documentation on the openssl_dhparam module.
openssl_pkcs12 – Generate OpenSSL PKCS#12 archive
The official documentation on the openssl_pkcs12 module.
openssl_privatekey – Generate OpenSSL private keys
The official documentation on the openssl_privatekey module.
openssl_publickey – Generate an OpenSSL public key from its private key
The official documentation on the openssl_publickey module.

Examples

- name: Generate an OpenSSL Certificate Signing Request
  openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    common_name: www.ansible.com

- name: Generate an OpenSSL Certificate Signing Request with a passphrase protected private key
  openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    privatekey_passphrase: ansible
    common_name: www.ansible.com

- name: Generate an OpenSSL Certificate Signing Request with Subject information
  openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    country_name: FR
    organization_name: Ansible
    email_address: [email protected]
    common_name: www.ansible.com

- name: Generate an OpenSSL Certificate Signing Request with subjectAltName extension
  openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    subject_alt_name: 'DNS:www.ansible.com,DNS:m.ansible.com'

- name: Generate an OpenSSL CSR with subjectAltName extension with dynamic list
  openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    subject_alt_name: "{{ item.value | map('regex_replace', '^', 'DNS:') | list }}"
  with_dict:
    dns_server:
    - www.ansible.com
    - m.ansible.com

- name: Force regenerate an OpenSSL Certificate Signing Request
  openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    force: yes
    common_name: www.ansible.com

- name: Generate an OpenSSL Certificate Signing Request with special key usages
  openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    common_name: www.ansible.com
    key_usage:
      - digitalSignature
      - keyAgreement
    extended_key_usage:
      - clientAuth

- name: Generate an OpenSSL Certificate Signing Request with OCSP Must Staple
  openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    common_name: www.ansible.com
    ocsp_must_staple: yes

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key Returned Description
backup_file
string
changed and if backup is yes
Name of backup file created.

Sample:
/path/to/www.ansible.com.csr.2019-03-09@11:22~
basicConstraints
list / elements=string
changed or success
Indicates if the certificate belongs to a CA

Sample:
['CA:TRUE', 'pathLenConstraint:0']
extendedKeyUsage
list / elements=string
changed or success
Additional restriction on the public key purposes

Sample:
['clientAuth']
filename
string
changed or success
Path to the generated Certificate Signing Request

Sample:
/etc/ssl/csr/www.ansible.com.csr
keyUsage
list / elements=string
changed or success
Purpose for which the public key may be used

Sample:
['digitalSignature', 'keyAgreement']
ocsp_must_staple
boolean
changed or success
Indicates whether the certificate has the OCSP Must Staple feature enabled

privatekey
string
changed or success
Path to the TLS/SSL private key the CSR was generated for

Sample:
/etc/ssl/private/ansible.com.pem
subject
list / elements=list
changed or success
A list of the subject tuples attached to the CSR

Sample:
[('CN', 'www.ansible.com'), ('O', 'Ansible')]
subjectAltName
list / elements=string
changed or success
The alternative names this CSR is valid for

Sample:
['DNS:www.ansible.com', 'DNS:m.ansible.com']


Status

Authors

  • Yanis Guenane (@Spredzy)

Hint

If you notice any issues in this documentation, you can edit this document to improve it.