Skip to content
This repository was archived by the owner on Jan 5, 2025. It is now read-only.

Commit 9bf495b

Browse files
author
Aman
committed
Fixed overwriting of certs when re-running playbook
1 parent 35275ed commit 9bf495b

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
Generate TLS certificates
22
=========================
3-
Generates self-signed CA, client and server certificates. Runs locally on control machine. **Note:** Ansible crypto modules do not support self-signed certs, using `shell` command instead as required.
3+
Generates self-signed CA, client and server certificates. Runs locally on control machine.
44

5-
**WARNING: re-running this role in the same output folder will overwrite any existing certs and keys!**
5+
Notes:
6+
- Will not overwrite any files in output cert dir
7+
- Ansible crypto modules do not support signing certs with own CA yet, using `shell` command instead. Should be resolved in Ansible 2.7 using the [ownca provider](https://github.com/ansible/ansible/commit/b61b113fb9e3fcfcb25f4a8aaabad618e3209ce1).
68

79

810
Requirements
@@ -37,7 +39,7 @@ Example Playbook
3739
- include_vars: vars.yaml
3840
3941
- name: Generate certs
40-
import_role:
42+
import_role:
4143
name: generate-tls-certs
4244
4345
```

tasks/generate-ca-cert.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88

99
- name: Generate self-signed cert for CA
1010
local_action:
11-
module: >
12-
shell openssl req -x509 -new -days {{tls_ca_valid_days}} -sha256 -nodes -key {{cert_dir}}/{{tls_ca_key}} -out {{cert_dir}}/{{tls_ca_cert}}
11+
module: |
12+
shell if [ ! -e {{cert_dir}}/{{tls_ca_cert}} ]
13+
then
14+
openssl req -x509 -new -days {{tls_ca_valid_days}} -sha256 -nodes -key {{cert_dir}}/{{tls_ca_key}} -out {{cert_dir}}/{{tls_ca_cert}} \
1315
-subj "{% if tls_ca_country is defined%}/C={{tls_ca_country}}{% endif %}{% if tls_ca_state is defined%}/ST={{tls_ca_state}}{% endif %}{% if tls_ca_locality is defined %}/L={{tls_ca_locality}}{% endif %}{% if tls_ca_organization is defined %}/O={{tls_ca_organization}}{% endif %}{% if tls_ca_organizationalunit is defined %}/OU={{tls_ca_organizationalunit}}{% endif %}/CN={{tls_ca_commonname}}{% if tls_ca_email is defined %}/emailAddress={{tls_ca_email}}{% endif %}"
16+
fi
17+
args:
18+
executable: /bin/bash
1419
ignore_errors: true
1520
run_once: true

tasks/generate-client-cert.yaml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010

1111
- name: Generate CSR and key for client cert
1212
local_action:
13-
module: >
14-
shell openssl req -newkey rsa:{{tls_client_key_size}} -nodes -subj "/CN={{tls_client_commonname}}"
13+
module: |
14+
shell if [ ! -e {{cert_dir}}/{{tls_client_csr}} ]
15+
then
16+
openssl req -newkey rsa:{{tls_client_key_size}} -nodes -subj "/CN={{tls_client_commonname}}" \
1517
-keyout "{{cert_dir}}/{{tls_client_key}}" -out "{{cert_dir}}/{{tls_client_csr}}"
18+
fi
19+
args:
20+
executable: /bin/bash
1621
ignore_errors: true
1722
run_once: true
1823
when: generate_client_cert
@@ -28,9 +33,14 @@
2833
# @AB TODO: using OpenSSL CA serial file does not always generate unique serial when running playbook against multiple hosts
2934
- name: Sign client cert request with CA
3035
local_action:
31-
module: >
32-
shell openssl x509 -req -sha256 -days {{tls_client_valid_days}} -CA {{cert_dir}}/{{tls_ca_cert}} -CAkey {{cert_dir}}/{{tls_ca_key}}
36+
module: |
37+
shell if [ ! -e {{cert_dir}}/{{tls_client_cert}} ]
38+
then
39+
openssl x509 -req -sha256 -days {{tls_client_valid_days}} -CA {{cert_dir}}/{{tls_ca_cert}} -CAkey {{cert_dir}}/{{tls_ca_key}} \
3340
-set_serial {{ 999999999 | random }} -in {{cert_dir}}/{{tls_client_csr}} -out {{cert_dir}}/{{tls_client_cert}} -extfile {{cert_dir}}/{{tls_client_extfile}}
41+
fi
42+
args:
43+
executable: /bin/bash
3444
ignore_errors: true
3545
run_once: true
3646
when: generate_client_cert

tasks/generate-server-cert.yaml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
# Generate server cert
33
- name: Create CSR for server cert
44
local_action:
5-
module: >
6-
shell openssl req -newkey rsa:{{tls_server_key_size}} -nodes -subj "/CN={{inventory_hostname}}"
5+
module: |
6+
shell if [ ! -e {{cert_dir}}/{{inventory_hostname_short}}.csr ]
7+
then
8+
openssl req -newkey rsa:{{tls_server_key_size}} -nodes -subj "/CN={{inventory_hostname}}" \
79
-keyout "{{cert_dir}}/{{inventory_hostname_short}}.key" -out "{{cert_dir}}/{{inventory_hostname_short}}.csr"
10+
fi
11+
args:
12+
executable: /bin/bash
813
ignore_errors: true
914
when: generate_server_cert
1015

@@ -19,10 +24,15 @@
1924

2025
- name: Sign server cert request by CA
2126
local_action:
22-
module: >
23-
shell openssl x509 -req -sha256 -days {{tls_server_valid_days}}
24-
-CA "{{cert_dir}}/{{tls_ca_cert}}" -CAkey "{{cert_dir}}/{{tls_ca_key}}" -set_serial {{ 999999999 | random }}
25-
-in "{{cert_dir}}/{{inventory_hostname_short}}.csr" -out "{{cert_dir}}/{{inventory_hostname_short}}.pem"
26-
{% if tls_server_enable_san %}-extfile "{{cert_dir}}/{{inventory_hostname_short}}-extfile.cnf"{% endif %}
27+
module: |
28+
shell if [ ! -e {{cert_dir}}/{{inventory_hostname_short}}.pem ]
29+
then
30+
openssl x509 -req -sha256 -days {{tls_server_valid_days}} \
31+
-CA "{{cert_dir}}/{{tls_ca_cert}}" -CAkey "{{cert_dir}}/{{tls_ca_key}}" -set_serial {{ 999999999 | random }} \
32+
-in "{{cert_dir}}/{{inventory_hostname_short}}.csr" -out "{{cert_dir}}/{{inventory_hostname_short}}.pem" {% if tls_server_enable_san %}-extfile "{{cert_dir}}/{{inventory_hostname_short}}-extfile.cnf"{% endif %}
33+
34+
fi
35+
args:
36+
executable: /bin/bash
2737
ignore_errors: true
2838
when: generate_server_cert
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
subjectAltName = DNS:{{inventory_hostname}},DNS:{{inventory_hostname_short}},IP:{{(alt_interface_ip is defined) | ternary(alt_interface_ip, ansible_host)}},IP:0.0.0.0,IP:127.0.0.1
1+
subjectAltName = DNS:{{inventory_hostname}},DNS:{{inventory_hostname_short}},IP:{{(alt_interface_ip is defined) | ternary(alt_interface_ip, ansible_default_ipv4.address)}},IP:0.0.0.0,IP:127.0.0.1
22

33
extendedKeyUsage = serverAuth

0 commit comments

Comments
 (0)