Skip to content

Commit 53649d8

Browse files
committed
Code Refactoring: move tasks to roles
and create handlers. fix: [503] Tasks that run when changed should likely be handlers
1 parent 8904a05 commit 53649d8

File tree

67 files changed

+785
-803
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+785
-803
lines changed

.github/workflows/ansible-lint.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
override-deps: |
3030
ansible==2.9.6
3131
ansible-lint==4.2.0
32-
args: "-x 403,503"
32+
args: "-x 403"
3333

3434
- name: Lint playbook with Ansible v2.8
3535
uses: ansible/ansible-lint-action@master
@@ -41,7 +41,7 @@ jobs:
4141
override-deps: |
4242
ansible==2.8.10
4343
ansible-lint==4.2.0
44-
args: "-x 403,503"
44+
args: "-x 403"
4545

4646
- name: Lint playbook with Ansible v2.7
4747
uses: ansible/ansible-lint-action@master
@@ -53,6 +53,6 @@ jobs:
5353
override-deps: |
5454
ansible==2.7.16
5555
ansible-lint==4.2.0
56-
args: "-x 403,503"
56+
args: "-x 403"
5757

5858
...

add_balancer.yml

+21-13
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
add_balancer: true
1616

1717
pre_tasks:
18-
- import_tasks: tasks/check_system.yml
18+
- name: Checking Linux distribution
19+
fail:
20+
msg: "{{ ansible_distribution }} is not supported"
21+
when: ansible_distribution not in os_valid_distributions
1922

20-
- import_tasks: tasks/hostname.yml
21-
when: hostname is defined and hostname | length > 0
22-
tags: hostname
23+
- name: Checking version of OS Linux
24+
fail:
25+
msg: "{{ ansible_distribution_version }} of {{ ansible_distribution }} is not supported"
26+
when: ansible_distribution_version is version_compare(os_minimum_versions[ansible_distribution], '<')
2327

2428
- name: Make sure the gnupg package is present
2529
apt:
@@ -30,14 +34,14 @@
3034
when: ansible_os_family == "Debian" and installation_method == "repo"
3135
tags: add_repo, install_packages, install_postgres
3236

33-
- name: Firewall | build a firewall_ports_dynamic_var
37+
- name: Build a firewall_ports_dynamic_var
3438
set_fact:
3539
firewall_ports_dynamic_var: "{{ firewall_ports_dynamic_var |default([]) }} + {{ firewall_allowed_tcp_ports_for[item] }}"
3640
loop: "{{ hostvars[inventory_hostname].group_names }}"
3741
when: firewall_enabled_at_boot|bool
3842
tags: firewall
3943

40-
- name: Firewall | build a firewall_rules_dynamic_var
44+
- name: Build a firewall_rules_dynamic_var
4145
set_fact:
4246
firewall_rules_dynamic_var: "{{ firewall_rules_dynamic_var |default([]) }} + {{ firewall_additional_rules_for[item] }}"
4347
loop: "{{ hostvars[inventory_hostname].group_names }}"
@@ -53,14 +57,18 @@
5357
when: firewall_enabled_at_boot|bool
5458
tags: firewall
5559

56-
tasks:
57-
- meta: flush_handlers
60+
- role: hostname
61+
- role: sysctl
5862

59-
- import_tasks: tasks/sysctl.yml
60-
tags: sysctl, kernel
61-
62-
- import_tasks: tasks/haproxy.yml
63+
- role: haproxy
6364
when: with_haproxy_load_balancing == "true"
64-
tags: load_balancing
65+
66+
- role: confd
67+
when: dcs_type == "etcd" and
68+
with_haproxy_load_balancing == "true"
69+
70+
- role: keepalived
71+
when: cluster_vip is defined and cluster_vip | length > 0 and
72+
with_haproxy_load_balancing == "true"
6573

6674
...

add_pgnode.yml

+29-32
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
existing_pgcluster: true
1616

1717
pre_tasks:
18-
- import_tasks: tasks/check_system.yml
18+
- name: Checking Linux distribution
19+
fail:
20+
msg: "{{ ansible_distribution }} is not supported"
21+
when: ansible_distribution not in os_valid_distributions
1922

20-
- import_tasks: tasks/hostname.yml
21-
when: hostname is defined and hostname | length > 0
22-
tags: hostname
23+
- name: Checking version of OS Linux
24+
fail:
25+
msg: "{{ ansible_distribution_version }} of {{ ansible_distribution }} is not supported"
26+
when: ansible_distribution_version is version_compare(os_minimum_versions[ansible_distribution], '<')
2327

