Skip to content

Commit ea6cd64

Browse files
committed
Code Refactoring
* Reduced the number of lines of code. Now we use "default (omit)" for the variable "proxy_env"; * Modules "apt" and "yum" have been replaced by the module "package"; * The tasks "Install EPEL Repository" and "install PostgreSQL repository" were transferred to the add-repository.yml file (for RedHad based); The variables "install_epel_repo" and "install_postgresql_repo" were added to the variable file RedHat.yml; * The variables "os_valid_distributions" and "os_minimum_versions" were transferred from the variable file "main.yml" to the group_vars file postgres_cluster; * "with_items" were replaced by "loop" (with the release of Ansible 2.5, the recommended way to perform loops is the use the new loop keyword instead of with_X style loops); * Minor code corrections.
1 parent 2defd74 commit ea6cd64

24 files changed

+157
-490
lines changed

group_vars/postgres_cluster

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
# "Check system" variables
3+
4+
os_valid_distributions:
5+
- RedHat
6+
- CentOS
7+
- Ubuntu
8+
- Debian
9+
10+
os_minimum_versions:
11+
RedHat: 7
12+
CentOS: 7
13+
Ubuntu: 16.04
14+
Debian: 8

tags.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
## Available tags
33

44
- add_repo
5+
- - install_epel_repo
6+
- - install_postgresql_repo
57
- install_packages
68
- - install_postgres
79
- sudo

tasks/add-repository.yml

+30-41
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,18 @@
66
apt_key:
77
url: "{{ item.key }}"
88
state: present
9-
with_items: "{{ apt_repository_keys }}"
10-
when: (proxy_env is not defined) or (proxy_env | length < 1)
9+
loop: "{{ apt_repository_keys }}"
1110

1211
- name: Add repository
1312
apt_repository:
1413
repo: "{{ item.repo }}"
1514
state: present
1615
update_cache: yes
17-
with_items: "{{ apt_repository }}"
18-
when: (proxy_env is not defined) or (proxy_env | length < 1)
19-
20-
21-
- name: Add repository apt-key (behind a proxy)
22-
apt_key:
23-
url: "{{ item.key }}"
24-
state: present
25-
with_items: "{{ apt_repository_keys }}"
26-
environment:
27-
"{{ proxy_env }}"
28-
when: proxy_env | length > 0
29-
30-
- name: Add repository (behind a proxy)
31-
apt_repository:
32-
repo: "{{ item.repo }}"
33-
state: present
34-
update_cache: yes
35-
with_items: "{{ apt_repository }}"
36-
environment:
37-
"{{ proxy_env }}"
38-
when: proxy_env | length > 0
39-
16+
loop: "{{ apt_repository }}"
17+
environment: "{{ proxy_env | default(omit) }}"
4018
when: ansible_os_family == "Debian"
4119
tags: add_repo
4220

43-
4421
# RedHat CentOS
4522
- block:
4623
- name: Add repository
@@ -49,20 +26,32 @@
4926
description: "{{ item.description }}"
5027
baseurl: "{{ item.baseurl }}"
5128
gpgkey: "{{ item.gpgkey }}"
52-
with_items: "{{ yum_repository }}"
53-
when: (yum_repository length > 0) and (proxy_env is not defined or proxy_env | length < 1)
54-
55-
- name: Add repository (behind a proxy)
56-
yum_repository:
57-
name: "{{ item.name }}"
58-
description: "{{ item.description }}"
59-
baseurl: "{{ item.baseurl }}"
60-
gpgkey: "{{ item.gpgkey }}"
61-
with_items: "{{ yum_repository }}"
62-
environment:
63-
"{{ proxy_env }}"
64-
when: (yum_repository length > 0) and (proxy_env | length > 0)
65-
29+
loop: "{{ yum_repository | flatten(1) }}"
30+
when: yum_repository length > 0
31+
32+
# Install Epel Repository
33+
- name: Install | EPEL Repository
34+
package:
35+
name: epel-release
36+
when: install_epel_repo == "true"
37+
tags: install_epel_repo
38+
39+
# Install PostgreSQL Repository
40+
- name: install | get pgdg-redhat-repo-latest.noarch.rpm
41+
get_url:
42+
url: "https://download.postgresql.org/pub/repos/yum/reporpms/EL-{{ ansible_distribution_major_version }}-x86_64/pgdg-redhat-repo-latest.noarch.rpm"
43+
dest: /tmp/
44+
timeout: 30
45+
validate_certs: no
46+
when: install_postgresql_repo == "true"
47+
tags: install_postgresql_repo
48+
49+
- name: install | PostgreSQL repository
50+
package:
51+
name: /tmp/pgdg-redhat-repo-latest.noarch.rpm
52+
state: present
53+
when: install_postgresql_repo == "true"
54+
tags: install_postgresql_repo
55+
environment: "{{ proxy_env | default(omit) }}"
6656
when: ansible_os_family == "RedHat"
6757
tags: add_repo
68-

tasks/confd.yml

