|
--- |
|
- name: create jks file with certificate |
|
hosts: localhost |
|
become: false |
|
connection: local |
|
collections: [ community.crypto, community.general ] |
|
vars: |
|
secret_ca_passphrase: Ahsh7gaeeeBaov6j |
|
tasks: |
|
|
|
# start creating self-signed ca certificate |
|
- name: Create private key with password protection |
|
community.crypto.openssl_privatekey: |
|
path: /home/runner/ca-certificate.key |
|
passphrase: "{{ secret_ca_passphrase }}" |
|
cipher: auto |
|
|
|
- name: Create certificate signing request (CSR) for CA certificate |
|
community.crypto.openssl_csr_pipe: |
|
privatekey_path: /home/runner/ca-certificate.key |
|
privatekey_passphrase: "{{ secret_ca_passphrase }}" |
|
common_name: Testing CA |
|
use_common_name_for_san: false # since we do not specify SANs, don't use CN as a SAN |
|
basic_constraints: |
|
- 'CA:TRUE' |
|
basic_constraints_critical: yes |
|
key_usage: |
|
- keyCertSign |
|
key_usage_critical: true |
|
register: ca_csr |
|
|
|
- name: Create self-signed CA certificate from CSR |
|
community.crypto.x509_certificate: |
|
path: /home/runner/ca-certificate.pem |
|
csr_content: "{{ ca_csr.csr }}" |
|
privatekey_path: /home/runner/ca-certificate.key |
|
privatekey_passphrase: "{{ secret_ca_passphrase }}" |
|
provider: selfsigned |
|
# end creating self-signed ca certificate |
|
|
|
# start creating server certificate |
|
- name: Create private key for new certificate |
|
community.crypto.openssl_privatekey: |
|
path: /home/runner/certificate.key |
|
size: 2048 |
|
run_once: true |
|
|
|
- name: Create certificate signing request (CSR) for new certificate |
|
community.crypto.openssl_csr_pipe: |
|
privatekey_path: /home/runner/certificate.key |
|
common_name: localhost |
|
subject_alt_name: |
|
- "DNS:localhost.localdomain" |
|
- "DNS:myApp.cool" |
|
key_usage: |
|
- digitalSignature |
|
- keyAgreement |
|
- keyEncipherment |
|
- nonRepudiation |
|
extended_key_usage: |
|
- serverAuth |
|
run_once: true |
|
register: csr |
|
|
|
- name: Check whether certificate exists |
|
stat: |
|
path: /home/runner/certificate.pem |
|
run_once: true |
|
register: certificate_exists |
|
|
|
- name: Read existing certificate if exists |
|
slurp: |
|
src: /home/runner/certificate.pem |
|
when: certificate_exists.stat.exists |
|
run_once: true |
|
register: certificate |
|
|
|
- name: Sign certificate with our CA |
|
community.crypto.x509_certificate_pipe: |
|
content: "{{ (certificate.content | b64decode) if certificate_exists.stat.exists else omit }}" |
|
csr_content: "{{ csr.csr }}" |
|
provider: ownca |
|
ownca_path: /home/runner/ca-certificate.pem |
|
ownca_privatekey_path: /home/runner/ca-certificate.key |
|
ownca_privatekey_passphrase: "{{ secret_ca_passphrase }}" |
|
ownca_not_after: +3650d |
|
ownca_not_before: "-1d" |
|
run_once: true |
|
register: certificate |
|
|
|
- name: Write certificate file |
|
copy: |
|
dest: /home/runner/certificate.pem |
|
content: "{{ certificate.certificate }}" |
|
run_once: true |
|
when: certificate is changed |
|
# end creating server certificate |
|
|
|
- name: Create a keystore for the given certificate/private key pair |
|
community.general.java_keystore: |
|
name: certificate |
|
certificate_path: /home/runner/certificate.pem |
|
private_key_path: /home/runner/certificate.key |
|
password: secret |
|
dest: /home/runner/certificate.jks |
|
keystore_type: jks |