Skip to content

Commit 9cb997c

Browse files
committed
Code Refactoring: add booleans.
1 parent 53649d8 commit 9cb997c

File tree

26 files changed

+108
-111
lines changed

26 files changed

+108
-111
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ You have two options available for deployment ("Type A" and "Type B"):
1818
### [Type A] PostgreSQL High-Availability with Load Balancing
1919
![TypeA](https://github.com/vitabaks/postgresql_cluster/blob/master/TypeA.png)
2020

21-
> To use this scheme, specify `with_haproxy_load_balancing: 'true'` in variable file vars/main.yml
21+
> To use this scheme, specify `with_haproxy_load_balancing: true` in variable file vars/main.yml
2222
2323
This scheme provides the ability to distribute the load on reading. This also allows us to scale out the cluster (with read-only replicas).
2424

@@ -234,7 +234,7 @@ See firewall_allowed_tcp_ports_for.balancers variable in the system.yml file.
234234

235235
3. Edit the `main.yml` variable file
236236

237-
Specify `with_haproxy_load_balancing: 'true'`
237+
Specify `with_haproxy_load_balancing: true`
238238

239239
4. Run playbook:
240240

add_balancer.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@
6161
- role: sysctl
6262

6363
- role: haproxy
64-
when: with_haproxy_load_balancing == "true"
64+
when: with_haproxy_load_balancing|bool
6565

6666
- role: confd
6767
when: dcs_type == "etcd" and
68-
with_haproxy_load_balancing == "true"
68+
with_haproxy_load_balancing|bool
6969

7070
- role: keepalived
7171
when: cluster_vip is defined and cluster_vip | length > 0 and
72-
with_haproxy_load_balancing == "true"
72+
with_haproxy_load_balancing|bool
7373

7474
...

add_pgnode.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@
7171
- role: ssh-keys
7272

7373
- role: wal-g
74-
when: wal_g_install == "true"
74+
when: wal_g_install|bool
7575

7676
- role: pgbouncer
77-
when: install_pgbouncer == "true"
77+
when: install_pgbouncer|bool
7878

7979
- role: patroni
8080

8181
- role: vip-manager
82-
when: with_haproxy_load_balancing != "true" and
82+
when: not with_haproxy_load_balancing|bool and
8383
(cluster_vip is defined and cluster_vip | length > 0)
8484

8585
# finish (info)

deploy_pgcluster.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
- role: ntp
7777

7878
- role: etcd
79-
when: dcs_exists == "false" and dcs_type == "etcd"
79+
when: not dcs_exists|bool and dcs_type == "etcd"
8080

8181

8282
- hosts: balancers
@@ -97,15 +97,15 @@
9797
- role: sysctl
9898

9999
- role: haproxy
100-
when: with_haproxy_load_balancing == "true"
100+
when: with_haproxy_load_balancing|bool
101101

102102
- role: confd
103103
when: dcs_type == "etcd" and
104-
with_haproxy_load_balancing == "true"
104+
with_haproxy_load_balancing|bool
105105

106106
- role: keepalived
107107
when: cluster_vip is defined and cluster_vip | length > 0 and
108-
with_haproxy_load_balancing == "true"
108+
with_haproxy_load_balancing|bool
109109

110110

111111
- hosts: postgres_cluster
@@ -136,15 +136,15 @@
136136
- role: ssh-keys
137137

138138
- role: wal-g
139-
when: wal_g_install == "true"
139+
when: wal_g_install|bool
140140

141141
- role: pgbouncer
142-
when: install_pgbouncer == "true"
142+
when: install_pgbouncer|bool
143143

144144
- role: patroni
145145

146146
- role: vip-manager
147-
when: with_haproxy_load_balancing != "true" and
147+
when: not with_haproxy_load_balancing|bool and
148148
(cluster_vip is defined and cluster_vip | length > 0)
149149

150150
# optional

inventory

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
# "postgresql_exists='true'" if PostgreSQL is already exists and runing
66
# "hostname=" variable is optional (used to change the server name)
77

8-
# (if dcs_exists: 'false' and dcs_type: 'etcd')
8+
# (if dcs_exists: false and dcs_type: "etcd")
99
[etcd_cluster] # recommendation: 3 or 5-7 nodes
1010
10.128.64.140
1111
10.128.64.142
1212
10.128.64.143
1313

1414

15-
# (if with_haproxy_load_balancing: 'true')
15+
# (if with_haproxy_load_balancing: true)
1616
[balancers]
1717
10.128.64.140
1818
10.128.64.142

roles/add-repository/tasks/main.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@
3838
dest: /tmp/
3939
timeout: 30
4040
validate_certs: false
41-
when: install_epel_repo == "true"
41+
when: install_epel_repo|bool
4242
tags: install_epel_repo
4343

4444
- name: Install EPEL repository
4545
package:
4646
name: "/tmp/epel-release-latest-{{ ansible_distribution_major_version }}.noarch.rpm"
4747
state: present
48-
when: install_epel_repo == "true"
48+
when: install_epel_repo|bool
4949
tags: install_epel_repo
5050

5151
# Install PostgreSQL Repository
@@ -55,14 +55,14 @@
5555
dest: /tmp/
5656
timeout: 30
5757
validate_certs: false
58-
when: install_postgresql_repo == "true"
58+
when: install_postgresql_repo|bool
5959
tags: install_postgresql_repo
6060

6161
- name: Install PostgreSQL repository
6262
package:
6363
name: /tmp/pgdg-redhat-repo-latest.noarch.rpm
6464
state: present
65-
when: install_postgresql_repo == "true"
65+
when: install_postgresql_repo|bool
6666
tags: install_postgresql_repo
6767
environment: "{{ proxy_env | default({}) }}"
6868
when: installation_method == "repo" and ansible_os_family == "RedHat"

roles/confd/templates/confd.toml.j2

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ backend = "etcd"
22
interval = 10
33
watch = true
44
nodes = [
5-
{% if dcs_exists == 'false' and dcs_type == 'etcd' %}
5+
{% if not dcs_exists|bool and dcs_type == 'etcd' %}
66
{% for host in groups['etcd_cluster'] %}
77
"http://{{ hostvars[host]['inventory_hostname'] }}:2379",
88
{% endfor %}
99
{% endif %}
10-
{% if dcs_exists == 'true' and dcs_type == 'etcd' %}
10+
{% if dcs_exists|bool and dcs_type == 'etcd' %}
1111
{% for etcd_hosts in patroni_etcd_hosts %}
1212
{{etcd_hosts.host}}:{{etcd_hosts.port}},
1313
{% endfor %}

roles/confd/templates/haproxy.tmpl.j2

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ listen master
3636
option httpchk OPTIONS /master
3737
http-check expect status 200
3838
default-server inter 3s fastinter 1s fall 3 rise 4 on-marked-down shutdown-sessions
39-
{% if install_pgbouncer == "true" %}
39+
{% if install_pgbouncer|bool %}
4040
{% raw %}{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split (index (split $data.conn_url ":") 1) "/") 2) "@" "/" -1)}}:{% endraw %}{{ pgbouncer_listen_port }}{% raw %} check port {{index (split (index (split $data.api_url "/") 2) ":") 1}}
4141
{{end}}{% endraw %}
4242
{% endif %}
43-
{% if ( install_pgbouncer is not defined ) or ( install_pgbouncer != "true" ) or ( install_pgbouncer | length < 0 ) %}
43+
{% if ( install_pgbouncer is not defined ) or ( not install_pgbouncer|bool ) %}
4444
{% raw %}{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split $data.conn_url "/") 2) "@" "/" -1)}} check port {{index (split (index (split $data.api_url "/") 2) ":") 1}}
4545
{{end}}{% endraw %}
4646
{% endif %}
@@ -58,11 +58,11 @@ listen replicas
5858
balance roundrobin
5959
http-check expect status 200
6060
default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions
61-
{% if install_pgbouncer == "true" %}
61+
{% if install_pgbouncer|bool %}
6262
{% raw %}{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split (index (split $data.conn_url ":") 1) "/") 2) "@" "/" -1)}}:{% endraw %}{{ pgbouncer_listen_port }}{% raw %} check port {{index (split (index (split $data.api_url "/") 2) ":") 1}}
6363
{{end}}{% endraw %}
6464
{% endif %}
65-
{% if ( install_pgbouncer is not defined ) or ( install_pgbouncer != "true" ) or ( install_pgbouncer | length < 0 ) %}
65+
{% if ( install_pgbouncer is not defined ) or ( not install_pgbouncer|bool ) %}
6666
{% raw %}{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split $data.conn_url "/") 2) "@" "/" -1)}} check port {{index (split (index (split $data.api_url "/") 2) ":") 1}}
6767
{{end}}{% endraw %}
6868
{% endif %}
@@ -80,11 +80,11 @@ listen replicas_sync
8080
balance roundrobin
8181
http-check expect status 200
8282
default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions
83-
{% if install_pgbouncer == "true" %}
83+
{% if install_pgbouncer|bool %}
8484
{% raw %}{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split (index (split $data.conn_url ":") 1) "/") 2) "@" "/" -1)}}:{% endraw %}{{ pgbouncer_listen_port }}{% raw %} check port {{index (split (index (split $data.api_url "/") 2) ":") 1}}
8585
{{end}}{% endraw %}
8686
{% endif %}
87-
{% if ( install_pgbouncer is not defined ) or ( install_pgbouncer != "true" ) or ( install_pgbouncer | length < 0 ) %}
87+
{% if ( install_pgbouncer is not defined ) or ( not install_pgbouncer|bool ) %}
8888
{% raw %}{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split $data.conn_url "/") 2) "@" "/" -1)}} check port {{index (split (index (split $data.api_url "/") 2) ":") 1}}
8989
{{end}}{% endraw %}
9090
{% endif %}
@@ -102,11 +102,11 @@ listen replicas_async
102102
balance roundrobin
103103
http-check expect status 200
104104
default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions
105-
{% if install_pgbouncer == "true" %}
105+
{% if install_pgbouncer|bool %}
106106
{% raw %}{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split (index (split $data.conn_url ":") 1) "/") 2) "@" "/" -1)}}:{% endraw %}{{ pgbouncer_listen_port }}{% raw %} check port {{index (split (index (split $data.api_url "/") 2) ":") 1}}
107107
{{end}}{% endraw %}
108108
{% endif %}
109-
{% if ( install_pgbouncer is not defined ) or ( install_pgbouncer != "true" ) or ( install_pgbouncer | length < 0 ) %}
109+
{% if ( install_pgbouncer is not defined ) or ( not install_pgbouncer|bool ) %}
110110
{% raw %}{{range gets "/members/*"}} server {{base .Key}} {{$data := json .Value}}{{base (replace (index (split $data.conn_url "/") 2) "@" "/" -1)}} check port {{index (split (index (split $data.api_url "/") 2) ":") 1}}
111111
{{end}}{% endraw %}
112112
{% endif %}