+2-14
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,14 @@
77
mode: u+x,g+x,o+x
88
timeout: 30
99
validate_certs: no
10-
when: (proxy_env is not defined) or (proxy_env | length < 1)
11-
tags: [ get_confd, confd ]
12-
13-
- name: confd | download and copy "confd" binary file to /usr/local/bin (behind a proxy)
14-
get_url:
15-
url: https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-amd64
16-
dest: /usr/local/bin/confd
17-
mode: u+x,g+x,o+x
18-
timeout: 30
19-
validate_certs: no
20-
environment:
21-
"{{ proxy_env }}"
22-
when: proxy_env | length > 0
10+
environment: "{{ proxy_env | default(omit) }}"
2311
tags: [ get_confd, confd ]
2412

2513
- name: confd | create conf directories
2614
file:
2715
path: "{{ item }}"
2816
state: directory
29-
with_items:
17+
loop:
3018
- /etc/confd/conf.d
3119
- /etc/confd/templates
3220
tags: [ confd_dir, confd_conf, confd ]

tasks/deploy_finish.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,13 @@
7676
ignore_errors: yes
7777
tags: conn_info
7878

79-
8079
- block:
8180
- name: Virtual IP Address (VIP) info
8281
set_fact:
8382
man_ip: "{{ item }}"
84-
with_items: "{{ ansible_all_ipv4_addresses }}"
83+
loop: "{{ ansible_all_ipv4_addresses }}"
8584
when: item == cluster_vip
8685
- debug:
8786
msg: Cluster ip address (VIP) {{ cluster_vip }} is running on server "{{ ansible_hostname }}"
8887
when: man_ip is defined and man_ip == cluster_vip
8988
tags: [ vip_owner, vip_status ]
90-

tasks/etcd.yml

+2-12
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,15 @@
66
dest: /tmp/
77
remote_src: yes
88
validate_certs: no
9-
when: (proxy_env is not defined) or (proxy_env | length < 1)
10-
11-
- name: etcd cluster | download and extract "etcd" into /tmp (behind a proxy)
12-
unarchive:
13-
src: https://github.com/etcd-io/etcd/releases/download/{{ etcd_ver }}/etcd-{{ etcd_ver }}-linux-amd64.tar.gz
14-
dest: /tmp/
15-
remote_src: yes
16-
validate_certs: no
17-
environment:
18-
"{{ proxy_env }}"
19-
when: proxy_env | length > 0
9+
environment: "{{ proxy_env | default(omit) }}"
2010

2111
- name: etcd cluster | copy "etcd" and "etcdctl" binary files to /usr/local/bin/
2212
copy:
2313
src: "/tmp/etcd-{{ etcd_ver }}-linux-amd64/{{ item }}"
2414
dest: /usr/local/bin/
2515
mode: u+x,g+x,o+x
2616
remote_src: yes
27-
with_items:
17+
loop:
2818
- etcd
2919
- etcdctl
3020

tasks/haproxy.yml

+4-80
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@
2121

2222
# "Debian"
2323
- name: haproxy | Install the prerequisites packages to compile HAProxy
24-
apt:
25-
update_cache: yes
24+
package:
2625
name: "{{ packages }}"
27-
state: present
2826
vars:
2927
packages:
3028
- unzip
@@ -42,10 +40,8 @@
4240

4341
# "RedHat"
4442
- name: haproxy | Install the prerequisites packages to compile HAProxy
45-
yum:
46-
update_cache: yes
43+
package:
4744
name: "{{ packages }}"
48-
state: present
4945
vars:
5046
packages:
5147
- unzip
@@ -59,78 +55,7 @@
5955
- openssl-libs
6056
- systemd-devel
6157
when: ansible_os_family == "RedHat"
62-
when: (proxy_env is not defined) or (proxy_env | length < 1)
63-
64-
# with proxy
65-
- block:
66-
- name: "haproxy | download HAProxy {{ haproxy_version }} source files (behind a proxy)"
67-
become: yes
68-
become_user: root
69-
unarchive:
70-
src: http://www.haproxy.org/download/{{ haproxy_major }}/src/haproxy-{{ haproxy_version }}.tar.gz
71-
dest: /tmp/
72-
remote_src: yes
73-
environment:
74-
"{{ proxy_env }}"
75-
76-
- name: "haproxy | download lua-5.3.5 source files (behind a proxy)"
77-
become: yes
78-
become_user: root
79-
unarchive:
80-
src: https://www.lua.org/ftp/lua-5.3.5.tar.gz
81-
dest: /tmp/
82-
remote_src: yes
83-
validate_certs: no
84-
environment:
85-
"{{ proxy_env }}"
86-
tags: lua
87-
88-
# "Debian"
89-
- name: haproxy | install the prerequisites packages to compile HAProxy (behind a proxy)
90-
apt:
91-
update_cache: yes
92-
name: "{{ packages }}"
93-
state: present
94-
vars:
95-
packages:
96-
- unzip
97-
- gzip
98-
- build-essential
99-
- libc6-dev
100-
- libpcre3-dev
101-
- liblua5.3-dev
102-
- libreadline-dev
103-
- zlib1g-dev
104-
- libsystemd-dev
105-
- ca-certificates
106-
- libssl-dev
107-
environment:
108-
"{{ proxy_env }}"
109-
when: ansible_os_family == "Debian"
110-
111-
# "RedHat"
112-
- name: haproxy | install the prerequisites packages to compile HAProxy (behind a proxy)
113-
yum:
114-
update_cache: yes
115-
name: "{{ packages }}"
116-
state: present
117-
vars:
118-
packages:
119-
- unzip
120-
- gzip
121-
- "@Development tools"
122-
- pcre-devel
123-
- zlib-devel
124-
- readline-devel
125-
- openssl
126-
- openssl-devel
127-
- openssl-libs
128-
- systemd-devel
129-
environment:
130-
"{{ proxy_env }}"
131-
when: ansible_os_family == "RedHat"
132-
when: proxy_env | length > 0
133-
58+
environment: "{{ proxy_env | default(omit) }}"
13459

