ansible.builtin.openssl_csr (v2.9.27) — module

Generate OpenSSL Certificate Signing Request (CSR)

| "added in version" 2.4 of ansible.builtin"

Authors: Yanis Guenane (@Spredzy)

preview | supported by community

Install Ansible via pip

Install with pip install ansible==2.9.27

Description

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 I(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 I(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

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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: jdoe@ansible.com
    common_name: www.ansible.com
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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

Inputs

    
mode:
    description:
    - The permissions the resulting filesystem object should have.
    - For those used to I(/usr/bin/chmod) remember that modes are actually octal numbers.
      You must give Ansible enough information to parse them correctly. For consistent
      results, quote octal numbers (for example, V('644') or V('1777')) so Ansible receives
      a string and can do its own conversion from string into number. Adding a leading
      zero (for example, V(0755)) works sometimes, but can fail in loops and some other
      circumstances.
    - Giving Ansible a number without following either 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, V(u+rwx)
      or V(u=rw,g=r,o=r)).
    - If O(mode) is not specified and the destination filesystem object B(does not) exist,
      the default C(umask) on the system will be used when setting the mode for the newly
      created filesystem object.
    - If O(mode) is not specified and the destination filesystem object B(does) exist,
      the mode of the existing filesystem object will be used.
    - Specifying O(mode) is the best way to ensure filesystem objects are created with
      the correct permissions. See CVE-2020-1736 for further details.
    type: raw

path:
    description:
    - The name of the file into which the generated OpenSSL certificate signing request
      will be written.
    required: true
    type: path

force:
    default: false
    description:
    - Should the certificate signing request be forced regenerated by this ansible module.
    type: bool

group:
    description:
    - Name of the group that should own the filesystem object, as would be fed to I(chown).
    - When left unspecified, it uses the current group of the current user unless you
      are root, in which case it can preserve the previous ownership.
    type: str

owner:
    description:
    - Name of the user that should own the filesystem object, as would be fed to I(chown).
    - When left unspecified, it uses the current user unless you are root, in which case
      it can preserve the previous ownership.
    - Specifying a numeric username will be assumed to be a user ID and not a username.
      Avoid numeric usernames to avoid this confusion.
    type: str

state:
    choices:
    - absent
    - present
    default: present
    description:
    - Whether the certificate signing request should exist or not, taking action if the
      state is different from what is stated.
    type: str

backup:
    default: false
    description:
    - 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.
    type: bool
    version_added: '2.8'
    version_added_collection: ansible.builtin

digest:
    default: sha256
    description:
    - The digest used when signing the certificate signing request with the private key.
    type: str

serole:
    description:
    - The role part of the SELinux filesystem object context.
    - When set to V(_default), it will use the C(role) portion of the policy if available.
    type: str

setype:
    description:
    - The type part of the SELinux filesystem object context.
    - When set to V(_default), it will use the C(type) portion of the policy if available.
    type: str

seuser:
    description:
    - The user part of the SELinux filesystem object context.
    - By default it uses the V(system) policy, where applicable.
    - When set to V(_default), it will use the C(user) portion of the policy if available.
    type: str

selevel:
    description:
    - The level part of the SELinux filesystem object context.
    - This is the MLS/MCS attribute, sometimes known as the C(range).
    - When set to V(_default), it will use the C(level) portion of the policy if available.
    type: str

subject:
    description:
    - 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.
    type: dict
    version_added: '2.5'
    version_added_collection: ansible.builtin

version:
    default: 1
    description:
    - The version of the certificate signing request.
    - The only allowed value according to L(RFC 2986,https://tools.ietf.org/html/rfc2986#section-4.1)
      is 1.
    type: int

key_usage:
    aliases:
    - keyUsage
    description:
    - This defines the purpose (e.g. encipherment, signature, certificate signing) of
      the key contained in the certificate.
    elements: str
    type: list

attributes:
    aliases:
    - attr
    description:
    - The attributes the resulting filesystem object should have.
    - To get supported flags look at the man page for I(chattr) on the target system.
    - This string should contain the attributes in the same order as the one displayed
      by I(lsattr).
    - The C(=) operator is assumed as default, otherwise C(+) or C(-) operators need to
      be included in the string.
    type: str
    version_added: '2.3'
    version_added_collection: ansible.builtin

common_name:
    aliases:
    - CN
    - commonName
    description:
    - The commonName field of the certificate signing request subject.
    type: str

country_name:
    aliases:
    - C
    - countryName
    description:
    - The countryName field of the certificate signing request subject.
    type: str

email_address:
    aliases:
    - E
    - emailAddress
    description:
    - The emailAddress field of the certificate signing request subject.
    type: str

locality_name:
    aliases:
    - L
    - localityName
    description:
    - The localityName field of the certificate signing request subject.
    type: str

unsafe_writes:
    default: false
    description:
    - Influence when to use atomic operation to prevent data corruption or inconsistent
      reads from the target filesystem object.
    - By default this module uses atomic operations to prevent data corruption or inconsistent
      reads from the target filesystem objects, but sometimes systems are configured or
      just broken in ways that prevent this. One example is docker mounted filesystem
      objects, 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 filesystem
      objects 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.
    type: bool
    version_added: '2.2'
    version_added_collection: ansible.builtin

privatekey_path:
    description:
    - The path to the private key to use when signing the certificate signing request.
    - Required if I(state) is C(present).
    type: path

ocsp_must_staple:
    aliases:
    - ocspMustStaple
    description:
    - Indicates that the certificate should contain the OCSP Must Staple extension (U(https://tools.ietf.org/html/rfc7633)).
    type: bool
    version_added: '2.5'
    version_added_collection: ansible.builtin

subject_alt_name:
    aliases:
    - subjectAltName
    description:
    - 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., C(email), C(URI), C(DNS), C(RID),
      C(IP), C(dirName), C(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 C(useCommonNameForSAN) is set to I(false).
    - More at U(https://tools.ietf.org/html/rfc5280#section-4.2.1.6).
    elements: str
    type: list

basic_constraints:
    aliases:
    - basicConstraints
    description:
    - Indicates basic constraints, such as if the certificate is a CA.
    elements: str
    type: list
    version_added: '2.5'
    version_added_collection: ansible.builtin

organization_name:
    aliases:
    - O
    - organizationName
    description:
    - The organizationName field of the certificate signing request subject.
    type: str

extended_key_usage:
    aliases:
    - extKeyUsage
    - extendedKeyUsage
    description:
    - Additional restrictions (e.g. client authentication, server authentication) on the
      allowed purposes for which the public key may be used.
    elements: str
    type: list

key_usage_critical:
    aliases:
    - keyUsage_critical
    description:
    - Should the keyUsage extension be considered as critical.
    type: bool

authority_cert_issuer:
    description:
    - 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., C(email), C(URI), C(DNS), C(RID),
      C(IP), C(dirName), C(otherName) and the ones specific to your CA)
    - 'Example: C(DNS:ca.example.org)'
    - If specified, I(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 C(cryptography) backend is used!
    - The C(AuthorityKeyIdentifier) will only be added if at least one of I(authority_key_identifier),
      I(authority_cert_issuer) and I(authority_cert_serial_number) is specified.
    elements: str
    type: list
    version_added: '2.9'
    version_added_collection: ansible.builtin

privatekey_passphrase:
    description:
    - The passphrase for the private key.
    - This is required if the private key is password protected.
    type: str

select_crypto_backend:
    choices:
    - auto
    - cryptography
    - pyopenssl
    default: auto
    description:
    - Determines which crypto backend to use.
    - The default choice is C(auto), which tries to use C(cryptography) if available,
      and falls back to C(pyopenssl).
    - If set to C(pyopenssl), will try to use the L(pyOpenSSL,https://pypi.org/project/pyOpenSSL/)
      library.
    - If set to C(cryptography), will try to use the L(cryptography,https://cryptography.io/)
      library.
    - Please note that the C(pyopenssl) backend has been deprecated in Ansible 2.9, and
      will be removed in Ansible 2.13. From that point on, only the C(cryptography) backend
      will be available.
    type: str
    version_added: '2.8'
    version_added_collection: ansible.builtin

state_or_province_name:
    aliases:
    - ST
    - stateOrProvinceName
    description:
    - The stateOrProvinceName field of the certificate signing request subject.
    type: str

subject_key_identifier:
    description:
    - The subject key identifier as a hex string, where two bytes are separated by colons.
    - 'Example: C(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 I(create_subject_key_identifier) is C(no).
    - Note that this is only supported if the C(cryptography) backend is used!
    type: str
    version_added: '2.9'
    version_added_collection: ansible.builtin

use_common_name_for_san:
    aliases:
    - useCommonNameForSAN
    default: true
    description:
    - If set to C(yes), the module will fill the common name in for C(subject_alt_name)
      with C(DNS:) prefix if no SAN is specified.
    type: bool
    version_added: '2.8'
    version_added_collection: ansible.builtin

authority_key_identifier:
    description:
    - The authority key identifier as a hex string, where two bytes are separated by colons.
    - 'Example: C(00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff:00:11:22:33)'
    - If specified, I(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 C(cryptography) backend is used!
    - The C(AuthorityKeyIdentifier) will only be added if at least one of I(authority_key_identifier),
      I(authority_cert_issuer) and I(authority_cert_serial_number) is specified.
    type: str
    version_added: '2.9'
    version_added_collection: ansible.builtin

organizational_unit_name:
    aliases:
    - OU
    - organizationalUnitName
    description:
    - The organizationalUnitName field of the certificate signing request subject.
    type: str

ocsp_must_staple_critical:
    aliases:
    - ocspMustStaple_critical
    description:
    - 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 U(https://tools.ietf.org/html/rfc7633#section-4)).
    type: bool
    version_added: '2.5'
    version_added_collection: ansible.builtin

subject_alt_name_critical:
    aliases:
    - subjectAltName_critical
    description:
    - Should the subjectAltName extension be considered as critical.
    type: bool

basic_constraints_critical:
    aliases:
    - basicConstraints_critical
    description:
    - Should the basicConstraints extension be considered as critical.
    type: bool
    version_added: '2.5'
    version_added_collection: ansible.builtin

extended_key_usage_critical:
    aliases:
    - extKeyUsage_critical
    - extendedKeyUsage_critical
    description:
    - Should the extkeyUsage extension be considered as critical.
    type: bool

authority_cert_serial_number:
    description:
    - The authority cert serial number.
    - Note that this is only supported if the C(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 C(AuthorityKeyIdentifier) will only be added if at least one of I(authority_key_identifier),
      I(authority_cert_issuer) and I(authority_cert_serial_number) is specified.
    type: int
    version_added: '2.9'
    version_added_collection: ansible.builtin

create_subject_key_identifier:
    default: false
    description:
    - 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 C(cryptography) backend is used!
    type: bool
    version_added: '2.9'
    version_added_collection: ansible.builtin

Outputs

backup_file:
  description: Name of backup file created.
  returned: changed and if I(backup) is C(yes)
  sample: /path/to/www.ansible.com.csr.2019-03-09@11:22~
  type: str
basicConstraints:
  description: Indicates if the certificate belongs to a CA
  elements: str
  returned: changed or success
  sample:
  - CA:TRUE
  - pathLenConstraint:0
  type: list
extendedKeyUsage:
  description: Additional restriction on the public key purposes
  elements: str
  returned: changed or success
  sample:
  - clientAuth
  type: list
filename:
  description: Path to the generated Certificate Signing Request
  returned: changed or success
  sample: /etc/ssl/csr/www.ansible.com.csr
  type: str
keyUsage:
  description: Purpose for which the public key may be used
  elements: str
  returned: changed or success
  sample:
  - digitalSignature
  - keyAgreement
  type: list
ocsp_must_staple:
  description: Indicates whether the certificate has the OCSP Must Staple feature
    enabled
  returned: changed or success
  sample: false
  type: bool
privatekey:
  description: Path to the TLS/SSL private key the CSR was generated for
  returned: changed or success
  sample: /etc/ssl/private/ansible.com.pem
  type: str
subject:
  description: A list of the subject tuples attached to the CSR
  elements: list
  returned: changed or success
  sample: '[(''CN'', ''www.ansible.com''), (''O'', ''Ansible'')]'
  type: list
subjectAltName:
  description: The alternative names this CSR is valid for
  elements: str
  returned: changed or success
  sample:
  - DNS:www.ansible.com
  - DNS:m.ansible.com
  type: list

See also