roles/deploy-finish/tasks/main.yml

+8-8
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@
6666
- port 5002 (read only) synchronous replica only
6767
- port 5003 (read only) asynchronous replicas only
6868
- +------------------------------------------------+
69-
when: with_haproxy_load_balancing == "true" and
70-
synchronous_mode == "true"
69+
when: with_haproxy_load_balancing|bool and
70+
synchronous_mode|bool
7171

7272
- name: PostgreSQL Cluster connection info
7373
run_once: true
@@ -78,8 +78,8 @@
7878
- port 5000 (read/write) master
7979
- port 5001 (read only) all replicas
8080
- +------------------------------------------------+
81-
when: with_haproxy_load_balancing == "true" and
82-
synchronous_mode != "true"
81+
when: with_haproxy_load_balancing|bool and
82+
not synchronous_mode|bool
8383

8484
- name: PostgreSQL Cluster connection info
8585
run_once: true
@@ -89,8 +89,8 @@
8989
- address (VIP) {{ cluster_vip }}
9090
- port {{ pgbouncer_listen_port }} (pgbouncer)
9191
- +------------------------------------------------+
92-
when: with_haproxy_load_balancing == "false" and
93-
install_pgbouncer == "true"
92+
when: not with_haproxy_load_balancing|bool and
93+
install_pgbouncer|bool
9494

