diff --git a/tasks/client_certificate.yaml b/tasks/client_certificate.yaml index 54d9d9a..9266ded 100644 --- a/tasks/client_certificate.yaml +++ b/tasks/client_certificate.yaml @@ -45,82 +45,28 @@ not certificate_authority_client_create - name: Create certificate chain file - block: - - name: Check if intermediate certificate exists - ansible.builtin.stat: - path: "{{ certificate_authority_intermediate_ca_path }}/cert.pem" - register: _stat_result - - name: Concatenate client certificate and intermediate certificate - vars: - _chain_files: - - "{{ certificate_authority_client_path }}/cert.pem" - - "{{ certificate_authority_intermediate_ca_path }}/cert.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 chain file - ansible.builtin.copy: - content: "{{ chain_content.stdout_lines | join('\n') }}" - dest: "{{ certificate_authority_client_path }}/chain.pem" - owner: "root" - group: "root" - mode: "0644" - when: _stat_result.stat.exists is defined and - _stat_result.stat.exists + ansible.builtin.include_tasks: concatenate.yaml + vars: + _concat_sources: + - "{{ certificate_authority_client_path }}/cert.pem" + - "{{ certificate_authority_intermediate_ca_path }}/cert.pem" + _concat_dest: "{{ certificate_authority_client_path }}/chain.pem" + _concat_mode: "0644" - name: Create certificate fullchain file - block: - - name: Check if intermediate chain exists - ansible.builtin.stat: - path: "{{ certificate_authority_intermediate_ca_path }}/chain.pem" - register: _stat_result - - name: Concatenate client certificate and intermediate chain file - vars: - _chain_files: - - "{{ certificate_authority_client_path }}/cert.pem" - - "{{ certificate_authority_intermediate_ca_path }}/chain.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 fullchain file - ansible.builtin.copy: - content: "{{ chain_content.stdout_lines | join('\n') }}" - dest: "{{ certificate_authority_client_path }}/fullchain.pem" - owner: "root" - group: "root" - mode: "0644" - when: _stat_result.stat.exists is defined and - _stat_result.stat.exists + ansible.builtin.include_tasks: concatenate.yaml + vars: + _concat_sources: + - "{{ certificate_authority_client_path }}/cert.pem" + - "{{ certificate_authority_intermediate_ca_path }}/chain.pem" + _concat_dest: "{{ certificate_authority_client_path }}/fullchain.pem" + _concat_mode: "0644" - name: Create file with private key and fullchain file of the client - block: - - name: Check if fullchain exists - ansible.builtin.stat: - path: "{{ certificate_authority_client_path }}/fullchain.pem" - register: _stat_result - - name: Concatenate private key and fullchain file of the client - vars: - _chain_files: - - "{{ certificate_authority_client_path }}/privkey.pem" - - "{{ certificate_authority_client_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_client_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_client_path }}/privkey.pem" + - "{{ certificate_authority_client_path }}/fullchain.pem" + _concat_dest: "{{ certificate_authority_client_path }}/all.pem" + _concat_mode: "0600" diff --git a/tasks/concatenate.yaml b/tasks/concatenate.yaml new file mode 100644 index 0000000..124c6ea --- /dev/null +++ b/tasks/concatenate.yaml @@ -0,0 +1,24 @@ +--- + +# awk 1 prints every line and thereby normalizes source files whose last line lacks a newline. +- name: Check the source files of {{ _concat_dest }} + ansible.builtin.stat: + path: "{{ item }}" + register: _concat_stat + loop: "{{ _concat_sources }}" + +- name: Read the source files of {{ _concat_dest }} + ansible.builtin.command: + cmd: "awk 1 {{ _concat_sources | join(' ') }}" + register: _concat_content + changed_when: false + when: _concat_stat.results | rejectattr('stat.exists') | list | length == 0 + +- name: Write {{ _concat_dest }} + ansible.builtin.copy: + content: "{{ _concat_content.stdout }}\n" + dest: "{{ _concat_dest }}" + owner: "root" + group: "root" + mode: "{{ _concat_mode }}" + when: _concat_content is not skipped diff --git a/tasks/intermediate_certificate_authority.yaml b/tasks/intermediate_certificate_authority.yaml index 0a4a05f..2a7152a 100644 --- a/tasks/intermediate_certificate_authority.yaml +++ b/tasks/intermediate_certificate_authority.yaml @@ -45,82 +45,28 @@ not certificate_authority_intermediate_ca_create - name: Create certificate chain file - block: - - name: Check if root certificate exists - ansible.builtin.stat: - path: "{{ certificate_authority_root_ca_path }}/cert.pem" - register: _stat_result - - name: Concatenate intermediate certificate and root certificate - vars: - _chain_files: - - "{{ certificate_authority_intermediate_ca_path }}/cert.pem" - - "{{ certificate_authority_root_ca_path }}/cert.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 chain file - ansible.builtin.copy: - content: "{{ chain_content.stdout_lines | join('\n') }}" - dest: "{{ certificate_authority_intermediate_ca_path }}/chain.pem" - owner: "root" - group: "root" - mode: "0644" - when: _stat_result.stat.exists is defined and - _stat_result.stat.exists + ansible.builtin.include_tasks: concatenate.yaml + vars: + _concat_sources: + - "{{ certificate_authority_intermediate_ca_path }}/cert.pem" + - "{{ certificate_authority_root_ca_path }}/cert.pem" + _concat_dest: "{{ certificate_authority_intermediate_ca_path }}/chain.pem" + _concat_mode: "0644" - name: Create certificate fullchain file - block: - - name: Check if root chain exists - ansible.builtin.stat: - path: "{{ certificate_authority_root_ca_path }}/chain.pem" - register: _stat_result - - name: Concatenate intermediate certificate and root chain file - vars: - _chain_files: - - "{{ certificate_authority_intermediate_ca_path }}/cert.pem" - - "{{ certificate_authority_root_ca_path }}/chain.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 fullchain file - ansible.builtin.copy: - content: "{{ chain_content.stdout_lines | join('\n') }}" - dest: "{{ certificate_authority_intermediate_ca_path }}/fullchain.pem" - owner: "root" - group: "root" - mode: "0644" - when: _stat_result.stat.exists is defined and - _stat_result.stat.exists + ansible.builtin.include_tasks: concatenate.yaml + vars: + _concat_sources: + - "{{ certificate_authority_intermediate_ca_path }}/cert.pem" + - "{{ certificate_authority_root_ca_path }}/chain.pem" + _concat_dest: "{{ certificate_authority_intermediate_ca_path }}/fullchain.pem" + _concat_mode: "0644" - name: Create file with private key and fullchain file of intermediate Certificate Authority (CA) - block: - - name: Check if private key exists - ansible.builtin.stat: - path: "{{ certificate_authority_intermediate_ca_path }}/privkey.pem" - register: _stat_result - - name: Concatenate private key and fullchain file of intermediate Certificate Authority (CA) - vars: - _chain_files: - - "{{ certificate_authority_intermediate_ca_path }}/privkey.pem" - - "{{ certificate_authority_intermediate_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_intermediate_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_intermediate_ca_path }}/privkey.pem" + - "{{ certificate_authority_intermediate_ca_path }}/fullchain.pem" + _concat_dest: "{{ certificate_authority_intermediate_ca_path }}/all.pem" + _concat_mode: "0600" diff --git a/tasks/root_certificate_authority.yaml b/tasks/root_certificate_authority.yaml index 5558fa4..06834ba 100644 --- a/tasks/root_certificate_authority.yaml +++ b/tasks/root_certificate_authority.yaml @@ -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: