|
73 | 73 | ansible.posix.authorized_key:
|
74 | 74 | user: postgres
|
75 | 75 | state: present
|
76 |
| - key: "{{ hostvars[item].pgbackrest_server_sshkey['content'] | b64decode }}" |
| 76 | + key: "{{ hostvars[item].get('pgbackrest_server_sshkey', {}).get('content', '') | b64decode }}" |
77 | 77 | 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 |
79 | 82 |
|
80 | 83 | - name: ssh_keys | Add database ssh keys in "~{{ pgbackrest_repo_user }}/.ssh/authorized_keys" on pgbackrest server
|
81 | 84 | ansible.posix.authorized_key:
|
82 | 85 | user: "{{ pgbackrest_repo_user }}"
|
83 | 86 | state: present
|
84 |
| - key: "{{ hostvars[item].postgres_cluster_sshkey['content'] | b64decode }}" |
| 87 | + key: "{{ hostvars[item].get('postgres_cluster_sshkey', {}).get('content', '') | b64decode }}" |
85 | 88 | 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 |
87 | 93 |
|
88 | 94 | # if 'backup-standby' are specified in pgbackrest_conf.global
|
89 | 95 | - name: ssh_keys | Add ssh keys in "~postgres/.ssh/authorized_keys" on database servers
|
90 | 96 | ansible.posix.authorized_key:
|
91 | 97 | user: postgres
|
92 | 98 | state: present
|
93 |
| - key: "{{ hostvars[item].postgres_cluster_sshkey['content'] | b64decode }}" |
| 99 | + key: "{{ hostvars[item].get('postgres_cluster_sshkey', {}).get('content', '') | b64decode }}" |
94 | 100 | loop: "{{ groups['postgres_cluster'] }}"
|
95 | 101 | when:
|
96 | 102 | - "'postgres_cluster' in group_names"
|
97 | 103 | - 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 |
98 | 106 |
|
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 }}" |
103 | 113 | register: ssh_known_host_keyscan
|
104 | 114 | changed_when: false
|
105 | 115 |
|
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 |
107 | 117 | become: true
|
108 | 118 | become_user: postgres
|
109 | 119 | 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 }}" |
112 | 122 | path: "~postgres/.ssh/known_hosts"
|
113 | 123 | no_log: true
|
114 | 124 | 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 |
116 | 128 |
|
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 |
118 | 130 | become: true
|
119 | 131 | become_user: "{{ pgbackrest_repo_user }}"
|
120 | 132 | 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 }}" |
123 | 135 | path: "~{{ pgbackrest_repo_user }}/.ssh/known_hosts"
|
124 | 136 | no_log: true
|
125 | 137 | 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 |
0 commit comments