community.crypto.x509_certificate – Generate and/or check OpenSSL certificates
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.x509_certificate
.
Synopsis
If both the cryptography and PyOpenSSL libraries are available (and meet the minimum version requirements) cryptography will be preferred as a backend over PyOpenSSL (unless the backend is forced with
select_crypto_backend
). Please note that the PyOpenSSL backend was deprecated in Ansible 2.9 and will be removed in community.crypto 2.0.0.It implements a notion of provider (ie.
selfsigned
,ownca
,acme
,assertonly
,entrust
) for your certificate.It uses the pyOpenSSL or cryptography python library to interact with OpenSSL.
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).
Note that this module was called
openssl_certificate
when included directly in Ansible up to version 2.9. When moved to the collectioncommunity.crypto
, it was renamed to community.crypto.x509_certificate. From Ansible 2.10 on, it can still be used by the old short name (or byansible.builtin.openssl_certificate
), which redirects tocommunity.crypto.x509_certificate
. When using FQCNs or when using the collections keyword, the new name community.crypto.x509_certificate should be used to avoid a deprecation warning.Please note that the module regenerates existing certificate if it does not match the module’s options, or if it seems to be corrupt. If you are concerned that this could overwrite your existing certificate, consider using the backup option.
The
assertonly
provider is intended for use cases where one is only interested in checking properties of a supplied certificate. Please note that this provider has been deprecated in Ansible 2.9 and will be removed in community.crypto 2.0.0. See the examples on how to emulateassertonly
usage with community.crypto.x509_certificate_info, community.crypto.openssl_csr_info, community.crypto.openssl_privatekey_info and ansible.builtin.assert. This also allows more flexible checks than the ones offered by theassertonly
provider.The
ownca
provider is intended for generating an OpenSSL certificate signed with your own CA (Certificate Authority) certificate (self-signed certificate).This module allows one to (re)generate OpenSSL certificates.
Requirements
The below requirements are needed on the host that executes this module.
PyOpenSSL >= 0.15 or cryptography >= 1.6 (if using
selfsigned
,ownca
orassertonly
provider)acme-tiny >= 4.0.0 (if using the
acme
provider)
Parameters
Parameter |
Comments |
---|---|
The path to the accountkey for the This is only used by the |
|
Include the intermediate certificate to the generated certificate This is only used by the Note that this is only available for older versions of Choices:
|
|
The path to the ACME challenge directory that is served on http://<HOST>:80/.well-known/acme-challenge/ This is only used by the |
|
The ACME directory to use. You can use any directory that supports the ACME protocol, such as Buypass or Let’s Encrypt. Let’s Encrypt recommends using their staging server while developing jobs. https://letsencrypt.org/docs/staging-environment/. Default: “https://acme-v02.api.letsencrypt.org/directory” |
|
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 |
|
Create a backup file including a timestamp so you can get the original certificate back if you overwrote it with a new one by accident. This is not used by the This option is deprecated since Ansible 2.9 and will be removed with the Choices:
|
|
Content of the Certificate Signing Request (CSR) used to generate this certificate. This is mutually exclusive with csr_path. |
|
Path to the Certificate Signing Request (CSR) used to generate this certificate. This is mutually exclusive with csr_content. |
|
The path to the private key of the client certificate used to authenticate to the Entrust Certificate Services (ECS) API. This is only used by the This is required if the provider is |
|
The path to the client certificate used to authenticate to the Entrust Certificate Services (ECS) API. This is only used by the This is required if the provider is |
|
The key (password) for authentication to the Entrust Certificate Services (ECS) API. This is only used by the This is required if the provider is |
|
The path to the specification file defining the Entrust Certificate Services (ECS) API configuration. You can use this to keep a local copy of the specification to avoid downloading it every time the module is used. This is only used by the Default: “https://cloud.entrust.net/EntrustCloud/documentation/cms-api-2.1.0.yaml” |
|
The username for authentication to the Entrust Certificate Services (ECS) API. This is only used by the This is required if the provider is |
|
Specify the type of certificate requested. This is only used by the Choices:
|
|
The point in time at which the certificate stops being valid. Time can be specified either as relative time or as an absolute timestamp. A valid absolute time format is A valid relative time format is Time will always be interpreted as UTC. Note that only the date (day, month, year) is supported for specifying the expiry date of the issued certificate. The full date-time is adjusted to EST (GMT -5:00) before issuance, which may result in a certificate with an expiration date one day earlier than expected if a relative time is used. The minimum certificate lifetime is 90 days, and maximum is three years. If this value is not specified, the certificate will stop being valid 365 days the date of issue. This is only used by the Default: “+365d” |
|
The email of the requester of the certificate (for tracking purposes). This is only used by the This is required if the provider is |
|
The name of the requester of the certificate (for tracking purposes). This is only used by the This is required if the provider is |
|
The phone number of the requester of the certificate (for tracking purposes). This is only used by the This is required if the provider is |
|
The extended_key_usage extension field must contain all these values. This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the |
|
If set to This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the Choices:
|
|
Generate the certificate, even if it already exists. Choices:
|
|
Name of the group that should own the file/directory, as would be fed to chown. |
|
Checks if the certificate is expired/not expired at the time the module is executed. This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the Choices:
|
|
The certificate must be invalid at this point in time. The timestamp is formatted as an ASN.1 TIME. This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the |
|
The 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. This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the |
|
If set to This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the Choices:
|
|
The key_usage extension field must contain all these values. This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the |
|
If set to This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the Choices:
|
|
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 |
|
The certificate must expire at this point in time. The timestamp is formatted as an ASN.1 TIME. This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the |
|
The certificate must start to become valid at this point in time. The timestamp is formatted as an ASN.1 TIME. This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the |
|
Content of the CA (Certificate Authority) certificate. This is only used by the This is mutually exclusive with ownca_path. |
|
Create a Authority Key Identifier from the CA’s certificate. If the CSR provided a authority key identifier, it is ignored. The Authority Key Identifier is generated from the CA certificate’s Subject Key Identifier, if available. If it is not available, the CA certificate’s public key will be used. This is only used by the Note that this is only supported if the Choices:
|
|
Whether to create the Subject Key Identifier (SKI) from the public key. A value of A value of A value of This is only used by the Note that this is only supported if the Choices:
|
|
The digest algorithm to be used for the This is only used by the Default: “sha256” |
|
The point in time at which the certificate stops being valid. Time can be specified either as relative time or as absolute timestamp. Time will always be interpreted as UTC. Valid format is If this value is not specified, the certificate will stop being valid 10 years from now. Note that this value is not used to determine whether an existing certificate should be regenerated. This is only used by the On macOS 10.15 and onwards, TLS server certificates must have a validity period of 825 days or fewer. Please see https://support.apple.com/en-us/HT210176 for more details. Default: “+3650d” |
|
The point in time the certificate is valid from. Time can be specified either as relative time or as absolute timestamp. Time will always be interpreted as UTC. Valid format is If this value is not specified, the certificate will start being valid from now. Note that this value is not used to determine whether an existing certificate should be regenerated. This is only used by the Default: “+0s” |
|
Remote absolute path of the CA (Certificate Authority) certificate. This is only used by the This is mutually exclusive with ownca_content. |
|
Content of the CA (Certificate Authority) private key to use when signing the certificate. This is only used by the This is mutually exclusive with ownca_privatekey_path. |
|
The passphrase for the ownca_privatekey_path resp. ownca_privatekey_content. This is only used by the |
|
Path to the CA (Certificate Authority) private key to use when signing the certificate. This is only used by the This is mutually exclusive with ownca_privatekey_content. |
|
The version of the Nowadays it should almost always be This is only used by the Default: 3 |
|
Name of the user that should own the file/directory, as would be fed to chown. |
|
Remote absolute path where the generated certificate file should be created or is already located. |
|
Path to the private key to use when signing the certificate. This is mutually exclusive with privatekey_path. |
|
The passphrase for the privatekey_path resp. privatekey_content. This is required if the private key is password protected. |
|
Path to the private key to use when signing the certificate. This is mutually exclusive with privatekey_content. |
|
Name of the provider to use to generate/retrieve the OpenSSL certificate. The The The Required if state is Choices:
|
|
If set to Choices:
|
|
Determines which crypto backend to use. The default choice is If set to If set to Please note that the Choices:
|
|
The level part of the SELinux file context. This is the MLS/MCS attribute, sometimes known as the When set to |
|
Whether to create the Subject Key Identifier (SKI) from the public key. A value of A value of A value of This is only used by the Note that this is only supported if the Choices:
|
|
Digest algorithm to be used when self-signing the certificate. This is only used by the Default: “sha256” |
|
The point in time at which the certificate stops being valid. Time can be specified either as relative time or as absolute timestamp. Time will always be interpreted as UTC. Valid format is If this value is not specified, the certificate will stop being valid 10 years from now. Note that this value is not used to determine whether an existing certificate should be regenerated. This is only used by the On macOS 10.15 and onwards, TLS server certificates must have a validity period of 825 days or fewer. Please see https://support.apple.com/en-us/HT210176 for more details. Default: “+3650d” |
|
The point in time the certificate is valid from. Time can be specified either as relative time or as absolute timestamp. Time will always be interpreted as UTC. Valid format is If this value is not specified, the certificate will start being valid from now. Note that this value is not used to determine whether an existing certificate should be regenerated. This is only used by the Default: “+0s” |
|
Version of the Nowadays it should almost always be This is only used by the Default: 3 |
|
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 |
|
A list of algorithms that you would accept the certificate to be signed with (e.g. [‘sha256WithRSAEncryption’, ‘sha512WithRSAEncryption’]). This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the |
|
Whether the certificate should exist or not, taking action if the state is different from what is stated. Choices:
|
|
The 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. This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the |
|
The subject_alt_name extension field must contain these values. This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the |
|
If set to This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the Choices:
|
|
If set to This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the 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:
|
|
The certificate must be valid at this point in time. The timestamp is formatted as an ASN.1 TIME. This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the |
|
The certificate must still be valid at this relative time offset from now. Valid format is Note that if using this parameter, this module is NOT idempotent. This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the |
|
The version of the certificate. Nowadays it should almost always be 3. This is only used by the This option is deprecated since Ansible 2.9 and will be removed with the |
Notes
Note
Supports
check_mode
.All ASN.1 TIME values should be specified following the YYYYMMDDHHMMSSZ pattern.
Date specified should be UTC. Minutes and seconds are mandatory.
For security reason, when you use
ownca
provider, you should NOT run community.crypto.x509_certificate on a target machine, but on a dedicated CA machine. It is recommended not to store the CA private key on the target machine. Once signed, the certificate can be moved to the target machine.For the
selfsigned
provider, csr_path and csr_content are optional. If not provided, a certificate without any information (Subject, Subject Alternative Names, Key Usage, etc.) is created.
See Also
See also
- community.crypto.x509_certificate_pipe
The official documentation on the community.crypto.x509_certificate_pipe module.
- community.crypto.openssl_csr
The official documentation on the community.crypto.openssl_csr module.
- community.crypto.openssl_csr_pipe
The official documentation on the community.crypto.openssl_csr_pipe module.
- community.crypto.openssl_dhparam
The official documentation on the community.crypto.openssl_dhparam module.
- community.crypto.openssl_pkcs12
The official documentation on the community.crypto.openssl_pkcs12 module.
- community.crypto.openssl_privatekey
The official documentation on the community.crypto.openssl_privatekey module.
- community.crypto.openssl_privatekey_pipe
The official documentation on the community.crypto.openssl_privatekey_pipe module.
- community.crypto.openssl_publickey
The official documentation on the community.crypto.openssl_publickey module.
Examples
- name: Generate a Self Signed OpenSSL certificate
community.crypto.x509_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 an OpenSSL certificate signed with your own CA certificate
community.crypto.x509_certificate:
path: /etc/ssl/crt/ansible.com.crt
csr_path: /etc/ssl/csr/ansible.com.csr
ownca_path: /etc/ssl/crt/ansible_CA.crt
ownca_privatekey_path: /etc/ssl/private/ansible_CA.pem
provider: ownca
- name: Generate a Let's Encrypt Certificate
community.crypto.x509_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
community.crypto.x509_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: yes
- name: Generate an Entrust certificate via the Entrust Certificate Services (ECS) API
community.crypto.x509_certificate:
path: /etc/ssl/crt/ansible.com.crt
csr_path: /etc/ssl/csr/ansible.com.csr
provider: entrust
entrust_requester_name: Jo Doe
entrust_requester_email: [email protected]
entrust_requester_phone: 555-555-5555
entrust_cert_type: STANDARD_SSL
entrust_api_user: apiusername
entrust_api_key: a^lv*32!cd9LnT
entrust_api_client_cert_path: /etc/ssl/entrust/ecs-client.crt
entrust_api_client_cert_key_path: /etc/ssl/entrust/ecs-key.crt
entrust_api_specification_path: /etc/ssl/entrust/api-docs/cms-api-2.1.0.yaml
# The following example shows one assertonly usage using all existing options for
# assertonly, and shows how to emulate the behavior with the x509_certificate_info,
# openssl_csr_info, openssl_privatekey_info and assert modules:
- name: Usage of assertonly with all existing options
community.crypto.x509_certificate:
provider: assertonly
path: /etc/ssl/crt/ansible.com.crt
csr_path: /etc/ssl/csr/ansible.com.csr
privatekey_path: /etc/ssl/csr/ansible.com.key
signature_algorithms:
- sha256WithRSAEncryption
- sha512WithRSAEncryption
subject:
commonName: ansible.com
subject_strict: yes
issuer:
commonName: ansible.com
issuer_strict: yes
has_expired: no
version: 3
key_usage:
- Data Encipherment
key_usage_strict: yes
extended_key_usage:
- DVCS
extended_key_usage_strict: yes
subject_alt_name:
- dns:ansible.com
subject_alt_name_strict: yes
not_before: 20190331202428Z
not_after: 20190413202428Z
valid_at: "+1d10h"
invalid_at: 20200331202428Z
valid_in: 10 # in ten seconds
- name: Get certificate information
community.crypto.x509_certificate_info:
path: /etc/ssl/crt/ansible.com.crt
# for valid_at, invalid_at and valid_in
valid_at:
one_day_ten_hours: "+1d10h"
fixed_timestamp: 20200331202428Z
ten_seconds: "+10"
register: result
- name: Get CSR information
community.crypto.openssl_csr_info:
# Verifies that the CSR signature is valid; module will fail if not
path: /etc/ssl/csr/ansible.com.csr
register: result_csr
- name: Get private key information
community.crypto.openssl_privatekey_info:
path: /etc/ssl/csr/ansible.com.key
register: result_privatekey
- assert:
that:
# When private key is specified for assertonly, this will be checked:
- result.public_key == result_privatekey.public_key
# When CSR is specified for assertonly, this will be checked:
- result.public_key == result_csr.public_key
- result.subject_ordered == result_csr.subject_ordered
- result.extensions_by_oid == result_csr.extensions_by_oid
# signature_algorithms check
- "result.signature_algorithm == 'sha256WithRSAEncryption' or result.signature_algorithm == 'sha512WithRSAEncryption'"
# subject and subject_strict
- "result.subject.commonName == 'ansible.com'"
- "result.subject | length == 1" # the number must be the number of entries you check for
# issuer and issuer_strict
- "result.issuer.commonName == 'ansible.com'"
- "result.issuer | length == 1" # the number must be the number of entries you check for
# has_expired
- not result.expired
# version
- result.version == 3
# key_usage and key_usage_strict
- "'Data Encipherment' in result.key_usage"
- "result.key_usage | length == 1" # the number must be the number of entries you check for
# extended_key_usage and extended_key_usage_strict
- "'DVCS' in result.extended_key_usage"
- "result.extended_key_usage | length == 1" # the number must be the number of entries you check for
# subject_alt_name and subject_alt_name_strict
- "'dns:ansible.com' in result.subject_alt_name"
- "result.subject_alt_name | length == 1" # the number must be the number of entries you check for
# not_before and not_after
- "result.not_before == '20190331202428Z'"
- "result.not_after == '20190413202428Z'"
# valid_at, invalid_at and valid_in
- "result.valid_at.one_day_ten_hours" # for valid_at
- "not result.valid_at.fixed_timestamp" # for invalid_at
- "result.valid_at.ten_seconds" # for valid_in
# Examples for some checks one could use the assertonly provider for:
# (Please note that assertonly has been deprecated!)
# 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
community.crypto.x509_certificate:
path: /etc/ssl/crt/example.com.crt
provider: assertonly
has_expired: no
ignore_errors: yes
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
community.crypto.x509_certificate:
path: /etc/ssl/crt/example.com.crt
provider: assertonly
has_expired: no
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
community.crypto.x509_certificate:
path: /etc/ssl/crt/example.com.crt
provider: assertonly
issuer:
O: Let's Encrypt
has_expired: no
- name: Ensure that a certificate uses a modern signature algorithm (no SHA1, MD5 or DSA)
community.crypto.x509_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
community.crypto.x509_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
community.crypto.x509_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
community.crypto.x509_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
community.crypto.x509_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
community.crypto.x509_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
community.crypto.x509_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
community.crypto.x509_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 |
Description |
---|---|
Name of backup file created. Returned: changed and if backup is Sample: “/path/to/www.ansible.com.crt.2019-03-09@11:22~” |
|
The (current or generated) certificate’s content. Returned: if state is |
|
Path to the generated certificate. Returned: changed or success Sample: “/etc/ssl/crt/www.ansible.com.crt” |
Authors
Yanis Guenane (@Spredzy)
Markus Teufelberger (@MarkusTeufelberger)