community.crypto.x509_certificate_pipe – Generate and/or check OpenSSL certificates

Note

This plugin is part of the community.crypto collection (version 1.9.3).

To install it use: ansible-galaxy collection install community.crypto.

To use it in a playbook, specify: community.crypto.x509_certificate_pipe.

New in version 1.3.0: of community.crypto

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, entrust) for your certificate.

  • It uses the pyOpenSSL or cryptography python library to interact with OpenSSL.

  • Please note that the module regenerates an 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 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 or assertonly provider)

Parameters

Parameter Choices/Defaults Comments
content
string
The existing certificate.
csr_content
string
Content of the Certificate Signing Request (CSR) used to generate this certificate.
This is mutually exclusive with csr_path.
csr_path
path
Path to the Certificate Signing Request (CSR) used to generate this certificate.
This is mutually exclusive with csr_content.
entrust_api_client_cert_key_path
path
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 entrust provider.
This is required if the provider is entrust.
entrust_api_client_cert_path
path
The path to the client certificate used to authenticate to the Entrust Certificate Services (ECS) API.
This is only used by the entrust provider.
This is required if the provider is entrust.
entrust_api_key
string
The key (password) for authentication to the Entrust Certificate Services (ECS) API.
This is only used by the entrust provider.
This is required if the provider is entrust.
entrust_api_specification_path
path
Default:
"https://cloud.entrust.net/EntrustCloud/documentation/cms-api-2.1.0.yaml"
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 entrust provider.
entrust_api_user
string
The username for authentication to the Entrust Certificate Services (ECS) API.
This is only used by the entrust provider.
This is required if the provider is entrust.
entrust_cert_type
string
    Choices:
  • STANDARD_SSL ←
  • ADVANTAGE_SSL
  • UC_SSL
  • EV_SSL
  • WILDCARD_SSL
  • PRIVATE_SSL
  • PD_SSL
  • CDS_ENT_LITE
  • CDS_ENT_PRO
  • SMIME_ENT
Specify the type of certificate requested.
This is only used by the entrust provider.
entrust_not_after
string
Default:
"+365d"
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 ASN.1 TIME such as 2019-06-18.
A valid relative time format is [+-]timespec where timespec can be an integer + [w | d | h | m | s], such as +365d or +32w1d2h).
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 entrust provider.
entrust_requester_email
string
The email of the requester of the certificate (for tracking purposes).
This is only used by the entrust provider.
This is required if the provider is entrust.
entrust_requester_name
string
The name of the requester of the certificate (for tracking purposes).
This is only used by the entrust provider.
This is required if the provider is entrust.
entrust_requester_phone
string
The phone number of the requester of the certificate (for tracking purposes).
This is only used by the entrust provider.
This is required if the provider is entrust.
force
boolean
    Choices:
  • no ←
  • yes
Generate the certificate, even if it already exists.
ownca_content
string
Content of the CA (Certificate Authority) certificate.
This is only used by the ownca provider.
This is mutually exclusive with ownca_path.
ownca_create_authority_key_identifier
boolean
    Choices:
  • no
  • yes ←
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 ownca provider.
Note that this is only supported if the cryptography backend is used!
ownca_create_subject_key_identifier
string
    Choices:
  • create_if_not_provided ←
  • always_create
  • never_create
