|
1 | 1 | --- |
| 2 | +- name: "Check if Cert already exists in certificates directory" |
| 3 | + stat: |
| 4 | + path: "{{ vault_pki_certificates_directory }}/{{ item.common_name | replace(' ', '-') }}.crt" |
| 5 | + register: cert_stat_file |
| 6 | + delegate_to: "{{ vault_pki_write_certificates_host }}" |
| 7 | + loop: "{{ vault_pki_certificate_subject }}" |
| 8 | + loop_control: |
| 9 | + label: "{{ item.common_name }}" |
| 10 | + when: vault_pki_write_certificate_files | bool |
| 11 | + |
2 | 12 | - name: "Generate Certificate" |
3 | 13 | hashivault_pki_cert_issue: |
4 | 14 | url: "{{ vault_api_addr }}" |
|
9 | 19 | role: "{{ item.role }}" |
10 | 20 | extra_params: "{{ item.extra_params }}" |
11 | 21 | loop: "{{ vault_pki_certificate_subject }}" |
12 | | - register: certificate_data |
| 22 | + loop_control: |
| 23 | + label: "{{ item.common_name }}" |
| 24 | + register: cert_data |
13 | 25 |
|
14 | 26 | - name: "Write out certificate pem_bundle" |
15 | 27 | vars: |
16 | | - cert_name: "{{ item.item.common_name if item.item.common_name | length > 0 else item.item.extra_params.ip_sans }}" |
| 28 | + cert_name: "{{ item.item.common_name if item.item.common_name | default() | length > 0 else item.item.extra_params.ip_sans | default() }}" |
| 29 | + cert_file: "{{ cert_stat_file.results | selectattr('item.common_name', 'match', item.item.common_name) | first }}" |
17 | 30 | copy: |
18 | 31 | content: | |
19 | 32 | {{ item.data.certificate }} |
|
22 | 35 | dest: "{{ vault_pki_certificates_directory }}/{{ cert_name | replace(' ', '-') }}.pem" |
23 | 36 | mode: 0600 |
24 | 37 | delegate_to: "{{ vault_pki_write_certificates_host }}" |
25 | | - loop: "{{ certificate_data.results }}" |
26 | | - when: vault_pki_write_certificate_files | bool |
| 38 | + loop: "{{ cert_data.results }}" |
| 39 | + loop_control: |
| 40 | + label: "{{ cert_name | default() }}" |
| 41 | + when: |
| 42 | + - vault_pki_write_pem_bundle | bool |
| 43 | + - vault_pki_write_certificate_files | bool |
| 44 | + - not cert_file.stat.exists or vault_pki_overwrite_certificates | bool |
| 45 | + |
| 46 | +- name: "Write out certificate" |
| 47 | + vars: |
| 48 | + cert_name: "{{ item.item.common_name if item.item.common_name | default() | length > 0 else item.item.extra_params.ip_sans | default() }}" |
| 49 | + cert_file: "{{ cert_stat_file.results | selectattr('item.common_name', 'match', item.item.common_name) | first }}" |
| 50 | + copy: |
| 51 | + content: | |
| 52 | + {{ item.data.certificate }} |
| 53 | + {{ item.data.issuing_ca }} |
| 54 | + dest: "{{ vault_pki_certificates_directory }}/{{ cert_name | replace(' ', '-') }}.crt" |
| 55 | + mode: 0600 |
| 56 | + delegate_to: "{{ vault_pki_write_certificates_host }}" |
| 57 | + loop: "{{ cert_data.results }}" |
| 58 | + loop_control: |
| 59 | + label: "{{ cert_name | default() }}" |
| 60 | + when: |
| 61 | + - not vault_pki_write_pem_bundle | bool |
| 62 | + - vault_pki_write_certificate_files | bool |
| 63 | + - not cert_file.stat.exists or vault_pki_overwrite_certificates | bool |
| 64 | + |
| 65 | +- name: "Write out key" |
| 66 | + vars: |
| 67 | + cert_name: "{{ item.item.common_name if item.item.common_name | default() | length > 0 else item.item.extra_params.ip_sans | default() }}" |
| 68 | + cert_file: "{{ cert_stat_file.results | selectattr('item.common_name', 'match', item.item.common_name) | first }}" |
| 69 | + copy: |
| 70 | + content: | |
| 71 | + {{ item.data.private_key }} |
| 72 | + dest: "{{ vault_pki_certificates_directory }}/{{ cert_name | replace(' ', '-') }}.key" |
| 73 | + mode: 0600 |
| 74 | + delegate_to: "{{ vault_pki_write_certificates_host }}" |
| 75 | + loop: "{{ cert_data.results }}" |
| 76 | + loop_control: |
| 77 | + label: "{{ cert_name | default() }}" |
| 78 | + when: |
| 79 | + - not vault_pki_write_pem_bundle | bool |
| 80 | + - vault_pki_write_certificate_files | bool |
| 81 | + - not cert_file.stat.exists or vault_pki_overwrite_certificates | bool |
0 commit comments