2023-03-29 20:49:37 +00:00
|
|
|
- name: "Add host to {{ patroni_play_group }} group"
|
|
|
|
add_host:
|
|
|
|
groups: "{{ patroni_play_group }}"
|
|
|
|
hostname: "{{ hostvars[item]['inventory_hostname'] }}"
|
|
|
|
ansible_host: "{{ hostvars[item]['ansible_host'] | default(omit) }}"
|
|
|
|
loop: "{{ ansible_play_hosts }}"
|
|
|
|
when: groups[patroni_play_group] is not defined
|
|
|
|
changed_when: false
|
|
|
|
|
|
|
|
- name: Install python3-cryptography as dependence
|
|
|
|
package:
|
|
|
|
name: python3-cryptography
|
|
|
|
state: present
|
|
|
|
|
2023-02-07 23:39:20 +00:00
|
|
|
- name: Check if ssl dir exist
|
|
|
|
file:
|
|
|
|
name: "{{ patroni_ssl_path }}"
|
|
|
|
state: directory
|
|
|
|
owner: postgres
|
|
|
|
group: postgres
|
|
|
|
when: patroni_ssl
|
|
|
|
|
|
|
|
- name: Add predefined ssl cert for Patroni
|
|
|
|
copy:
|
|
|
|
src: "{{ patroni_cert_name }}"
|
|
|
|
dest: "{{ patroni_ssl_path }}/{{ patroni_cert_name }}"
|
|
|
|
owner: postgres
|
|
|
|
group: postgres
|
|
|
|
notify: Restart Patroni
|
|
|
|
when: patroni_cert is defined
|
|
|
|
|
|
|
|
- name: Generate OpenSSL key and cert for Patroni
|
2023-03-29 20:49:37 +00:00
|
|
|
when: "inventory_hostname == groups[patroni_play_group]|first"
|
2023-02-07 23:39:20 +00:00
|
|
|
block:
|
|
|
|
- name: Generate an OpenSSL private CA key with the default values (4096 bits, RSA)
|
|
|
|
community.crypto.openssl_privatekey:
|
|
|
|
path: "{{ patroni_ssl_path }}/CA-{{ patroni_self_signed_cert_name }}.key"
|
|
|
|
owner: postgres
|
|
|
|
group: postgres
|
|
|
|
when: patroni_cert is not defined
|
|
|
|
register: patroni_ca_key_gen
|
|
|
|
|
|
|
|
- name: Generate an OpenSSL Certificate Signing Request
|
|
|
|
community.crypto.openssl_csr:
|
|
|
|
path: "{{ patroni_ssl_path }}/CA-{{ patroni_self_signed_cert_name }}.csr"
|
|
|
|
privatekey_path: "{{ patroni_ssl_path }}/CA-{{ patroni_self_signed_cert_name }}.key"
|
|
|
|
use_common_name_for_san: false
|
|
|
|
basic_constraints:
|
|
|
|
- 'CA:TRUE'
|
|
|
|
basic_constraints_critical: yes
|
|
|
|
key_usage:
|
|
|
|
- keyCertSign
|
|
|
|
key_usage_critical: true
|
|
|
|
common_name: "CA-{{ patroni_self_signed_cert_name }}"
|
|
|
|
owner: postgres
|
|
|
|
group: postgres
|
|
|
|
register: patroni_ca_csr
|
|
|
|
|
|
|
|
- name: Generate a Self Signed OpenSSL CA certificate
|
|
|
|
community.crypto.x509_certificate:
|
|
|
|
path: "{{ patroni_ssl_path }}/CA-{{ patroni_self_signed_cert_name }}.crt"
|
|
|
|
csr_path: "{{ patroni_ssl_path }}/CA-{{ patroni_self_signed_cert_name }}.csr"
|
|
|
|
privatekey_path: "{{ patroni_ssl_path }}/CA-{{ patroni_self_signed_cert_name }}.key"
|
|
|
|
provider: selfsigned
|
|
|
|
owner: postgres
|
|
|
|
group: postgres
|
|
|
|
when: patroni_cert is not defined
|
|
|
|
register: patroni_ca_cert_gen
|
|
|
|
|
|
|
|
- name: Generate an OpenSSL private client key with the default values (4096 bits, RSA)
|
|
|
|
community.crypto.openssl_privatekey:
|
|
|
|
path: "{{ patroni_ssl_path }}/{{ patroni_self_signed_cert_name }}.key"
|
|
|
|
owner: postgres
|
|
|
|
group: postgres
|
|
|
|
when: patroni_cert is not defined
|
|
|
|
register: patroni_key_gen
|
|
|
|
|
2023-03-29 20:49:37 +00:00
|
|
|
- name: Generate Patroni subject_alt_ips from ansible_host
|
2023-02-07 23:39:20 +00:00
|
|
|
set_fact:
|
2023-03-29 20:49:37 +00:00
|
|
|
patroni_server_subject_alt_ips_from_ansible_host: "{{ groups[patroni_play_group] | default([]) | map('extract', hostvars, ['ansible_host']) | map('regex_replace', '^', 'IP:') | list }}"
|
|
|
|
when: hostvars[inventory_hostname]['ansible_host'] is defined
|
2023-02-07 23:39:20 +00:00
|
|
|
|
2023-03-29 20:49:37 +00:00
|
|
|
- name: Generate Patroni subject_alt_ips from default ipv4 address
|
2023-02-07 23:39:20 +00:00
|
|
|
set_fact:
|
2023-03-29 20:49:37 +00:00
|
|
|
patroni_server_subject_alt_ips: "{{ groups[patroni_play_group] | default([]) | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | map('regex_replace', '^', 'IP:') | list }}"
|
2023-09-26 13:51:20 +00:00
|
|
|
when:
|
|
|
|
- hostvars[inventory_hostname]['ansible_default_ipv4']['address'] is defined
|
|
|
|
- not patroni_cacert_multiple_default_gw_workaround
|
|
|
|
|
|
|
|
- name: Generate Patroni subject_alt_ips from ansible_all_ipv4_addresses
|
|
|
|
set_fact:
|
|
|
|
patroni_server_subject_alt_ips_all_ipv4: "{{ groups[patroni_play_group] | default([]) | map('extract', hostvars, ['ansible_all_ipv4_addresses']) | flatten | map('regex_replace', '^', 'IP:') | list }}"
|
|
|
|
when: hostvars[inventory_hostname]['ansible_all_ipv4_addresses'] is defined
|
2023-02-07 23:39:20 +00:00
|
|
|
|
2023-03-29 20:49:37 +00:00
|
|
|
- name: Generate Patroni subject_alt_names
|
|
|
|
set_fact:
|
|
|
|
patroni_server_subject_alt_names: "{{ groups[patroni_play_group] | default([]) | map('extract', hostvars, ['inventory_hostname']) | map('regex_replace', '^', 'DNS:') | list }}"
|
2023-02-07 23:39:20 +00:00
|
|
|
|
|
|
|
- name: Generate an OpenSSL Certificate Signing Request for client
|
|
|
|
community.crypto.openssl_csr:
|
|
|
|
path: "{{ patroni_ssl_path }}/{{ patroni_self_signed_cert_name }}.csr"
|
|
|
|
privatekey_path: "{{ patroni_ssl_path }}/{{ patroni_self_signed_cert_name }}.key"
|
|
|
|
common_name: "{{ patroni_self_signed_cert_name }}"
|
2023-09-26 13:51:20 +00:00
|
|
|
subject_alt_name: "{{ patroni_server_subject_alt_ips | default([]) + patroni_server_subject_alt_names | default([]) + patroni_agent_subject_alt_ips | default([]) + patroni_agent_subject_alt_names | default([]) + patroni_server_subject_alt_ips_from_ansible_host | default([]) + patroni_server_subject_alt_ips_all_ipv4 | default([]) + patroni_agent_subject_alt_ips_from_ansible_host | default([]) }}"
|
2023-02-07 23:39:20 +00:00
|
|
|
owner: postgres
|
|
|
|
group: postgres
|
|
|
|
register: patroni_csr
|
|
|
|
|
|
|
|
- name: Generate an OpenSSL certificate for client signed with your own CA certificate
|
|
|
|
community.crypto.x509_certificate:
|
|
|
|
path: "{{ patroni_ssl_path }}/{{ patroni_self_signed_cert_name }}.crt"
|
|
|
|
csr_path: "{{ patroni_ssl_path }}/{{ patroni_self_signed_cert_name }}.csr"
|
|
|
|
ownca_path: "{{ patroni_ssl_path }}/CA-{{ patroni_self_signed_cert_name }}.crt"
|
|
|
|
ownca_privatekey_path: "{{ patroni_ssl_path }}/CA-{{ patroni_self_signed_cert_name }}.key"
|
|
|
|
provider: ownca
|
|
|
|
owner: postgres
|
|
|
|
group: postgres
|
|
|
|
register: patroni_cert
|
|
|
|
|
|
|
|
- name: Get CA cert content
|
|
|
|
slurp:
|
|
|
|
src: "{{ patroni_ssl_path }}/CA-{{ patroni_self_signed_cert_name }}.crt"
|
|
|
|
register: patroni_ca_cert_b64
|
|
|
|
|
|
|
|
- name: Get CA key content
|
|
|
|
slurp:
|
|
|
|
src: "{{ patroni_ssl_path }}/CA-{{ patroni_self_signed_cert_name }}.key"
|
|
|
|
register: patroni_ca_key_b64
|
|
|
|
|
|
|
|
- name: Get client cert content
|
|
|
|
slurp:
|
|
|
|
src: "{{ patroni_ssl_path }}/{{ patroni_self_signed_cert_name }}.crt"
|
|
|
|
register: patroni_cert_b64
|
|
|
|
|
|
|
|
- name: Get client key content
|
|
|
|
slurp:
|
|
|
|
src: "{{ patroni_ssl_path }}/{{ patroni_self_signed_cert_name }}.key"
|
|
|
|
register: patroni_key_b64
|
|
|
|
|
|
|
|
- name: Set facts about key and cert
|
|
|
|
set_fact:
|
|
|
|
patroni_ca_key: "{{ patroni_ca_key_b64.content | b64decode }}"
|
|
|
|
patroni_ca_cert: "{{ patroni_ca_cert_b64.content | b64decode }}"
|
|
|
|
patroni_key: "{{ patroni_key_b64.content | b64decode }}"
|
|
|
|
patroni_cert: "{{ patroni_cert_b64.content | b64decode }}"
|
|
|
|
delegate_to: "{{ item }}"
|
|
|
|
delegate_facts: true
|
|
|
|
run_once: true
|
|
|
|
loop: "{{ groups.patroni }}"
|
|
|
|
|
|
|
|
- name: Put Patroni CA OpenSSL key
|
|
|
|
copy:
|
|
|
|
content: "{{ patroni_ca_key }}"
|
|
|
|
dest: "{{ patroni_ssl_path }}/CA-{{ patroni_self_signed_cert_name }}.key"
|
|
|
|
owner: postgres
|
|
|
|
group: postgres
|
|
|
|
mode: 0600
|
|
|
|
notify: Restart Patroni
|
|
|
|
|
|
|
|
- name: Put Patroni CA OpenSSL cert
|
|
|
|
copy:
|
|
|
|
content: "{{ patroni_ca_cert }}"
|
|
|
|
dest: "{{ patroni_ssl_path }}/CA-{{ patroni_self_signed_cert_name }}.crt"
|
|
|
|
owner: postgres
|
|
|
|
group: postgres
|
|
|
|
notify: Restart Patroni
|
|
|
|
|
|
|
|
- name: Put Patroni CA OpenSSL cert to PKI
|
|
|
|
copy:
|
|
|
|
content: "{{ patroni_ca_cert }}"
|
2023-03-29 20:49:37 +00:00
|
|
|
dest: "{{ patroni_ssl_ca_trust_dir }}/CA-{{ patroni_self_signed_cert_name }}.crt"
|
|
|
|
register: ca_trust
|
2023-02-07 23:39:20 +00:00
|
|
|
notify: Restart Patroni
|
|
|
|
|
|
|
|
- name: Update CA trust
|
2023-03-29 20:49:37 +00:00
|
|
|
shell: "{{ patroni_ssl_update_ca_command }}"
|
|
|
|
when: ca_trust.changed
|
2023-02-07 23:39:20 +00:00
|
|
|
|
|
|
|
- name: Put Patroni OpenSSL key
|
|
|
|
copy:
|
|
|
|
content: "{{ patroni_key }}"
|
|
|
|
dest: "{{ patroni_ssl_path }}/{{ patroni_self_signed_cert_name }}.key"
|
|
|
|
owner: postgres
|
|
|
|
group: postgres
|
|
|
|
mode: 0600
|
|
|
|
notify: Restart Patroni
|
|
|
|
|
|
|
|
- name: Put Patroni OpenSSL cert
|
|
|
|
copy:
|
|
|
|
content: "{{ patroni_cert }}"
|
|
|
|
dest: "{{ patroni_ssl_path }}/{{ patroni_self_signed_cert_name }}.crt"
|
|
|
|
owner: postgres
|
|
|
|
group: postgres
|
|
|
|
notify: Restart Patroni
|