Whether to create the Subject Key Identifier (SKI) from the public key.
A value of create_if_not_provided (default) only creates a SKI when the CSR does not provide one.
A value of always_create always creates a SKI. If the CSR provides one, that one is ignored.
A value of never_create never creates a SKI. If the CSR provides one, that one is used.
This is only used by the ownca provider.
Note that this is only supported if the cryptography backend is used!
ownca_digest
string
Default:
"sha256"
The digest algorithm to be used for the ownca certificate.
This is only used by the ownca provider.
ownca_not_after
string
Default:
"+3650d"
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 [+-]timespec | ASN.1 TIME where timespec can be an integer + [w | d | h | m | s] (e.g. +32w1d2h.
Note that if using relative time this module is NOT idempotent.
If this value is not specified, the certificate will stop being valid 10 years from now.
This is only used by the ownca provider.
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.
ownca_not_before
string
Default:
"+0s"
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 [+-]timespec | ASN.1 TIME where timespec can be an integer + [w | d | h | m | s] (e.g. +32w1d2h.
Note that if using relative time this module is NOT idempotent.
If this value is not specified, the certificate will start being valid from now.
This is only used by the ownca provider.
ownca_path
path
Remote absolute path of the CA (Certificate Authority) certificate.
This is only used by the ownca provider.
This is mutually exclusive with ownca_content.
ownca_privatekey_content
string
Content of the CA (Certificate Authority) private key to use when signing the certificate.
This is only used by the ownca provider.
This is mutually exclusive with ownca_privatekey_path.
ownca_privatekey_passphrase
string
The passphrase for the ownca_privatekey_path resp. ownca_privatekey_content.
This is only used by the ownca provider.
ownca_privatekey_path
path
Path to the CA (Certificate Authority) private key to use when signing the certificate.
This is only used by the ownca provider.
This is mutually exclusive with ownca_privatekey_content.
ownca_version
integer
Default:
3
The version of the ownca certificate.
Nowadays it should almost always be 3.
This is only used by the ownca provider.
privatekey_content
string
Path to the private key to use when signing the certificate.
This is mutually exclusive with privatekey_path.
privatekey_passphrase
string
The passphrase for the privatekey_path resp. privatekey_content.
This is required if the private key is password protected.
privatekey_path
path
Path to the private key to use when signing the certificate.
This is mutually exclusive with privatekey_content.
provider
string / required
    Choices:
  • entrust
  • ownca
  • selfsigned
Name of the provider to use to generate/retrieve the OpenSSL certificate.
The entrust provider requires credentials for the Entrust Certificate Services (ECS) API.
select_crypto_backend
string
    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 community.crypto 2.0.0. From that point on, only the cryptography backend will be available.
selfsigned_create_subject_key_identifier
string
    Choices:
  • create_if_not_provided ←
  • always_create
  • never_create
Whether to create the Subject Key Identifier (SKI) from the public key.
A value of create_if_not_provided (default) only creates a SKI when the CSR does not provide one.
A value of always_create always creates a SKI. If the CSR provides one, that one is ignored.
A value of never_create never creates a SKI. If the CSR provides one, that one is used.
This is only used by the selfsigned provider.
Note that this is only supported if the cryptography backend is used!
selfsigned_digest
string
Default:
"sha256"
Digest algorithm to be used when self-signing the certificate.
This is only used by the selfsigned provider.
selfsigned_not_after
string
Default:
"+3650d"
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 [+-]timespec | ASN.1 TIME where timespec can be an integer + [w | d | h | m | s] (e.g. +32w1d2h.
Note that if using relative time this module is NOT idempotent.
If this value is not specified, the certificate will stop being valid 10 years from now.
This is only used by the selfsigned provider.
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.

aliases: selfsigned_notAfter
selfsigned_not_before
string
Default:
"+0s"
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 [+-]timespec | ASN.1 TIME where timespec can be an integer + [w | d | h | m | s] (e.g. +32w1d2h.
Note that if using relative time this module is NOT idempotent.
If this value is not specified, the certificate will start being valid from now.
This is only used by the selfsigned provider.

aliases: selfsigned_notBefore
selfsigned_version
integer
Default:
3
Version of the selfsigned certificate.
Nowadays it should almost always be 3.
This is only used by the selfsigned provider.

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

The official documentation on the community.crypto.x509_certificate 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_pipe:
    provider: selfsigned
    privatekey_path: /etc/ssl/private/ansible.com.pem
    csr_path: /etc/ssl/csr/ansible.com.csr
  register: result
- name: Print the certificate
  ansible.builtin.debug:
    var: result.certificate

# In the following example, both CSR and certificate file are stored on the
# machine where ansible-playbook is executed, while the OwnCA data (certificate,
# private key) are stored on the remote machine.

- name: (1/2) Generate an OpenSSL Certificate with the CSR provided inline
  community.crypto.x509_certificate_pipe:
    provider: ownca
    content: "{{ lookup('file', '/etc/ssl/csr/www.ansible.com.crt') }}"
    csr_content: "{{ lookup('file', '/etc/ssl/csr/www.ansible.com.csr') }}"
    ownca_cert: /path/to/ca_cert.crt
    ownca_privatekey: /path/to/ca_cert.key
    ownca_privatekey_passphrase: hunter2
  register: result

- name: (2/2) Store certificate
  ansible.builtin.copy:
    dest: /etc/ssl/csr/www.ansible.com.crt
    content: "{{ result.certificate }}"
  delegate_to: localhost
  when: result is changed

# In the following example, the certificate from another machine is signed by
# our OwnCA whose private key and certificate are only available on this
# machine (where ansible-playbook is executed), without having to write
# the certificate file to disk on localhost. The CSR could have been
# provided by community.crypto.openssl_csr_pipe earlier, or also have been
# read from the remote machine.

- name: (1/3) Read certificate's contents from remote machine
  ansible.builtin.slurp:
    src: /etc/ssl/csr/www.ansible.com.crt
  register: certificate_content

- name: (2/3) Generate an OpenSSL Certificate with the CSR provided inline
  community.crypto.x509_certificate_pipe:
    provider: ownca
    content: "{{ certificate_content.content | b64decode }}"
    csr_content: "{{ the_csr }}"
    ownca_cert: /path/to/ca_cert.crt
    ownca_privatekey: /path/to/ca_cert.key
    ownca_privatekey_passphrase: hunter2
  delegate_to: localhost
  register: result

- name: (3/3) Store certificate
  ansible.builtin.copy:
    dest: /etc/ssl/csr/www.ansible.com.crt
    content: "{{ result.certificate }}"
  when: result is changed

Return Values

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

Key Returned Description
certificate
string
changed or success
The (current or generated) certificate's content.



Authors

  • Yanis Guenane (@Spredzy)

  • Markus Teufelberger (@MarkusTeufelberger)

  • Felix Fontein (@felixfontein)