mirror of
https://gitea.0xace.cc/ansible-galaxy/postgresql.git
synced 2024-11-25 08:16:43 +00:00
189 lines
7.6 KiB
YAML
189 lines
7.6 KiB
YAML
- name: "Add host to {{ postgresql_play_group }} group"
|
|
add_host:
|
|
groups: "{{ postgresql_play_group }}"
|
|
hostname: "{{ hostvars[item]['inventory_hostname'] }}"
|
|
ansible_host: "{{ hostvars[item]['ansible_host'] | default(omit) }}"
|
|
loop: "{{ ansible_play_hosts }}"
|
|
when: groups[postgresql_play_group] is not defined
|
|
changed_when: false
|
|
|
|
- name: Install python3-cryptography as dependence
|
|
dnf:
|
|
name: python3-cryptography
|
|
state: present
|
|
when: ansible_facts['os_family'] == 'RedHat'
|
|
|
|
- name: Check if ssl dir exist
|
|
file:
|
|
name: "{{ postgresql_ssl_path }}"
|
|
state: directory
|
|
owner: postgres
|
|
group: postgres
|
|
when: postgresql_ssl
|
|
|
|
- name: Add predefined ssl cert for PostgreSQL
|
|
copy:
|
|
src: "{{ postgresql_cert_name }}"
|
|
dest: "{{ postgresql_ssl_path }}/{{ postgresql_cert_name }}"
|
|
owner: postgres
|
|
group: postgres
|
|
notify: Restart PostgreSQL
|
|
when: postgresql_cert is defined
|
|
|
|
- name: Generate OpenSSL key and cert for PostgreSQL
|
|
when: "inventory_hostname == groups[postgresql_play_group]|first"
|
|
block:
|
|
- name: Generate an OpenSSL private CA key with the default values (4096 bits, RSA)
|
|
community.crypto.openssl_privatekey:
|
|
path: "{{ postgresql_ssl_path }}/CA-{{ postgresql_self_signed_cert_name }}.key"
|
|
owner: postgres
|
|
group: postgres
|
|
when: postgresql_cert is not defined
|
|
register: postgresql_ca_key_gen
|
|
|
|
- name: Generate an OpenSSL Certificate Signing Request
|
|
community.crypto.openssl_csr:
|
|
path: "{{ postgresql_ssl_path }}/CA-{{ postgresql_self_signed_cert_name }}.csr"
|
|
privatekey_path: "{{ postgresql_ssl_path }}/CA-{{ postgresql_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-{{ postgresql_self_signed_cert_name }}"
|
|
owner: postgres
|
|
group: postgres
|
|
register: postgresql_ca_csr
|
|
|
|
- name: Generate a Self Signed OpenSSL CA certificate
|
|
community.crypto.x509_certificate:
|
|
path: "{{ postgresql_ssl_path }}/CA-{{ postgresql_self_signed_cert_name }}.crt"
|
|
csr_path: "{{ postgresql_ssl_path }}/CA-{{ postgresql_self_signed_cert_name }}.csr"
|
|
privatekey_path: "{{ postgresql_ssl_path }}/CA-{{ postgresql_self_signed_cert_name }}.key"
|
|
provider: selfsigned
|
|
owner: postgres
|
|
group: postgres
|
|
when: postgresql_cert is not defined
|
|
register: postgresql_ca_cert_gen
|
|
|
|
- name: Generate an OpenSSL private client key with the default values (4096 bits, RSA)
|
|
community.crypto.openssl_privatekey:
|
|
path: "{{ postgresql_ssl_path }}/{{ postgresql_self_signed_cert_name }}.key"
|
|
owner: postgres
|
|
group: postgres
|
|
when: postgresql_cert is not defined
|
|
register: postgresql_key_gen
|
|
|
|
- name: Generate PostgreSQL subject_alt_ips from ansible_host
|
|
set_fact:
|
|
postgresql_server_subject_alt_ips_from_ansible_host: "{{ groups[postgresql_play_group] | default([]) | map('extract', hostvars, ['ansible_host']) | map('regex_replace', '^', 'IP:') | list }}"
|
|
when: hostvars[inventory_hostname]['ansible_host'] is defined
|
|
|
|
- name: Generate PostgreSQL subject_alt_ips from default ipv4 address
|
|
set_fact:
|
|
postgresql_server_subject_alt_ips: "{{ groups[postgresql_play_group] | default([]) | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | map('regex_replace', '^', 'IP:') | list }}"
|
|
when: hostvars[inventory_hostname]['ansible_default_ipv4']['address'] is defined
|
|
|
|
- name: Generate PostgreSQL subject_alt_names
|
|
set_fact:
|
|
postgresql_server_subject_alt_names: "{{ groups[postgresql_play_group] | default([]) | map('extract', hostvars, ['inventory_hostname']) | map('regex_replace', '^', 'DNS:') | list }}"
|
|
|
|
- name: Generate an OpenSSL Certificate Signing Request for client
|
|
community.crypto.openssl_csr:
|
|
path: "{{ postgresql_ssl_path }}/{{ postgresql_self_signed_cert_name }}.csr"
|
|
privatekey_path: "{{ postgresql_ssl_path }}/{{ postgresql_self_signed_cert_name }}.key"
|
|
common_name: "{{ postgresql_self_signed_cert_name }}"
|
|
subject_alt_name: "{{ postgresql_server_subject_alt_ips | default([]) + postgresql_server_subject_alt_names | default([]) + postgresql_agent_subject_alt_ips | default([]) + postgresql_agent_subject_alt_names | default([]) + postgresql_server_subject_alt_ips_from_ansible_host | default([]) + postgresql_agent_subject_alt_ips_from_ansible_host | default([]) }}"
|
|
owner: postgres
|
|
group: postgres
|
|
register: postgresql_csr
|
|
|
|
- name: Generate an OpenSSL certificate for client signed with your own CA certificate
|
|
community.crypto.x509_certificate:
|
|
path: "{{ postgresql_ssl_path }}/{{ postgresql_self_signed_cert_name }}.crt"
|
|
csr_path: "{{ postgresql_ssl_path }}/{{ postgresql_self_signed_cert_name }}.csr"
|
|
ownca_path: "{{ postgresql_ssl_path }}/CA-{{ postgresql_self_signed_cert_name }}.crt"
|
|
ownca_privatekey_path: "{{ postgresql_ssl_path }}/CA-{{ postgresql_self_signed_cert_name }}.key"
|
|
provider: ownca
|
|
owner: postgres
|
|
group: postgres
|
|
register: postgresql_cert
|
|
|
|
- name: Get CA cert content
|
|
slurp:
|
|
src: "{{ postgresql_ssl_path }}/CA-{{ postgresql_self_signed_cert_name }}.crt"
|
|
register: postgresql_ca_cert_b64
|
|
|
|
- name: Get CA key content
|
|
slurp:
|
|
src: "{{ postgresql_ssl_path }}/CA-{{ postgresql_self_signed_cert_name }}.key"
|
|
register: postgresql_ca_key_b64
|
|
|
|
- name: Get client cert content
|
|
slurp:
|
|
src: "{{ postgresql_ssl_path }}/{{ postgresql_self_signed_cert_name }}.crt"
|
|
register: postgresql_cert_b64
|
|
|
|
- name: Get client key content
|
|
slurp:
|
|
src: "{{ postgresql_ssl_path }}/{{ postgresql_self_signed_cert_name }}.key"
|
|
register: postgresql_key_b64
|
|
|
|
- name: Set facts about key and cert
|
|
set_fact:
|
|
postgresql_ca_key: "{{ postgresql_ca_key_b64.content | b64decode }}"
|
|
postgresql_ca_cert: "{{ postgresql_ca_cert_b64.content | b64decode }}"
|
|
postgresql_key: "{{ postgresql_key_b64.content | b64decode }}"
|
|
postgresql_cert: "{{ postgresql_cert_b64.content | b64decode }}"
|
|
delegate_to: "{{ item }}"
|
|
delegate_facts: true
|
|
run_once: true
|
|
loop: "{{ groups.postgresql }}"
|
|
|
|
- name: Put PostgreSQL CA OpenSSL key
|
|
copy:
|
|
content: "{{ postgresql_ca_key }}"
|
|
dest: "{{ postgresql_ssl_path }}/CA-{{ postgresql_self_signed_cert_name }}.key"
|
|
owner: postgres
|
|
group: postgres
|
|
mode: 0600
|
|
notify: Restart PostgreSQL
|
|
|
|
- name: Put PostgreSQL CA OpenSSL cert
|
|
copy:
|
|
content: "{{ postgresql_ca_cert }}"
|
|
dest: "{{ postgresql_ssl_path }}/CA-{{ postgresql_self_signed_cert_name }}.crt"
|
|
owner: postgres
|
|
group: postgres
|
|
notify: Restart PostgreSQL
|
|
|
|
- name: Put PostgreSQL CA OpenSSL cert to PKI
|
|
copy:
|
|
content: "{{ postgresql_ca_cert }}"
|
|
dest: "{{ postgresql_ssl_ca_trust_dir }}/CA-{{ postgresql_self_signed_cert_name }}.crt"
|
|
register: ca_trust
|
|
notify: Restart PostgreSQL
|
|
|
|
- name: Update CA trust
|
|
shell: "{{ postgresql_ssl_update_ca_command }}"
|
|
when: ca_trust.changed
|
|
|
|
- name: Put PostgreSQL OpenSSL key
|
|
copy:
|
|
content: "{{ postgresql_key }}"
|
|
dest: "{{ postgresql_ssl_path }}/{{ postgresql_self_signed_cert_name }}.key"
|
|
owner: postgres
|
|
group: postgres
|
|
mode: 0600
|
|
notify: Restart PostgreSQL
|
|
|
|
- name: Put PostgreSQL OpenSSL cert
|
|
copy:
|
|
content: "{{ postgresql_cert }}"
|
|
dest: "{{ postgresql_ssl_path }}/{{ postgresql_self_signed_cert_name }}.crt"
|
|
owner: postgres
|
|
group: postgres
|
|
notify: Restart PostgreSQL
|