refactor: extract the duplicated certificate concatenation into a shared task file
Building chain.pem, fullchain.pem and all.pem was implemented seven times across three task files with identical stat, awk and copy tasks. The blocks now include tasks/concatenate.yaml and pass the sources, the destination and the mode, which removes about a hundred lines. Two side effects come with it. Every source file is checked instead of only the foreign one, so a missing file skips the block instead of letting awk fail. And the trailing newline of the result is kept, because stdout_lines joined by a newline dropped it. Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -38,31 +38,13 @@
|
||||
- fullchain.pem
|
||||
|
||||
- name: Create file with private key and fullchain file of root Certificate Authority (CA)
|
||||
block:
|
||||
- name: Check if private key exists
|
||||
ansible.builtin.stat:
|
||||
path: "{{ certificate_authority_root_ca_path }}/privkey.pem"
|
||||
register: _stat_result
|
||||
- name: Concatenate private key and fullchain file of root Certificate Authority (CA)
|
||||
vars:
|
||||
_chain_files:
|
||||
- "{{ certificate_authority_root_ca_path }}/privkey.pem"
|
||||
- "{{ certificate_authority_root_ca_path }}/fullchain.pem"
|
||||
ansible.builtin.command:
|
||||
cmd: awk 1 {{ _chain_files | join(' ') }}
|
||||
register: chain_content
|
||||
changed_when: false
|
||||
when: _stat_result.stat.exists is defined and
|
||||
_stat_result.stat.exists
|
||||
- name: Create concatenated file
|
||||
ansible.builtin.copy:
|
||||
content: "{{ chain_content.stdout_lines | join('\n') }}"
|
||||
dest: "{{ certificate_authority_root_ca_path }}/all.pem"
|
||||
owner: "root"
|
||||
group: "root"
|
||||
mode: "0600"
|
||||
when: _stat_result.stat.exists is defined and
|
||||
_stat_result.stat.exists
|
||||
ansible.builtin.include_tasks: concatenate.yaml
|
||||
vars:
|
||||
_concat_sources:
|
||||
- "{{ certificate_authority_root_ca_path }}/privkey.pem"
|
||||
- "{{ certificate_authority_root_ca_path }}/fullchain.pem"
|
||||
_concat_dest: "{{ certificate_authority_root_ca_path }}/all.pem"
|
||||
_concat_mode: "0600"
|
||||
|
||||
- name: Import certificate of root Certificate Authority (CA) into systems trust store
|
||||
vars:
|
||||
|
||||
Reference in New Issue
Block a user