2428
- name: Make sure the gnupg package is present
2529
apt:
@@ -30,14 +34,14 @@
3034
when: ansible_os_family == "Debian" and installation_method == "repo"
3135
tags: add_repo, install_packages, install_postgres
3236

33-
- name: Firewall | build a firewall_ports_dynamic_var
37+
- name: Build a firewall_ports_dynamic_var
3438
set_fact:
3539
firewall_ports_dynamic_var: "{{ firewall_ports_dynamic_var |default([]) }} + {{ firewall_allowed_tcp_ports_for[item] }}"
3640
loop: "{{ hostvars[inventory_hostname].group_names }}"
3741
when: firewall_enabled_at_boot|bool
3842
tags: firewall
3943

40-
- name: Firewall | build a firewall_rules_dynamic_var
44+
- name: Build a firewall_rules_dynamic_var
4145
set_fact:
4246
firewall_rules_dynamic_var: "{{ firewall_rules_dynamic_var |default([]) }} + {{ firewall_additional_rules_for[item] }}"
4347
loop: "{{ hostvars[inventory_hostname].group_names }}"
@@ -53,39 +57,32 @@
5357
when: firewall_enabled_at_boot|bool
5458
tags: firewall
5559

56-
tasks:
57-
- meta: flush_handlers
58-
59-
- import_tasks: tasks/add-repository.yml
60-
tags: add_repo
61-
62-
- import_tasks: tasks/packages.yml
63-
tags: install_packages
64-
65-
- import_tasks: tasks/sudo.yml
66-
tags: sudo, postgres_sudo
67-
68-
- import_tasks: tasks/configure.yml
69-
tags: configure
60+
- role: hostname
61+
- role: add-repository
62+
- role: packages
63+
- role: sudo
64+
- role: sysctl
65+
- role: transparent_huge_pages
66+
- role: pam_limits
67+
- role: io-scheduler
68+
- role: locales
69+
- role: timezone
70+
- role: ntp
71+
- role: ssh-keys
72+
73+
- role: wal-g
74+
when: wal_g_install == "true"
7075

71-
- import_tasks: tasks/pgbouncer.yml
76+
- role: pgbouncer
7277
when: install_pgbouncer == "true"
73-
tags: pgbouncer
74-
75-
- import_tasks: tasks/wal-g.yml
76-
when: wal_g_install == "true"
77-
tags: wal-g
7878

79-
- import_tasks: tasks/patroni.yml
80-
tags: patroni
79+
- role: patroni
8180

82-
- import_tasks: tasks/vip-manager.yml
81+
- role: vip-manager
8382
when: with_haproxy_load_balancing != "true" and
8483
(cluster_vip is defined and cluster_vip | length > 0)
85-
tags: vip, vip_manager
8684

8785
# finish (info)
88-
- import_tasks: tasks/deploy_finish.yml
89-
tags: cluster_info, cluster_status
86+
- role: deploy-finish
9087

9188
...

deploy_pgcluster.yml

+52-55
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
- "vars/{{ ansible_os_family }}.yml"
1414

1515
pre_tasks:
16-
- import_tasks: tasks/check_system.yml
16+
- name: Checking Linux distribution
17+
fail:
18+
msg: "{{ ansible_distribution }} is not supported"
19+
when: ansible_distribution not in os_valid_distributions
1720

18-
- import_tasks: tasks/hostname.yml
19-
when: hostname is defined and hostname | length > 0
20-
tags: hostname
21+
- name: Checking version of OS Linux
22+
fail:
23+
msg: "{{ ansible_distribution_version }} of {{ ansible_distribution }} is not supported"
24+
when: ansible_distribution_version is version_compare(os_minimum_versions[ansible_distribution], '<')
2125

2226
- name: Make sure the gnupg package is present
2327
apt:
@@ -28,14 +32,14 @@
2832
when: ansible_os_family == "Debian" and installation_method == "repo"
2933
tags: add_repo, install_packages, install_postgres
3034

31-
- name: Firewall | build a firewall_ports_dynamic_var
35+
- name: Build a firewall_ports_dynamic_var
3236
set_fact:
3337
firewall_ports_dynamic_var: "{{ firewall_ports_dynamic_var |default([]) }} + {{ firewall_allowed_tcp_ports_for[item] }}"
3438
loop: "{{ hostvars[inventory_hostname].group_names }}"
3539
when: firewall_enabled_at_boot|bool
3640
tags: firewall
3741