9595
- name: PostgreSQL Cluster connection info
9696
run_once: true
@@ -100,8 +100,8 @@
100100
- address (VIP) {{ cluster_vip }}
101101
- port {{ postgresql_port }}
102102
- +------------------------------------------------+
103-
when: with_haproxy_load_balancing == "false" and
104-
install_pgbouncer != "true"
103+
when: not with_haproxy_load_balancing|bool and
104+
not install_pgbouncer|bool
105105
when: cluster_vip is defined and cluster_vip | length > 0
106106
ignore_errors: true
107107
tags: conn_info, cluster_info, cluster_status

roles/haproxy/tasks/main.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
package:
1010
name: centos-release-scl-rh
1111
state: present
12-
when: haproxy_install_repo == "true" and
12+
when: haproxy_install_repo|bool and
1313
(ansible_distribution == 'CentOS' and
1414
ansible_distribution_major_version == '7')
1515
tags: haproxy_scl_repo
@@ -22,14 +22,14 @@
2222
gpgkey: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
2323
gpgcheck: true
2424
enabled: true
25-
when: haproxy_install_repo == "true" and
25+
when: haproxy_install_repo|bool and
2626
(ansible_distribution == "OracleLinux" and
2727
ansible_distribution_major_version == '7')
2828
tags: haproxy_scl_repo
2929

