community.crypto.openssl_pkcs12_extract module – Extract certificate and private key from PKCS#12 archive

Note

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

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. You need further requirements to be able to use this module, see Requirements for details.

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

New in community.crypto 3.4.0

Synopsis

  • This module allows to extract certificates and private key from a PKCS#12 archive.

Requirements

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

  • cryptography >= 3.3

Parameters

Parameter

Comments

all_certs_path

path

If specified, the concatenation of all certificates will be written to this path in PEM format.

If backup=true and the file exists and is about to be overwritten, a backup will be created and its filename is returned as all_certs_backup_file.

At least one of combined_path, cert_path, privatekey_path, other_certs_path, and all_certs_path must be specified.

attributes

aliases: attr

string

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:

  • false ← (default)

  • true

cert_path

path

If specified, the certificate (if exists) associated to the private key will be written to this path in PEM format.

If backup=true and the file exists and is about to be overwritten, a backup will be created and its filename is returned as cert_backup_file.

At least one of combined_path, cert_path, privatekey_path, other_certs_path, and all_certs_path must be specified.

combined_path

path

If specified, the concatenation of private key (if exists), certificate (if exists), and other certificates will be written to this path. All objects are written in PEM format.

If backup=true and the file exists and is about to be overwritten, a backup will be created and its filename is returned as combined_backup_file.

At least one of combined_path, cert_path, privatekey_path, other_certs_path, and all_certs_path must be specified.

content

string

Base64 encoded contents of a PKCS#12 file.

Either path or content must be specified, but not both.

group

string

Name of the group that should own the filesystem object, as would be fed to 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.

Specifying a numeric group name (for example, “1000”) will be assumed to be a group ID (GID) and not a group name. To prevent confusion, avoid using purely numeric group names.

mode

any

The permissions the resulting filesystem object should have.

For those used to /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, '644' or '1777') so Ansible receives a string and can do its own conversion from string into number. Adding a leading zero (for example, 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, 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_certs_path

path

If specified, the concatenation of all other certificates (not associated to the private key) will be written to this path in PEM format.

If backup=true and the file exists and is about to be overwritten, a backup will be created and its filename is returned as other_certs_backup_file.

At least one of combined_path, cert_path, privatekey_path, other_certs_path, and all_certs_path must be specified.

owner

string

Name of the user that should own the filesystem object, as would be fed to 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 (for example, “1000”) will be assumed to be a user ID (UID) and not a username. To prevent confusion, avoid using purely numeric usernames.

passphrase

string

The PKCS#12 password.

path

path

Filename to read the PKCS#12 file from.

Either path or content must be specified, but not both.

privatekey_path

path

If specified, the private key will be written to this path in PEM format.

If backup=true and the file exists and is about to be overwritten, a backup will be created and its filename is returned as privatekey_backup_file.

At least one of combined_path, cert_path, privatekey_path, other_certs_path, and all_certs_path must be specified.

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.

unsafe_writes

boolean

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.

Choices:

  • false ← (default)

  • true

Attributes

Attribute

Support

Description

check_mode

Support: full

Can run in check_mode and return changed status prediction without modifying target.

diff_mode

Support: none

Will return details on what has changed (or possibly needs changing in check_mode), when in diff mode.

idempotent

Support: full

When run twice in a row outside check mode, with the same arguments, the second invocation indicates no change.

This assumes that the system controlled/queried by the module has not changed in a relevant way.

safe_file_operations

Support: full

Uses Ansible’s strict file operation functions to ensure proper permissions and avoid data corruption.

See Also

See also

community.crypto.x509_certificate_info

Provide information of OpenSSL X.509 certificates.

community.crypto.openssl_pkcs12

Generate OpenSSL PKCS#12 archive.

community.crypto.openssl_pkcs12_info

Return certificates and (optionally) private key of a PKCS#12 file.

community.crypto.openssl_privatekey_info

Provide information for OpenSSL private keys.

Examples

---
- name: Parse PKCS#12 file and concatenate private key and certs into PEM file
  community.crypto.openssl_pkcs12_extract:
    path: /opt/certs/ansible.p12
    combined_path: /opt/certs/ansible-combined.pem

Return Values

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

Key

Description

all_certs_backup_file

string

Name of backup file created.

Returned: backup=true, all_certs_path is specified, and the file contents have been changed

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

cert_backup_file

string

Name of backup file created.

Returned: backup=true, cert_path is specified, and the file contents have been changed

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

combined_backup_file

string

Name of backup file created.

Returned: backup=true, combined_path is specified, and the file contents have been changed

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

friendly_name

string

“Friendly name” of the certificate and private key pair.

If no private key is contained in the archive, this has value null.

Returned: success

Sample: "keypair"

other_certs_backup_file

string

Name of backup file created.

Returned: backup=true, other_certs_path is specified, and the file contents have been changed

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

privatekey_backup_file

string

Name of backup file created.

Returned: backup=true, privatekey_path is specified, and the file contents have been changed

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

Authors

  • Felix Fontein (@felixfontein)

  • Guillaume Delpierre (@gdelpierre)