community.crypto.openssl_pkcs12 module – Generate OpenSSL PKCS#12 archive
Note
This module is part of the community.crypto collection (version 2.26.5).
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.
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.
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_sizeand- maciter_sizeoptions are used. This can be overridden with the- select_crypto_backendoption.
Requirements
The below requirements are needed on the host that executes this module.
- PyOpenSSL >= 0.15, < 23.3.0 or cryptography >= 3.0 
Parameters
| Parameter | Comments | 
|---|---|
| 
 Choices: 
 | |
| The attributes the resulting filesystem object should have. To get supported flags look at the man page for  This string should contain the attributes in the same order as the one displayed by  The  | |
| 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: 
 | |
| Content of the certificate file in PEM format. Mutually exclusive with  | |
| The path to read certificates and private keys from. Must be in PEM format. Mutually exclusive with  | |
| Determines the encryption level used. 
 
 Note that this option is not used for idempotency. Choices: 
 | |
| Should the file be regenerated even if it already exists. Choices: 
 | |
| Specifies the friendly name for the certificate and private key. | |
| Name of the group that should own the filesystem object, as would be fed to  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. | |
| Number of times to repeat the encryption step. This is not considered during idempotency checks. This is only used by the  When using it, the default is  | |
| Number of times to repeat the MAC step. This is not considered during idempotency checks. This is only used by the  | |
| The permissions the resulting filesystem object should have. For those used to  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,  If  If  Specifying  | |
| List of other certificates to include. Pre Ansible 2.8 this parameter was called  Assumes there is one PEM-encoded certificate per file. If a file contains multiple PEM certificates, set  Mutually exclusive with  | |
| List of other certificates to include. Assumes there is one PEM-encoded certificate per item. If an item contains multiple PEM certificates, set  Mutually exclusive with  | |
| If set to  Choices: 
 | |
| Name of the user that should own the filesystem object, as would be fed to  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. | |
| The PKCS#12 password. Note: PKCS12 encryption is typically 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. (Source). | |
| Filename to write the PKCS#12 file to. | |
| Content of the private key file. Mutually exclusive with  | |
| Passphrase source to decrypt any input private keys with. | |
| File to read private key from. Mutually exclusive with  | |
| If set to  Choices: 
 | |
| Determines which crypto backend to use. The default choice is  If set to  If set to  Note that the  Choices: 
 | |
| The level part of the SELinux filesystem object context. This is the MLS/MCS attribute, sometimes known as the  When set to  | |
| The role part of the SELinux filesystem object context. When set to  | |
| The type part of the SELinux filesystem object context. When set to  | |
| The user part of the SELinux filesystem object context. By default it uses the  When set to  | |
| PKCS#12 file path to parse. | |
| Whether the file should exist or not. All parameters except  Choices: 
 | |
| 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: 
 | 
Attributes
| Attribute | Support | Description | 
|---|---|---|
| Support: full | Can run in  | |
| Support: none | Will return details on what has changed (or possibly needs changing in  | |
| Support: partial The module is not idempotent if  | 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. | |
| 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
- Generate and/or check OpenSSL certificates. 
- community.crypto.openssl_csr
- Generate OpenSSL Certificate Signing Request (CSR). 
- community.crypto.openssl_dhparam
- Generate OpenSSL Diffie-Hellman Parameters. 
- community.crypto.openssl_privatekey
- Generate OpenSSL private keys. 
- community.crypto.openssl_publickey
- Generate an OpenSSL public key from its private key. 
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: true
- 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 | 
|---|---|
| Name of backup file created. Returned: changed and if  Sample:  | |
| Path to the generate PKCS#12 file. Returned: changed or success Sample:  | |
| The (current or generated) PKCS#12’s content Base64 encoded. Returned: if  | |
| Path to the TLS/SSL private key the public key was generated from. Returned: changed or success Sample:  | 