3030
- name: Enable Software Collections (SCL) repository for RedHat 7
3131
command: 'sudo yum-config-manager --enable rhel-server-rhscl-7-rpms'
32-
when: haproxy_install_repo == "true" and
32+
when: haproxy_install_repo|bool and
3333
(ansible_distribution == 'Red Hat Enterprise Linux' and
3434
ansible_distribution_major_version == '7')
3535
tags: haproxy_scl_repo
@@ -73,7 +73,7 @@
7373
apt_key:
7474
url: https://haproxy.debian.net/bernat.debian.org.gpg
7575
state: present
76-
when: haproxy_install_repo == "true" and
76+
when: haproxy_install_repo|bool and
7777
(ansible_distribution == "Debian" and
7878
ansible_distribution_major_version is version('10', '<='))
7979

@@ -82,7 +82,7 @@
8282
repo: "deb http://haproxy.debian.net {{ ansible_distribution_release }}-backports-1.8 main"
8383
state: present
8484
update_cache: true
85-
when: haproxy_install_repo == "true" and
85+
when: haproxy_install_repo|bool and
8686
(ansible_distribution == "Debian" and
8787
ansible_distribution_major_version is version('10', '<='))
8888

@@ -103,7 +103,7 @@
103103
=ZiOf
104104
-----END PGP PUBLIC KEY BLOCK-----
105105
state: present
106-
when: haproxy_install_repo == "true" and
106+
when: haproxy_install_repo|bool and
107107
(ansible_distribution == 'Ubuntu' and
108108
ansible_distribution_major_version is version('18', '<='))
109109

