cacert/tasks/cn_certs.yaml

171 lines
6.6 KiB
YAML
Raw Normal View History

2022-09-06 11:31:23 +00:00
- name: CA and certs | Generate CN certs and keys
when: inventory_hostname in groups[cacert_ca_group]
block:
- name: Generate an OpenSSL private client key with the default values (4096 bits, RSA)
community.crypto.openssl_privatekey:
path: "{{ cacert_ssl_gen_path }}/{{ item.name }}.key"
when: cacert_cert is not defined
register: cacert_client_key_gen
- name: Generate subject_alt_ips
set_fact:
client_subject_alt_ips: "{{ item.subject_alt_ips | map('regex_replace', '^', 'IP:') | list }}"
when: item.subject_alt_ips is defined
- name: Generate subject_alt_names
set_fact:
client_subject_alt_names: "{{ item.subject_alt_names | map('regex_replace', '^', 'DNS:') | list }}"
when: item.subject_alt_names is defined
- name: Generate an OpenSSL Certificate Signing Request for client
community.crypto.openssl_csr:
path: "{{ cacert_ssl_gen_path }}/{{ item.name }}.csr"
privatekey_path: "{{ cacert_ssl_gen_path }}/{{ item.name }}.key"
common_name: "{{ item.name }}"
register: cacert_client_csr
when:
- item.subject_alt_names is not defined
- item.subject_alt_ips is not defined
- name: Generate an OpenSSL Certificate Signing Request for client with subject_alt_name
community.crypto.openssl_csr:
path: "{{ cacert_ssl_gen_path }}/{{ item.name }}.csr"
privatekey_path: "{{ cacert_ssl_gen_path }}/{{ item.name }}.key"
common_name: "{{ item.name }}"
subject_alt_name: "{{ ((client_subject_alt_ips | default([])) + (client_subject_alt_names | default([]) + (['DNS:' ~ item.name]))) }}"
register: cacert_client_csr
when: item.subject_alt_names is defined or item.subject_alt_ips is defined
- name: Generate an OpenSSL certificate for client signed with your own CA certificate
community.crypto.x509_certificate:
path: "{{ cacert_ssl_gen_path }}/{{ item.name }}.crt"
csr_path: "{{ cacert_ssl_gen_path }}/{{ item.name }}.csr"
ownca_path: "{{ cacert_ssl_gen_path }}/{{ cacert_ca_name }}.crt"
ownca_privatekey_path: "{{ cacert_ssl_gen_path }}/{{ cacert_ca_name }}.key"
provider: ownca
register: cacert_client_cert
- name: Get {{ item.name }} OpenSSL crt and key content
ansible.builtin.shell: |
cat {{ cacert_ssl_gen_path }}/{{ item.name }}.{crt,key}
register: concated_crt_key
changed_when: false
- name: Concatenate and save {{ item.name }} OpenSSL crt and key to single file
copy:
content: "{{ concated_crt_key.stdout }}"
dest: "{{ cacert_ssl_gen_path }}/{{ item.name }}.pem"
- name: Get CN key content
slurp:
src: "{{ cacert_ssl_gen_path }}/{{ item.name }}.key"
register: cacert_cn_certs_key_b64
- name: Get CN cert content
slurp:
src: "{{ cacert_ssl_gen_path }}/{{ item.name }}.crt"
register: cacert_cn_certs_cert_b64
- name: Get CN cert and key concat content
slurp:
src: "{{ cacert_ssl_gen_path }}/{{ item.name }}.pem"
register: cacert_cn_certs_concat_b64
- name: Set facts about key and cert
set_fact:
cacert_cn_certs_key: "{{ cacert_cn_certs_key_b64.content | b64decode }}"
cacert_cn_certs_cert: "{{ cacert_cn_certs_cert_b64.content | b64decode }}"
cacert_cn_certs_concat: "{{ cacert_cn_certs_concat_b64.content | b64decode }}"
delegate_to: "{{ fact_item }}"
delegate_facts: true
run_once: true
with_items:
- "{{ groups[cacert_ca_group] | default([]) }}"
- "{{ groups[cacert_clients_group] | default([]) }}"
loop_control:
loop_var: fact_item
- name: Distribute CN certificates
become: true
when: inventory_hostname in groups[cacert_clients_group]
block:
- name: CA and cert | Check if dest dir exist on remote hosts
file:
name: "{{ host_item.path }}"
state: directory
loop: "{{ item.hosts }}"
loop_control:
loop_var: host_item
when:
- inventory_hostname == host_item.host
- name: CA and cert | Check if dest dir exist on remote hosts for groups
file:
name: "{{ group_item.path }}"
state: directory
loop: "{{ item.groups }}"
loop_control:
loop_var: group_item
when:
- "inventory_hostname in (groups[group_item.group] | map('extract', hostvars, ['inventory_hostname']) | join(','))"
- name: Put CN key for host
copy:
content: "{{ cacert_cn_certs_key }}"
dest: "{{ host_item.path }}/{{ host_item.name | default(item.name) }}.key"
loop: "{{ item.hosts }}"
loop_control:
loop_var: host_item
when:
- inventory_hostname == host_item.host
- name: Put CN cert for host
copy:
content: "{{ cacert_cn_certs_cert }}"
dest: "{{ host_item.path }}/{{ host_item.name | default(item.name) }}.crt"
loop: "{{ item.hosts }}"
loop_control:
loop_var: host_item
when:
- inventory_hostname == host_item.host
- name: Put CN key and cert concat for host
copy:
content: "{{ cacert_cn_certs_concat }}"
dest: "{{ host_item.path }}/{{ host_item.name | default(item.name) }}.{{ host_item.concat | default('pem') }}"
loop: "{{ item.hosts }}"
loop_control:
loop_var: host_item
when:
- inventory_hostname == host_item.host
- name: Put CN key for group
copy:
content: "{{ cacert_cn_certs_key }}"
dest: "{{ group_item.path }}/{{ group_item.name | default(item.name) }}.key"
loop: "{{ item.groups }}"
loop_control:
loop_var: group_item
when:
- "inventory_hostname in (groups[group_item.group] | map('extract', hostvars, ['inventory_hostname']) | join(','))"
- name: Put CN cert for group
copy:
content: "{{ cacert_cn_certs_cert }}"
dest: "{{ group_item.path }}/{{ group_item.name | default(item.name) }}.crt"
loop: "{{ item.groups }}"
loop_control:
loop_var: group_item
when:
- "inventory_hostname in (groups[group_item.group] | map('extract', hostvars, ['inventory_hostname']) | join(','))"
- name: Put CN key and cert concat for group
copy:
content: "{{ cacert_cn_certs_concat }}"
dest: "{{ group_item.path }}/{{ group_item.name | default(item.name) }}.{{ group_item.concat | default('pem') }}"
loop: "{{ item.groups }}"
loop_control:
loop_var: group_item
when:
- "inventory_hostname in (groups[group_item.group] | map('extract', hostvars, ['inventory_hostname']) | join(','))"