38-
- name: Firewall | build a firewall_rules_dynamic_var
42+
- name: Build a firewall_rules_dynamic_var
3943
set_fact:
4044
firewall_rules_dynamic_var: "{{ firewall_rules_dynamic_var |default([]) }} + {{ firewall_additional_rules_for[item] }}"
4145
loop: "{{ hostvars[inventory_hostname].group_names }}"
@@ -62,25 +66,17 @@
6266
- vars/system.yml
6367
- "vars/{{ ansible_os_family }}.yml"
6468

65-
tasks:
69+
pre_tasks:
6670
- meta: flush_handlers
6771

68-
- import_tasks: tasks/sysctl.yml
69-
tags: sysctl, kernel
70-
71-
- import_tasks: tasks/timezone.yml
72-
when: timezone is defined and timezone | length > 0
73-
ignore_errors: true
74-
tags: timezone, configure
75-
76-
- import_tasks: tasks/ntp.yml
77-
when: ntp_enabled is defined and ntp_enabled == "true"
78-
ignore_errors: true
79-
tags: ntp, configure
72+
roles:
73+
- role: hostname
74+
- role: sysctl
75+
- role: timezone
76+
- role: ntp
8077

81-
- import_tasks: tasks/etcd.yml
78+
- role: etcd
8279
when: dcs_exists == "false" and dcs_type == "etcd"
83-
tags: etcd, etcd_cluster
8480

8581

8682
- hosts: balancers
@@ -93,15 +89,23 @@
9389
- vars/system.yml
9490
- "vars/{{ ansible_os_family }}.yml"
9591

96-
tasks:
92+
pre_tasks:
9793
- meta: flush_handlers
9894

99-
- import_tasks: tasks/sysctl.yml
100-
tags: sysctl, kernel
95+
roles:
96+
- role: hostname
97+
- role: sysctl
10198

102-
- import_tasks: tasks/haproxy.yml
99+
- role: haproxy
103100
when: with_haproxy_load_balancing == "true"
104-
tags: load_balancing
101+
102+
- role: confd
103+
when: dcs_type == "etcd" and
104+
with_haproxy_load_balancing == "true"
105+
106+
- role: keepalived
107+
when: cluster_vip is defined and cluster_vip | length > 0 and
108+
with_haproxy_load_balancing == "true"
105109

106110

107111
- hosts: postgres_cluster
@@ -117,50 +121,43 @@
117121
pre_tasks:
118122
- meta: flush_handlers
119123

120-
- import_tasks: tasks/add-repository.yml
121-
tags: add_repo
122-
123-
- import_tasks: tasks/packages.yml
124-
tags: install_packages
125-
126-
- import_tasks: tasks/sudo.yml
127-
tags: sudo, postgres_sudo
128-
129-
- import_tasks: tasks/configure.yml
130-
tags: configure
131-
132-
- import_tasks: tasks/wal-g.yml
124+
roles:
125+
- role: hostname
126+
- role: add-repository
127+
- role: packages
128+
- role: sudo
129+
- role: sysctl
130+
- role: transparent_huge_pages
131+
- role: pam_limits
132+
- role: io-scheduler
133+
- role: locales
134+
- role: timezone
135+
- role: ntp
136+
- role: ssh-keys
137+
138+
- role: wal-g
133139
when: wal_g_install == "true"
134-
tags: wal-g
135140

136-
tasks:
137-
- import_tasks: tasks/pgbouncer.yml
141+
- role: pgbouncer
138142
when: install_pgbouncer == "true"
139-
tags: pgbouncer
140143

141-
- import_tasks: tasks/patroni.yml
142-
tags: patroni
144+
- role: patroni
143145

144-
- import_tasks: tasks/vip-manager.yml
146+
- role: vip-manager
145147
when: with_haproxy_load_balancing != "true" and
146148
(cluster_vip is defined and cluster_vip | length > 0)
147-
tags: vip, vip_manager
148149

149150
# optional
150-
- import_tasks: tasks/postgresql-users.yml
151+
- role: postgresql-users
151152
when: is_master == "true" and postgresql_users | length > 0
152-
tags: postgresql_users
153153

154-
- import_tasks: tasks/postgresql-databases.yml
154+
- role: postgresql-databases
155155
when: is_master == "true" and postgresql_databases | length > 0
156-
tags: postgresql_databases
157156

158-
- import_tasks: tasks/postgresql-extensions.yml
157+
- role: postgresql-extensions
159158
when: is_master == "true" and postgresql_extensions | length > 0
160-
tags: postgresql_extensions
161159

162160
# finish (info)
163-
- import_tasks: tasks/deploy_finish.yml
164-
tags: cluster_info, cluster_status
161+
- role: deploy-finish
165162

166163
...

0 commit comments

Comments
 (0)