13560
- name: haproxy | add haproxy group
13661
group:
@@ -150,7 +75,7 @@
15075
state: directory
15176
owner: haproxy
15277
group: haproxy
153-
with_items:
78+
loop:
15479
- /etc/haproxy
15580
- /var/run/haproxy
15681
- /var/lib/haproxy/dev
@@ -231,4 +156,3 @@
231156
# virtual ip address (and haproxy health checking)
232157
- import_tasks: ../tasks/keepalived.yml
233158
tags: keepalived
234-

tasks/hostname.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
line: "{{ item.line }}"
1313
state: present
1414
no_log: True
15-
with_items:
15+
loop:
1616
- { regexp: '^127\.0\.0\.1[ \t]+localhost', line: '127.0.0.1 localhost {{ ansible_hostname }}' }
1717
- { regexp: '^{{ ansible_default_ipv4.address }}.+$', line: '{{ ansible_default_ipv4.address }} {{ ansible_hostname }}' }
1818
when: hostname_result.changed

tasks/keepalived.yml

+2-14
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,7 @@
33
- name: keepalived | install packages
44
package:
55
name: keepalived
6-
state: present
7-
update_cache: yes
8-
when: (proxy_env is not defined) or (proxy_env | length < 1)
9-
tags: [ keepalived_install, keepalived ]
10-
11-
- name: keepalived | install packages (behind a proxy)
12-
package:
13-
name: keepalived
14-
state: present
15-
update_cache: yes
16-
environment:
17-
"{{ proxy_env }}"
18-
when: proxy_env | length > 0
6+
environment: "{{ proxy_env | default(omit) }}"
197
tags: [ keepalived_install, keepalived ]
208

219
- name: keepalived | make sure the kernel parameters "net.ipv4.ip_nonlocal_bind", "net.ipv4.ip_forward" are enabled
@@ -25,7 +13,7 @@
2513
sysctl_set: "yes"
2614
state: "present"
2715
reload: "yes"
28-
with_items:
16+
loop:
2917
- "net.ipv4.ip_nonlocal_bind"
3018
- "net.ipv4.ip_forward"
3119
tags: [ keepalived_conf, keepalived ]

tasks/locales.yml

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
locale_gen:
77
name: "{{ item.language_country }}.{{ item.encoding }}"
88
state: present
9-
with_items:
10-
- "{{ locales }}"
9+
loop: "{{ locales | flatten(1) }}"
1110
when: ansible_os_family == "Debian"
1211

1312
# RedHat
1413
- name: Generate locales "{% for set in locales %}{{ set.language_country }}.{{ set.encoding }}{% if not loop.last %},{% endif %}{% endfor %}"
1514
become: yes
1615
command: localedef -c -i {{ item.language_country }} -f {{ item.encoding }} {{ item.language_country }}.{{ item.encoding }}
1716
changed_when: false
18-
with_items:
19-
- "{{ locales }}"
17+
loop: "{{ locales | flatten(1) }}"
2018
when: ansible_os_family == "RedHat"
21-

tasks/ntp.yml

+1-14
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,7 @@
33
- name: ntp | install package
44
package:
55
name: ntp
6-
state: present
7-
update_cache: yes
8-
when: (proxy_env is not defined) or (proxy_env | length < 1)
9-
tags: [ ntp_install, ntp ]
10-
11-
- name: ntp | install package (behind a proxy)
12-
package:
13-
name: ntp
14-
state: present
15-
update_cache: yes
16-
environment:
17-
"{{ proxy_env }}"
18-
when: proxy_env | length > 0
6+
environment: "{{ proxy_env | default(omit) }}"
197
tags: [ ntp_install, ntp ]
208

219
- name: ntp | copy the ntp.conf template file
@@ -39,4 +27,3 @@
3927
enabled: yes
4028
state: restarted
4129
when: ansible_os_family == "RedHat"
42-

0 commit comments

Comments
 (0)