community.crypto.openssl_pkcs12 module – Generate OpenSSL PKCS#12 archive

Note

This module is part of the community.crypto collection (version 2.3.4).

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.openssl_pkcs12.

Synopsis

  • This module allows one to (re-)generate PKCS#12.

  • The module can use the cryptography Python library, or the pyOpenSSL Python library. By default, it tries to detect which one is available, assuming none of the iter_size and maciter_size options are used. This can be overridden with the select_crypto_backend option.

Requirements

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

  • PyOpenSSL >= 0.15 or cryptography >= 3.0

Parameters

Parameter

Comments

action

string

export or parse a PKCS#12.

Choices:

  • export ← (default)

  • parse

attributes

aliases: attr

string

added in 2.3 of ansible.builtin

The attributes the resulting filesystem object 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.

backup

boolean

Create a backup file including a timestamp so you can get the original output file back if you overwrote it with a new one by accident.

Choices:

  • no ← (default)

  • yes

certificate_path

path

The path to read certificates and private keys from.

Must be in PEM format.

force

boolean

Should the file be regenerated even if it already exists.

Choices:

  • no ← (default)

  • yes

friendly_name

aliases: name

string

Specifies the friendly name for the certificate and private key.

group

string

Name of the group that should own the filesystem object, as would be fed to chown.

iter_size

integer

Number of times to repeat the encryption step.

This is not considered during idempotency checks.

This is only used by the pyopenssl backend. When using it, the default is 2048.

maciter_size

integer

Number of times to repeat the MAC step.

This is not considered during idempotency checks.

This is only used by the pyopenssl backend. When using it, the default is 1.

mode

raw

The permissions the resulting filesystem object 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).

If mode is not specified and the destination filesystem object does not exist, the default umask on the system will be used when setting the mode for the newly created filesystem object.

If mode is not specified and the destination filesystem object does exist, the mode of the existing filesystem object will be used.

Specifying mode is the best way to ensure filesystem objects are created with the correct permissions. See CVE-2020-1736 for further details.

other_certificates

aliases: ca_certificates

list / elements=path

List of other certificates to include. Pre Ansible 2.8 this parameter was called ca_certificates.

Assumes there is one PEM-encoded certificate per file. If a file contains multiple PEM certificates, set other_certificates_parse_all to true.

other_certificates_parse_all

boolean

added in 1.4.0 of community.crypto

If set to true, assumes that the files mentioned in other_certificates can contain more than one certificate per file (or even none per file).

Choices:

  • no ← (default)

  • yes

owner

string

Name of the user that should own the filesystem object, as would be fed to chown.

passphrase

string

The PKCS#12 password.

Note: PKCS12 encryption is not secure and should not be used as a security mechanism. If you need to store or send a PKCS12 file safely, you should additionally encrypt it with something else.

path

path / required

Filename to write the PKCS#12 file to.

privatekey_content

string

added in 2.3.0 of community.crypto

Content of the private key file.

Mutually exclusive with privatekey_path.

privatekey_passphrase

string

Passphrase source to decrypt any input private keys with.

privatekey_path

path

File to read private key from.

Mutually exclusive with privatekey_content.

return_content

boolean

added in 1.0.0 of community.crypto

If set to yes, will return the (current or generated) PKCS#12’s content as pkcs12.

Choices:

  • no ← (default)

  • yes

select_crypto_backend

string

added in 1.7.0 of community.crypto

Determines which crypto backend to use.

The default choice is auto, which tries to use cryptography if available, and falls back to pyopenssl. If one of iter_size or maciter_size is used, auto will always result in pyopenssl to be chosen for backwards compatibility.

If set to pyopenssl, will try to use the pyOpenSSL library.

If set to cryptography, will try to use the cryptography library.

Choices:

  • auto ← (default)

  • cryptography

  • pyopenssl

selevel

string

The level part of the SELinux filesystem object 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 filesystem object context.

When set to _default, it will use the role portion of the policy if available.

setype

string

The type part of the SELinux filesystem object context.

When set to _default, it will use the type portion of the policy if available.

seuser

string

The user part of the SELinux filesystem object 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.

src

path

PKCS#12 file path to parse.

state

string

Whether the file should exist or not. All parameters except path are ignored when state is absent.

Choices:

  • absent

  • present ← (default)

unsafe_writes

boolean

added in 2.2 of ansible.builtin

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 objecs, 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.

Choices:

  • no ← (default)

  • yes

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_dhparam

The official documentation on the community.crypto.openssl_dhparam module.

community.crypto.openssl_privatekey

The official documentation on the community.crypto.openssl_privatekey module.

community.crypto.openssl_publickey

The official documentation on the community.crypto.openssl_publickey module.

Examples

- name: Generate PKCS#12 file
  community.crypto.openssl_pkcs12:
    action: export
    path: /opt/certs/ansible.p12
    friendly_name: raclette
    privatekey_path: /opt/certs/keys/key.pem
    certificate_path: /opt/certs/cert.pem
    other_certificates: /opt/certs/ca.pem
    # Note that if /opt/certs/ca.pem contains multiple certificates,
    # only the first one will be used. See the other_certificates_parse_all
    # option for changing this behavior.
    state: present

- name: Generate PKCS#12 file
  community.crypto.openssl_pkcs12:
    action: export
    path: /opt/certs/ansible.p12
    friendly_name: raclette
    privatekey_content: '{{ private_key_contents }}'
    certificate_path: /opt/certs/cert.pem
    other_certificates_parse_all: true
    other_certificates:
      - /opt/certs/ca_bundle.pem
        # Since we set other_certificates_parse_all to true, all
        # certificates in the CA bundle are included and not just
        # the first one.
      - /opt/certs/intermediate.pem
        # In case this file has multiple certificates in it,
        # all will be included as well.
    state: present

- name: Change PKCS#12 file permission
  community.crypto.openssl_pkcs12:
    action: export
    path: /opt/certs/ansible.p12
    friendly_name: raclette
    privatekey_path: /opt/certs/keys/key.pem
    certificate_path: /opt/certs/cert.pem
    other_certificates: /opt/certs/ca.pem
    state: present
    mode: '0600'

- name: Regen PKCS#12 file
  community.crypto.openssl_pkcs12:
    action: export
    src: /opt/certs/ansible.p12
    path: /opt/certs/ansible.p12
    friendly_name: raclette
    privatekey_path: /opt/certs/keys/key.pem
    certificate_path: /opt/certs/cert.pem
    other_certificates: /opt/certs/ca.pem
    state: present
    mode: '0600'
    force: yes

- name: Dump/Parse PKCS#12 file
  community.crypto.openssl_pkcs12:
    action: parse
    src: /opt/certs/ansible.p12
    path: /opt/certs/ansible.pem
    state: present

- name: Remove PKCS#12 file
  community.crypto.openssl_pkcs12:
    path: /opt/certs/ansible.p12
    state: absent

Return Values

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

Key

Description

backup_file

string

Name of backup file created.

Returned: changed and if backup is yes

Sample:/path/to/ansible.com.pem.2019-03-09@11:22~”

filename

string

Path to the generate PKCS#12 file.

Returned: changed or success

Sample: “/opt/certs/ansible.p12”

pkcs12

string

added in 1.0.0 of community.crypto

The (current or generated) PKCS#12’s content Base64 encoded.

Returned: if state is present and return_content is yes

privatekey

string

Path to the TLS/SSL private key the public key was generated from.

Returned: changed or success

Sample: “/etc/ssl/private/ansible.com.pem”

Authors

  • Guillaume Delpierre (@gdelpierre)