openssl_certificate - Generate and/or check OpenSSL certificates

New in version 2.4.

Synopsis

  • This module allows one to (re)generate OpenSSL certificates. It implements a notion of provider (ie. selfsigned, acme, assertonly) for your certificate. The ‘assertonly’ provider is intended for use cases where one is only interested in checking properties of a supplied certificate. Many properties that can be specified in this module are for validation of an existing or newly generated certificate. The proper place to specify them, if you want to receive a certificate with these properties is a CSR (Certificate Signing Request). It uses the pyOpenSSL python library to interact with OpenSSL.

Requirements

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

  • python-pyOpenSSL >= 0.15 (if using selfsigned or assertonly provider)
  • acme-tiny (if using the acme provider)

Parameters

Parameter Choices/Defaults Comments
acme_accountkey_path
Path to the accountkey for the acme provider
acme_chain
(added in 2.5)
Default:
yes
Include the intermediate certificate to the generated certificate
acme_challenge_path
Path to the ACME challenge directory that is served on http://<HOST>:80/.well-known/acme-challenge/
attributes
(added in 2.3)
Default:
None
Attributes the 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.

aliases: attr
csr_path
Path to the Certificate Signing Request (CSR) used to generate this certificate. This is not required in assertonly mode.
extended_key_usage
The extended_key_usage extension field must contain all these values.

aliases: extendedKeyUsage
extended_key_usage_strict
    Choices:
  • no
  • yes
If set to True, the extended_key_usage extension field must contain only these values.

aliases: extendedKeyUsage_strict
force
    Choices:
  • no
  • yes
Generate the certificate, even if it already exists.
group Default:
None
Name of the group that should own the file/directory, as would be fed to chown.
has_expired
    Choices:
  • no
  • yes
Checks if the certificate is expired/not expired at the time the module is executed.
invalid_at
The certificate must be invalid at this point in time. The timestamp is formatted as an ASN.1 TIME.
issuer
Key/value pairs that must be present in the issuer name field of the certificate. If you need to specify more than one value with the same key, use a list as value.
issuer_strict
(added in 2.5)
    Choices:
  • no
  • yes
If set to True, the issuer field must contain only these values.
key_usage
The key_usage extension field must contain all these values.

aliases: keyUsage
key_usage_strict
    Choices:
  • no
  • yes
If set to True, the key_usage extension field must contain only these values.

aliases: keyUsage_strict
mode Default:
None
Mode the file or directory should be. For those used to /usr/bin/chmod remember that modes are actually octal numbers (like 0644 or 01777). Leaving off the leading zero will likely have unexpected results. As of version 1.8, the mode may be specified as a symbolic mode (for example, u+rwx or u=rw,g=r,o=r).
not_after
The certificate must expire at this point in time. The timestamp is formatted as an ASN.1 TIME.

aliases: notAfter
not_before
The certificate must start to become valid at this point in time. The timestamp is formatted as an ASN.1 TIME.

aliases: notBefore
owner Default:
None
Name of the user that should own the file/directory, as would be fed to chown.
path
required
Remote absolute path where the generated certificate file should be created or is already located.
privatekey_passphrase
The passphrase for the privatekey_path.
privatekey_path
Path to the private key to use when signing the certificate.
provider
required
    Choices:
  • selfsigned
  • assertonly
  • acme
Name of the provider to use to generate/retrieve the OpenSSL certificate. The assertonly provider will not generate files and fail if the certificate file is missing.
selevel Default:
s0
Level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the range. _default feature works as for seuser.
selfsigned_digest Default:
sha256
Digest algorithm to be used when self-signing the certificate
selfsigned_not_after
The timestamp at which the certificate stops being valid. The timestamp is formatted as an ASN.1 TIME. If this value is not specified, certificate will stop being valid 10 years from now.

aliases: selfsigned_notAfter
selfsigned_not_before
The timestamp at which the certificate starts being valid. The timestamp is formatted as an ASN.1 TIME. If this value is not specified, certificate will start being valid from now.

aliases: selfsigned_notBefore
selfsigned_version
(added in 2.5)
Default:
3
Version of the selfsigned certificate. Nowadays it should almost always be 3.
serole Default:
None
Role part of SELinux file context, _default feature works as for seuser.
setype Default:
None
Type part of SELinux file context, _default feature works as for seuser.
seuser Default:
None
User part of SELinux file context. Will default to system policy, if applicable. If set to _default, it will use the user portion of the policy if available.
signature_algorithms
list of algorithms that you would accept the certificate to be signed with (e.g. ['sha256WithRSAEncryption', 'sha512WithRSAEncryption']).
state
    Choices:
  • present
  • absent
Whether the certificate should exist or not, taking action if the state is different from what is stated.
subject
Key/value pairs that must be present in the subject name field of the certificate. If you need to specify more than one value with the same key, use a list as value.
subject_alt_name
The subject_alt_name extension field must contain these values.

aliases: subjectAltName
subject_alt_name_strict
    Choices:
  • no
  • yes
If set to True, the subject_alt_name extension field must contain only these values.

aliases: subjectAltName_strict
subject_strict
(added in 2.5)
    Choices:
  • no
  • yes
If set to True, the subject field must contain only these values.
unsafe_writes
(added in 2.2)
    Choices:
  • no
  • yes
