| 1 |
.. |
| 2 |
Copyright (c) Ansible Project |
| 3 |
GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
.. _ansible_collections.community.crypto.docsite.guide_selfsigned: |
| 7 |
|
| 8 |
How to create self-signed certificates |
| 9 |
====================================== |
| 10 |
|
| 11 |
The `community.crypto collection <https://galaxy.ansible.com/ui/repo/published/community/crypto/>`_ offers multiple modules that create private keys, certificate signing requests, and certificates. This guide shows how to create self-signed certificates. |
| 12 |
|
| 13 |
For creating any kind of certificate, you always have to start with a private key. You can use the :ansplugin:`community.crypto.openssl_privatekey module <community.crypto.openssl_privatekey#module>` to create a private key. If you only specify :ansopt:`community.crypto.openssl_privatekey#module:path`, the default parameters will be used. This will result in a 4096 bit RSA private key: |
| 14 |
|
| 15 |
.. code-block:: yaml+jinja |
| 16 |
|
| 17 |
- name: Create private key (RSA, 4096 bits) |
| 18 |
community.crypto.openssl_privatekey: |
| 19 |
path: /path/to/certificate.key |
| 20 |
|
| 21 |
You can specify :ansopt:`community.crypto.openssl_privatekey#module:type` to select another key type, :ansopt:`community.crypto.openssl_privatekey#module:size` to select a different key size (only available for RSA and DSA keys), or :ansopt:`community.crypto.openssl_privatekey#module:passphrase` if you want to store the key password-protected: |
| 22 |
|
| 23 |
.. code-block:: yaml+jinja |
| 24 |
|
| 25 |
- name: Create private key (X25519) with password protection |
| 26 |
community.crypto.openssl_privatekey: |
| 27 |
path: /path/to/certificate.key |
| 28 |
type: X25519 |
| 29 |
passphrase: changeme |
| 30 |
|
| 31 |
To create a very simple self-signed certificate with no specific information, you can proceed directly with the :ansplugin:`community.crypto.x509_certificate module <community.crypto.x509_certificate#module>`: |
| 32 |
|
| 33 |
.. code-block:: yaml+jinja |
| 34 |
|
| 35 |
- name: Create simple self-signed certificate |
| 36 |
community.crypto.x509_certificate: |
| 37 |
path: /path/to/certificate.pem |
| 38 |
privatekey_path: /path/to/certificate.key |
| 39 |
provider: selfsigned |
| 40 |
|
| 41 |
(If you used :ansopt:`community.crypto.openssl_privatekey#module:passphrase` for the private key, you have to provide :ansopt:`community.crypto.x509_certificate#module:privatekey_passphrase`.) |
| 42 |
|
| 43 |
You can use :ansopt:`community.crypto.x509_certificate#module:selfsigned_not_after` to define when the certificate expires (default: in roughly 10 years), and :ansopt:`community.crypto.x509_certificate#module:selfsigned_not_before` to define from when the certificate is valid (default: now). |
| 44 |
|
| 45 |
To define further properties of the certificate, like the subject, Subject Alternative Names (SANs), key usages, name constraints, etc., you need to first create a Certificate Signing Request (CSR) and provide it to the :ansplugin:`community.crypto.x509_certificate module <community.crypto.x509_certificate#module>`. If you do not need the CSR file, you can use the :ansplugin:`community.crypto.openssl_csr_pipe module <community.crypto.openssl_csr_pipe#module>` as in the example below. (To store it to disk, use the :ansplugin:`community.crypto.openssl_csr module <community.crypto.openssl_csr#module>` instead.) |
| 46 |
|
| 47 |
.. code-block:: yaml+jinja |
| 48 |
|
| 49 |
- name: Create certificate signing request (CSR) for self-signed certificate |
| 50 |
community.crypto.openssl_csr_pipe: |
| 51 |
privatekey_path: /path/to/certificate.key |
| 52 |
common_name: ansible.com |
| 53 |
organization_name: Ansible, Inc. |
| 54 |
subject_alt_name: |
| 55 |
- "DNS:ansible.com" |
| 56 |
- "DNS:www.ansible.com" |
| 57 |
- "DNS:docs.ansible.com" |
| 58 |
register: csr |
| 59 |
|
| 60 |
- name: Create self-signed certificate from CSR |
| 61 |
community.crypto.x509_certificate: |
| 62 |
path: /path/to/certificate.pem |
| 63 |
csr_content: "{{ csr.csr }}" |
| 64 |
privatekey_path: /path/to/certificate.key |
| 65 |
provider: selfsigned |