@@ -112,7 +112,7 @@
112112
repo: "deb http://ppa.launchpad.net/vbernat/haproxy-1.8/ubuntu {{ ansible_distribution_release }} main"
113113
state: present
114114
update_cache: true
115-
when: haproxy_install_repo == "true" and
115+
when: haproxy_install_repo|bool and
116116
(ansible_distribution == 'Ubuntu' and
117117
ansible_distribution_major_version is version('18', '<='))
118118

roles/haproxy/templates/haproxy.cfg.j2

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ listen master
3636
option httpchk OPTIONS /master
3737
http-check expect status 200
3838
default-server inter 3s fastinter 1s fall 3 rise 4 on-marked-down shutdown-sessions
39-
{% if install_pgbouncer == "true" %}
39+
{% if install_pgbouncer|bool %}
4040
{% for host in groups['postgres_cluster'] %}
4141
server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ pgbouncer_listen_port }} check port 8008
4242
{% endfor %}
4343
{% endif %}
44-
{% if ( install_pgbouncer is not defined ) or ( install_pgbouncer != "true" ) or ( install_pgbouncer | length < 1 ) %}
44+
{% if ( install_pgbouncer is not defined ) or ( not install_pgbouncer|bool ) %}
4545
{% for host in groups['postgres_cluster'] %}
4646
server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ postgresql_port }} check port 8008
4747
{% endfor %}
@@ -59,12 +59,12 @@ listen replicas
5959
balance roundrobin
6060
http-check expect status 200
6161
default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions
62-
{% if install_pgbouncer == "true" %}
62+
{% if install_pgbouncer|bool %}
6363
{% for host in groups['postgres_cluster'] %}
6464
server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ pgbouncer_listen_port }} check port 8008
6565
{% endfor %}
6666
{% endif %}
67-
{% if ( install_pgbouncer is not defined ) or ( install_pgbouncer != "true" ) or ( install_pgbouncer | length < 1 ) %}
67+
{% if ( install_pgbouncer is not defined ) or ( not install_pgbouncer|bool ) %}
6868
{% for host in groups['postgres_cluster'] %}
6969
server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ postgresql_port }} check port 8008
7070
{% endfor %}
@@ -82,12 +82,12 @@ listen replicas_sync
8282
balance roundrobin
8383
http-check expect status 200
8484
default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions
85-
{% if install_pgbouncer == "true" %}
85+
{% if install_pgbouncer|bool %}
8686
{% for host in groups['postgres_cluster'] %}
8787
server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ pgbouncer_listen_port }} check port 8008
8888
{% endfor %}
8989
{% endif %}
90-
{% if ( install_pgbouncer is not defined ) or ( install_pgbouncer != "true" ) or ( install_pgbouncer | length < 1 ) %}
90+
{% if ( install_pgbouncer is not defined ) or ( not install_pgbouncer|bool ) %}
9191
{% for host in groups['postgres_cluster'] %}
9292
server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ postgresql_port }} check port 8008
9393
{% endfor %}
@@ -105,12 +105,12 @@ listen replicas_async
105105
balance roundrobin
106106
http-check expect status 200
107107
default-server inter 3s fastinter 1s fall 3 rise 2 on-marked-down shutdown-sessions
108-
{% if install_pgbouncer == "true" %}
108+
{% if install_pgbouncer|bool %}
109109
{% for host in groups['postgres_cluster'] %}
110110
server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ pgbouncer_listen_port }} check port 8008
111111
{% endfor %}
112112
{% endif %}
113-
{% if ( install_pgbouncer is not defined ) or ( install_pgbouncer != "true" ) or ( install_pgbouncer | length < 1 ) %}
113+
{% if ( install_pgbouncer is not defined ) or ( not install_pgbouncer|bool ) %}
114114
{% for host in groups['postgres_cluster'] %}
115115
server {{ hostvars[host]['ansible_hostname'] }} {{ hostvars[host]['inventory_hostname'] }}:{{ postgresql_port }} check port 8008
116116
{% endfor %}

0 commit comments

Comments
 (0)