Normally this module uses atomic operations to prevent data corruption or inconsistent reads from the target files, sometimes systems are configured or just broken in ways that prevent this. One example are docker mounted files, they cannot be updated atomically and can only be done in an unsafe manner.
This boolean option allows ansible to fall back to unsafe methods of updating files for those cases in which you do not have any other choice. Be aware that this is subject to race conditions and can lead to data corruption.
valid_at
The certificate must be valid at this point in time. The timestamp is formatted as an ASN.1 TIME.
valid_in
The certificate must still be valid in valid_in seconds from now.
version
Version of the certificate. Nowadays it should almost always be 3.

Notes

Note

  • All ASN.1 TIME values should be specified following the YYYYMMDDHHMMSSZ pattern. Date specified should be UTC. Minutes and seconds are mandatory.

Examples

- name: Generate a Self Signed OpenSSL certificate
  openssl_certificate:
    path: /etc/ssl/crt/ansible.com.crt
    privatekey_path: /etc/ssl/private/ansible.com.pem
    csr_path: /etc/ssl/csr/ansible.com.csr
    provider: selfsigned

- name: Generate a Let's Encrypt Certificate
  openssl_certificate:
    path: /etc/ssl/crt/ansible.com.crt
    csr_path: /etc/ssl/csr/ansible.com.csr
    provider: acme
    acme_accountkey_path: /etc/ssl/private/ansible.com.pem
    acme_challenge_path: /etc/ssl/challenges/ansible.com/

- name: Force (re-)generate a new Let's Encrypt Certificate
  openssl_certificate:
    path: /etc/ssl/crt/ansible.com.crt
    csr_path: /etc/ssl/csr/ansible.com.csr
    provider: acme
    acme_accountkey_path: /etc/ssl/private/ansible.com.pem
    acme_challenge_path: /etc/ssl/challenges/ansible.com/
    force: True

# Examples for some checks one could use the assertonly provider for:

# How to use the assertonly provider to implement and trigger your own custom certificate generation workflow:
- name: Check if a certificate is currently still valid, ignoring failures
  openssl_certificate:
    path: /etc/ssl/crt/example.com.crt
    provider: assertonly
    has_expired: False
  ignore_errors: True
  register: validity_check

- name: Run custom task(s) to get a new, valid certificate in case the initial check failed
  command: superspecialSSL recreate /etc/ssl/crt/example.com.crt
  when: validity_check.failed

- name: Check the new certificate again for validity with the same parameters, this time failing the play if it is still invalid
  openssl_certificate:
    path: /etc/ssl/crt/example.com.crt
    provider: assertonly
    has_expired: False
  when: validity_check.failed

# Some other checks that assertonly could be used for:
- name: Verify that an existing certificate was issued by the Let's Encrypt CA and is currently still valid
  openssl_certificate:
    path: /etc/ssl/crt/example.com.crt
    provider: assertonly
    issuer:
      O: Let's Encrypt
    has_expired: False

- name: Ensure that a certificate uses a modern signature algorithm (no SHA1, MD5 or DSA)
  openssl_certificate:
    path: /etc/ssl/crt/example.com.crt
    provider: assertonly
    signature_algorithms:
      - sha224WithRSAEncryption
      - sha256WithRSAEncryption
      - sha384WithRSAEncryption
      - sha512WithRSAEncryption
      - sha224WithECDSAEncryption
      - sha256WithECDSAEncryption
      - sha384WithECDSAEncryption
      - sha512WithECDSAEncryption

- name: Ensure that the existing certificate belongs to the specified private key
  openssl_certificate:
    path: /etc/ssl/crt/example.com.crt
    privatekey_path: /etc/ssl/private/example.com.pem
    provider: assertonly

- name: Ensure that the existing certificate is still valid at the winter solstice 2017
  openssl_certificate:
    path: /etc/ssl/crt/example.com.crt
    provider: assertonly
    valid_at: 20171221162800Z

- name: Ensure that the existing certificate is still valid 2 weeks (1209600 seconds) from now
  openssl_certificate:
    path: /etc/ssl/crt/example.com.crt
    provider: assertonly
    valid_in: 1209600

- name: Ensure that the existing certificate is only used for digital signatures and encrypting other keys
  openssl_certificate:
    path: /etc/ssl/crt/example.com.crt
    provider: assertonly
    key_usage:
      - digitalSignature
      - keyEncipherment
    key_usage_strict: true

- name: Ensure that the existing certificate can be used for client authentication
  openssl_certificate:
    path: /etc/ssl/crt/example.com.crt
    provider: assertonly
    extended_key_usage:
      - clientAuth

- name: Ensure that the existing certificate can only be used for client authentication and time stamping
  openssl_certificate:
    path: /etc/ssl/crt/example.com.crt
    provider: assertonly
    extended_key_usage:
      - clientAuth
      - 1.3.6.1.5.5.7.3.8
    extended_key_usage_strict: true

- name: Ensure that the existing certificate has a certain domain in its subjectAltName
  openssl_certificate:
    path: /etc/ssl/crt/example.com.crt
    provider: assertonly
    subject_alt_name:
      - www.example.com
      - test.example.com

Return Values

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

Key Returned Description
filename
string
changed or success
Path to the generated Certificate

Sample:
/etc/ssl/crt/www.ansible.com.crt


Status

This module is flagged as preview which means that it is not guaranteed to have a backwards compatible interface.

Author

  • Yanis Guenane (@Spredzy)
  • Markus Teufelberger (@MarkusTeufelberger)

Hint

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

© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.5/modules/openssl_certificate_module.html