|
1 | 1 | --- |
| 2 | +- name: Ensure the custom directories to host certificates are present |
| 3 | + become: yes |
| 4 | + file: |
| 5 | + state: directory |
| 6 | + recurse: yes |
| 7 | + path: "/etc/ssl/{{ item.path }}" |
| 8 | + mode: "{{ item.mode }}" |
| 9 | + owner: root |
| 10 | + group: root |
| 11 | + loop: |
| 12 | + - {path: local/certs, mode: "0755"} |
| 13 | + - {path: local/private, mode: "0700"} |
2 | 14 |
|
3 | | - - name: Generate client private key |
4 | | - local_action: |
5 | | - module: openssl_privatekey |
6 | | - path: "{{cert_dir}}/{{tls_client_key}}" |
7 | | - size: "{{tls_client_key_size}}" |
8 | | - run_once: true |
9 | | - when: generate_client_cert |
10 | | - |
11 | | - - name: Generate CSR and key for client cert |
12 | | - local_action: |
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}}" \ |
17 | | - -keyout "{{cert_dir}}/{{tls_client_key}}" -out "{{cert_dir}}/{{tls_client_csr}}" |
18 | | - fi |
19 | | - args: |
20 | | - executable: /bin/bash |
21 | | - ignore_errors: true |
22 | | - run_once: true |
23 | | - when: generate_client_cert |
24 | | - |
25 | | - - name: Add required extension for client authentication |
26 | | - local_action: |
27 | | - module: > |
28 | | - shell echo extendedKeyUsage = clientAuth >> {{cert_dir}}/{{tls_client_extfile}} |
29 | | - ignore_errors: true |
30 | | - run_once: true |
31 | | - when: generate_client_cert |
32 | | - |
33 | | - # @AB TODO: using OpenSSL CA serial file does not always generate unique serial when running playbook against multiple hosts |
34 | | - - name: Sign client cert request with CA |
35 | | - local_action: |
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}} \ |
40 | | - -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 |
44 | | - ignore_errors: true |
45 | | - run_once: true |
46 | | - when: generate_client_cert |
| 15 | +- name: Check if the client private key exists |
| 16 | + delegate_to: localhost |
| 17 | + stat: |
| 18 | + path: "{{ cert_dir }}/{{ tls_client_key }}" |
| 19 | + register: client_key |
| 20 | + |
| 21 | +- name: Generate client private key |
| 22 | + delegate_to: localhost |
| 23 | + community.crypto.openssl_privatekey: |
| 24 | + path: "{{ cert_dir }}/{{ tls_client_key }}" |
| 25 | + size: "{{ tls_client_key_size}}" |
| 26 | + when: |
| 27 | + - not client_key.stat.exists |
| 28 | + - generate_client_cert |
| 29 | + register: client_key_file |
| 30 | + |
| 31 | +- name: Copy the key on the server |
| 32 | + become: yes |
| 33 | + copy: |
| 34 | + src: "{{ cert_dir }}/{{ tls_client_key}}" |
| 35 | + dest: /etc/ssl/local/certs/ |
| 36 | + mode: 0644 |
| 37 | + owner: root |
| 38 | + group: root |
| 39 | + when: client_key_file.changed |
| 40 | + |
| 41 | +- name: Check if the client CSR exists |
| 42 | + delegate_to: localhost |
| 43 | + stat: |
| 44 | + path: "{{ cert_dir }}/{{ tls_client_csr }}" |
| 45 | + register: client_csr |
| 46 | + |
| 47 | +- name: Generate CSR and key for client cert |
| 48 | + delegate_to: localhost |
| 49 | + community.crypto.openssl_csr: |
| 50 | + path: "{{ cert_dir }}/{{ tls_client_csr }}" |
| 51 | + privatekey_path: "{{ cert_dir }}/{{ tls_client_key }}" |
| 52 | + common_name: "{{ tls_client_commonname }}" |
| 53 | + extended_key_usage: |
| 54 | + - clientAuth |
| 55 | + when: |
| 56 | + - not client_csr.stat.exists |
| 57 | + - generate_client_cert |
| 58 | + |
| 59 | +- name: Check if the client cert exists |
| 60 | + delegate_to: localhost |
| 61 | + stat: |
| 62 | + path: "{{ cert_dir }}/{{ tls_client_cert }}" |
| 63 | + register: client_crt |
| 64 | + |
| 65 | +- name: Create and sign server cert request by CA |
| 66 | + delegate_to: localhost |
| 67 | + community.crypto.x509_certificate: |
| 68 | + path: "{{ cert_dir }}/{{ tls_client_cert }}" |
| 69 | + csr_path: "{{ cert_dir }}/{{ tls_client_csr }}" |
| 70 | + ownca_not_after: "+{{ tls_client_valid_days }}d" |
| 71 | + ownca_path: "{{ cert_dir }}/{{ tls_ca_cert }}" |
| 72 | + ownca_privatekey_path: "{{ cert_dir }}/{{ tls_ca_key }}" |
| 73 | + provider: ownca |
| 74 | + when: |
| 75 | + - not client_crt.stat.exists |
| 76 | + - generate_client_cert |
| 77 | + register: client_cert_file |
| 78 | + |
| 79 | +- name: Copy the certificate to the remote machine |
| 80 | + become: yes |
| 81 | + copy: |
| 82 | + src: "{{ cert_dir }}/{{ tls_client_cert }}" |
| 83 | + dest: /etc/ssl/local/private |
| 84 | + mode: 0600 |
| 85 | + owner: root |
| 86 | + group: root |
| 87 | + when: client_cert_file.changed |
0 commit comments