Skip to content

Commit 6c78fa4

Browse files
authored
fix: various fixes for add_pgnode playbook (#1202)
1 parent 70b5ae2 commit 6c78fa4

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

automation/roles/pgbackrest/tasks/ssh_keys.yml

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,54 +73,68 @@
7373
ansible.posix.authorized_key:
7474
user: postgres
7575
state: present
76-
key: "{{ hostvars[item].pgbackrest_server_sshkey['content'] | b64decode }}"
76+
key: "{{ hostvars[item].get('pgbackrest_server_sshkey', {}).get('content', '') | b64decode }}"
7777
loop: "{{ groups['pgbackrest'] | default([]) }}"
78-
when: "'postgres_cluster' in group_names"
78+
when:
79+
- "'postgres_cluster' in group_names"
80+
- hostvars[item].get('pgbackrest_server_sshkey') is not none
81+
- hostvars[item].get('pgbackrest_server_sshkey', {}).get('content', '') | length > 0
7982

8083
- name: ssh_keys | Add database ssh keys in "~{{ pgbackrest_repo_user }}/.ssh/authorized_keys" on pgbackrest server
8184
ansible.posix.authorized_key:
8285
user: "{{ pgbackrest_repo_user }}"
8386
state: present
84-
key: "{{ hostvars[item].postgres_cluster_sshkey['content'] | b64decode }}"
87+
key: "{{ hostvars[item].get('postgres_cluster_sshkey', {}).get('content', '') | b64decode }}"
8588
loop: "{{ groups['postgres_cluster'] }}"
86-
when: "'pgbackrest' in group_names"
89+
when:
90+
- "'pgbackrest' in group_names"
91+
- hostvars[item].get('postgres_cluster_sshkey') is not none
92+
- hostvars[item].get('postgres_cluster_sshkey', {}).get('content', '') | length > 0
8793

8894
# if 'backup-standby' are specified in pgbackrest_conf.global
8995
- name: ssh_keys | Add ssh keys in "~postgres/.ssh/authorized_keys" on database servers
9096
ansible.posix.authorized_key:
9197
user: postgres
9298
state: present
93-
key: "{{ hostvars[item].postgres_cluster_sshkey['content'] | b64decode }}"
99+
key: "{{ hostvars[item].get('postgres_cluster_sshkey', {}).get('content', '') | b64decode }}"
94100
loop: "{{ groups['postgres_cluster'] }}"
95101
when:
96102
- "'postgres_cluster' in group_names"
97103
- pgbackrest_conf.global | selectattr('option', 'equalto', 'backup-standby') | map(attribute='value') | list | last | default('') == 'y'
104+
- hostvars[item].get('postgres_cluster_sshkey') is not none
105+
- hostvars[item].get('postgres_cluster_sshkey', {}).get('content', '') | length > 0
98106

99-
- name: known_hosts | Get public ssh keys of hosts (ssh-keyscan)
100-
ansible.builtin.command: >
101-
ssh-keyscan -trsa -p {{ hostvars[item].ansible_ssh_port | default(hostvars[item].ansible_port) | default(22) }} {{ hostvars[item]['bind_address'] }}
102-
loop: "{{ groups['postgres_cluster'] }}"
107+
- name: known_hosts | Get public ssh host keys (ssh-keyscan)
108+
ansible.builtin.command: ssh-keyscan -p {{ target_port }} {{ target_host }}
109+
loop: "{{ (groups['postgres_cluster']) + (groups['pgbackrest'] | default([])) }}"
110+
vars:
111+
target_host: "{{ hostvars[item].get('bind_address') if (item in (groups['postgres_cluster'])) else pgbackrest_repo_host }}"
112+
target_port: "{{ hostvars[item].get('ansible_ssh_port') or hostvars[item].get('ansible_port') or 22 }}"
103113
register: ssh_known_host_keyscan
104114
changed_when: false
105115

106-
- name: known_hosts | add ssh public keys in "~postgres/.ssh/known_hosts" on database servers
116+
- name: known_hosts | Add ssh host keys in "~postgres/.ssh/known_hosts" on database servers
107117
become: true
108118
become_user: postgres
109119
ansible.builtin.known_hosts:
110-
host: "{{ item.stdout.split(' ')[0] }}"
111-
key: "{{ item.stdout }}"
120+
host: "{{ item.stdout_lines | select('match', '^[^#].*') | first | split(' ') | first }}"
121+
key: "{{ item.stdout_lines | select('match', '^[^#].*') | first }}"
112122
path: "~postgres/.ssh/known_hosts"
113123
no_log: true
114124
loop: "{{ ssh_known_host_keyscan.results }}"
115-
when: "'postgres_cluster' in group_names"
125+
when:
126+
- "'postgres_cluster' in group_names"
127+
- item.stdout_lines | select('match', '^[^#].*') | list | length > 0
116128

117-
- name: known_hosts | add ssh public keys in "~{{ pgbackrest_repo_user }}/.ssh/known_hosts" on pgbackrest server
129+
- name: known_hosts | Add ssh host keys in "~{{ pgbackrest_repo_user }}/.ssh/known_hosts" on pgbackrest server
118130
become: true
119131
become_user: "{{ pgbackrest_repo_user }}"
120132
ansible.builtin.known_hosts:
121-
host: "{{ item.stdout.split(' ')[0] }}"
122-
key: "{{ item.stdout }}"
133+
host: "{{ item.stdout_lines | select('match', '^[^#].*') | first | split(' ') | first }}"
134+
key: "{{ item.stdout_lines | select('match', '^[^#].*') | first }}"
123135
path: "~{{ pgbackrest_repo_user }}/.ssh/known_hosts"
124136
no_log: true
125137
loop: "{{ ssh_known_host_keyscan.results }}"
126-
when: "'pgbackrest' in group_names"
138+
when:
139+
- "'pgbackrest' in group_names"
140+
- item.stdout_lines | select('match', '^[^#].*') | list | length > 0

automation/roles/pgbackrest/templates/pgbackrest.server.stanza.conf.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[{{ pgbackrest_stanza }}]
22
{% for host in groups['postgres_cluster'] %}
3-
pg{{ loop.index }}-host={{ host }}
3+
pg{{ loop.index }}-host={{ hostvars[host].bind_address }}
44
pg{{ loop.index }}-port={{ postgresql_port }}
55
pg{{ loop.index }}-socket-path={{ postgresql_unix_socket_dir }}
66
pg{{ loop.index }}-path={{ postgresql_data_dir }}

automation/roles/tls_certificate/tasks/copy.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
---
2-
- name: "Fetch TLS certificate, key and CA from {{ inventory_hostname }}"
2+
- name: "Fetch TLS certificate, key and CA from master node"
33
ansible.builtin.slurp:
44
src: "{{ fetch_tls_dir | default(tls_dir | default('/etc/tls')) }}/{{ item }}"
55
register: tls_files_raw
66
loop:
77
- "{{ fetch_tls_privatekey | default(tls_privatekey | default('server.key')) }}"
88
- "{{ fetch_tls_cert | default(tls_cert | default('server.crt')) }}"
99
- "{{ fetch_tls_ca_cert | default(tls_ca_cert | default('ca.crt')) }}"
10-
when: inventory_hostname == (groups[tls_group_name | default('master')][0])
10+
delegate_to: "{{ groups[tls_group_name | default('master')][0] }}"
11+
run_once: true
1112
tags: tls, tls_cert_copy
1213

1314
- name: "Set variable: tls_files"
@@ -16,7 +17,7 @@
1617
server_key: "{{ tls_files_raw.results[0].content }}"
1718
server_crt: "{{ tls_files_raw.results[1].content }}"
1819
ca_crt: "{{ tls_files_raw.results[2].content }}"
19-
when: inventory_hostname == (groups[tls_group_name | default('master')][0])
20+
run_once: true
2021
tags: tls, tls_cert_copy
2122

2223
- name: Create directory {{ copy_tls_dir | default(tls_dir | default('/etc/tls')) }}
@@ -30,7 +31,7 @@
3031

3132
- name: Copy TLS certificate, key and CA to all nodes
3233
ansible.builtin.copy:
33-
content: "{{ (hostvars[groups[tls_group_name | default('master')][0]].tls_files[item.key] | b64decode) }}"
34+
content: "{{ (tls_files[item.key] | b64decode) }}"
3435
dest: "{{ copy_tls_dir | default(tls_dir | default('/etc/tls')) }}/{{ item.filename }}"
3536
owner: "{{ copy_tls_owner | default(tls_owner | default('postgres')) }}"
3637
group: "{{ copy_tls_owner | default(tls_owner | default('postgres')) }}"

0 commit comments

Comments
 (0)