|
32 | 32 | args: |
33 | 33 | creates: "{{ certificates_ca_directory_certs }}/{{ certificates_hostname }}.crt" |
34 | 34 |
|
| 35 | +- name: 'Read current SANs from server certificate' |
| 36 | + ansible.builtin.command: > |
| 37 | + openssl x509 |
| 38 | + -noout |
| 39 | + -ext subjectAltName |
| 40 | + -in "{{ certificates_ca_directory_certs }}/{{ certificates_hostname }}.crt" |
| 41 | + register: certificates_current_sans |
| 42 | + changed_when: false |
| 43 | + failed_when: false |
| 44 | + |
| 45 | +- name: 'Determine desired SANs' |
| 46 | + ansible.builtin.set_fact: |
| 47 | + certificates_desired_sans: >- |
| 48 | + DNS:{{ certificates_hostname }}{% for cname in certificates_cnames %}, DNS:{{ cname }}{% endfor %} |
| 49 | + certificates_current_sans_normalized: >- |
| 50 | + {{ certificates_current_sans.stdout | default('') |
| 51 | + | regex_search('DNS:.*') |
| 52 | + | default('') | trim }} |
| 53 | +
|
| 54 | +- name: 'Determine if server certificate SANs need updating' |
| 55 | + ansible.builtin.set_fact: |
| 56 | + certificates_update_sans: >- |
| 57 | + {{ certificates_current_sans.rc == 0 and |
| 58 | + certificates_desired_sans != certificates_current_sans_normalized }} |
| 59 | +
|
| 60 | +- name: 'Remove outdated server signing request' |
| 61 | + ansible.builtin.file: |
| 62 | + path: "{{ certificates_ca_directory_requests }}/{{ certificates_hostname }}.csr" |
| 63 | + state: absent |
| 64 | + when: certificates_update_sans | bool |
| 65 | + |
| 66 | +- name: 'Recreate server signing request with updated SANs' |
| 67 | + ansible.builtin.command: > |
| 68 | + openssl req |
| 69 | + -new |
| 70 | + -config "{{ certificates_ca_directory }}/openssl.cnf" |
| 71 | + -key "{{ certificates_ca_directory_keys }}/{{ certificates_hostname }}.key" |
| 72 | + -subj "/CN={{ certificates_hostname }}" |
| 73 | + -addext "subjectAltName = {{ certificates_desired_sans }}" |
| 74 | + -out "{{ certificates_ca_directory_requests }}/{{ certificates_hostname }}.csr" |
| 75 | + when: certificates_update_sans | bool |
| 76 | + |
| 77 | +- name: 'Remove outdated server certificate' |
| 78 | + ansible.builtin.file: |
| 79 | + path: "{{ certificates_ca_directory_certs }}/{{ certificates_hostname }}.crt" |
| 80 | + state: absent |
| 81 | + when: certificates_update_sans | bool |
| 82 | + |
| 83 | +- name: 'Re-sign server certificate with updated SANs' |
| 84 | + ansible.builtin.command: > |
| 85 | + openssl ca |
| 86 | + -create_serial |
| 87 | + -batch |
| 88 | + -extensions ssl_server |
| 89 | + -config "{{ certificates_ca_directory }}/openssl.cnf" |
| 90 | + -passin "file:{{ certificates_ca_directory_keys }}/ca.pwd" |
| 91 | + -in "{{ certificates_ca_directory_requests }}/{{ certificates_hostname }}.csr" |
| 92 | + -out "{{ certificates_ca_directory_certs }}/{{ certificates_hostname }}.crt" |
| 93 | + when: certificates_update_sans | bool |
| 94 | + |
35 | 95 | - name: 'Create client key' |
36 | 96 | ansible.builtin.command: > |
37 | 97 | openssl genrsa |
|
0 commit comments