|
1 | 1 | --- |
2 | | -- name: Deploy, configure, and populate Postgres 10 |
3 | | - hosts: application_servers |
| 2 | +- name: Deploy, configure, and populate flask repo and virtualenv |
| 3 | + hosts: app_servers |
4 | 4 | become: true |
5 | 5 | gather_facts: false |
6 | 6 | tags: |
7 | 7 | - application_servers |
8 | 8 | vars: |
9 | | - flask_app_name: resource_hub |
| 9 | + flask_app_name: resource_hub |
10 | 10 | flask_user: flask |
11 | 11 | flask_home: /opt/ |
12 | 12 | flask_repo: https://github.com/tonykay/resource_hub.git |
13 | 13 | flask_scm_ref: main |
14 | | - |
15 | | - flask_packages: |
| 14 | + virtualenv_name: "venv-{{ flask_app_name }}" |
| 15 | + virtualenv_home: /opt/virtual_envs |
| 16 | + virtualenv_python: /usr/bin/python3 |
| 17 | + |
| 18 | + |
| 19 | + app_yum_packages: |
| 20 | + - autoconf |
| 21 | + - automake |
| 22 | + - git |
| 23 | + - gcc |
| 24 | + - libtool |
16 | 25 | - python3 |
17 | | - postgres_users: |
18 | | - - name: flask |
19 | | - password: redhat |
20 | | - database: flask_db |
21 | | - state: present |
22 | | - priv: ALL |
| 26 | + - python3-devel |
| 27 | + - python3-pip |
| 28 | + |
| 29 | + pip_dependencies: |
| 30 | + - virtualenv |
| 31 | + - pip |
23 | 32 |
|
24 | 33 | tasks: |
25 | 34 |
|
26 | 35 | - name: Install flask packages |
27 | 36 | package: |
28 | 37 | name: "{{ __package }}" |
29 | 38 | state: present |
30 | | - loop: "{{ flask_packages }}" |
| 39 | + loop: "{{ app_yum_packages }}" |
31 | 40 | loop_control: |
32 | 41 | loop_var: __package |
33 | 42 |
|
34 | | - - name: "Install flask repo {{ flask_repo }}" |
35 | | - git: |
36 | | - repo: "{{ flask_repo }}" |
37 | | - dest: "{{ flask_home }}" |
38 | | - update: yes |
39 | | - |
40 | | - - name: install modules in a virtualenv |
41 | | - pip: |
42 | | - requirements: "{{ flask_home }}/{{ flask_app_name }}/requirements.txt |
43 | | - virtualenv: "{{ /home/{{ ansible_ssh_user }}/{{ app_name }}/env |
44 | | - virtualenv_python: python3.5 |
45 | | - |
46 | | - - name: Check if Postgres initialized |
47 | | - stat: |
48 | | - path: "{{ postgres_10_data_dir }}/PG_VERSION" |
49 | | - register: r_postgres_init_dir_state |
50 | | - |
51 | | - - when: not r_postgres_init_dir_state.stat.exists |
52 | | - name: Run Postgres initdb to initialize if postgres not initialized |
53 | | - command: "{{ postgres_10_bin_path }}/postgresql-10-setup initdb" |
54 | | - notify: restart_postgres |
55 | | - |
56 | | - - name: Setup Postgres for remote password auth |
57 | | - template: |
58 | | - src: pg_hba.conf.j2 |
59 | | - dest: "{{ postgres_10_data_dir }}/pg_hba.conf" |
60 | | - notify: restart_postgres |
| 43 | + - name: "Install flask repo {{ flask_repo }}" |
| 44 | + git: |
| 45 | + repo: "{{ flask_repo }}" |
| 46 | + dest: "{{ flask_home }}/{{ flask_app_name }}" |
| 47 | + update: yes |
61 | 48 |
|
62 | | - - name: Setup Postgres to listen on network interfaces |
63 | | - lineinfile: |
64 | | - dest: "{{ postgres_10_data_dir }}/postgresql.conf" |
65 | | - line: "listen_addresses = '*'" |
66 | | - insertafter: "#listen_addresses" |
67 | | - notify: restart_postgres |
68 | | - |
69 | | - - name: Setup Postgres as started and enabled on boot. |
70 | | - service: |
71 | | - name: "{{ postgres_service }}" |
72 | | - state: "{{ postgres_service_state | default('started') }}" |
73 | | - enabled: "{{ postgres_service_enabled | default(true) }}" |
74 | | - |
75 | | - - name: Configure Postgres resources, user, database etc |
| 49 | + - name: virtualenv setup |
76 | 50 | block: |
77 | 51 |
|
78 | | - - name: Setup Postgres database(s) |
79 | | - postgresql_db: |
80 | | - name: "{{ __database.name }}" |
81 | | - state: "{{ __database.state | default('present') }}" |
82 | | - loop: "{{ postgres_databases }}" |
83 | | - loop_control: |
84 | | - loop_var: __database |
85 | | - |
86 | | - - name: Create Postgres user#(s) |
87 | | - postgresql_user: |
88 | | - name: "{{ __user.name }}" |
89 | | - password: "{{ __user.password | default(omit) }}" |
90 | | - encrypted: true # "{{ __user.encrypted | default(omit) }}" |
91 | | - priv: "{{ __user.priv | default(omit) }}" |
92 | | - db: "{{ __user.database | default(omit) }}" |
93 | | - # role_attr_flags: "{{ __user.role_attr_flags | default(omit) }}" |
94 | | - state: "{{ __user.state | default('present') }}" |
95 | | - loop: "{{ postgres_users }}" |
96 | | - loop_control: |
97 | | - loop_var: __user |
98 | | - no_log: "{{ postgres_user_no_log_state | default(false) }}" |
99 | | - |
100 | | - |
101 | | - become_user: "{{ postgres_user }}" |
102 | | - |
103 | | - handlers: |
104 | | - |
105 | | - - name: restart_postgres |
106 | | - service: |
107 | | - name: "{{ postgres_service }}" |
108 | | - state: restarted |
109 | | - |
110 | | - |
111 | | -- name: Deploy Application Tier |
112 | | - hosts: app_servers |
113 | | - gather_facts: false |
114 | | - become: true |
115 | | - vars: |
116 | | - postgres_rhel7_repo: "https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm" |
117 | | - app_server_packages: |
118 | | - - python-psycopg2 |
119 | | - app_server_port: 8080 |
120 | | - app_server_service: tomcat |
121 | | - |
122 | | - tags: |
123 | | - - app_servers |
124 | | - |
125 | | - tasks: |
126 | | - |
127 | | - - name: Install Tomcat package(s) |
128 | | - package: |
129 | | - name: "{{ app_server_packages }}" |
130 | | - state: present |
131 | | - |
132 | | - - name: Enable Tomcat at boot |
133 | | - service: |
134 | | - name: tomcat |
135 | | - enabled: yes |
136 | | - |
137 | | - - name: Create Tomcat content directory |
138 | | - file: |
139 | | - path: /usr/share/tomcat/webapps/ROOT |
140 | | - state: directory |
141 | | - |
142 | | - - name: Copy static index.html to tomcat webapps/ansible/index.html |
143 | | - template: |
144 | | - src: index.html.j2 |
145 | | - dest: /usr/share/tomcat/webapps/ROOT/index.html |
146 | | - mode: 0644 |
147 | | - |
148 | | - - name: Start Tomcat |
149 | | - service: |
150 | | - name: "{{ app_server_service }}" |
151 | | - state: started |
152 | | - |
153 | | - |
154 | | -- name: Deploy haproxy load balancer |
155 | | - hosts: load_balancers |
156 | | - gather_facts: false |
157 | | - become: true |
158 | | - |
159 | | - vars: |
160 | | - load_balancer_packages: |
161 | | - - haproxy |
162 | | - |
163 | | - tags: |
164 | | - - load_balancers |
165 | | - |
166 | | - tasks: |
167 | | - |
168 | | - - name: Install load balancer packages |
169 | | - package: |
170 | | - name: "{{ load_balancer_packages }}" |
171 | | - state: present |
172 | | - |
173 | | - - name: Configure haproxy load balancing |
174 | | - template: |
175 | | - src: haproxy.cfg.j2 |
176 | | - dest: /etc/haproxy/haproxy.cfg |
177 | | - notify: restart_haproxy |
178 | | - |
179 | | - - name: Enable Haproxy |
180 | | - service: |
181 | | - name: haproxy |
182 | | - state: started |
183 | | - enabled: true |
184 | | - |
185 | | - handlers: |
186 | | - |
187 | | - - name: restart_haproxy |
188 | | - service: |
189 | | - name: haproxy |
190 | | - state: restarted |
191 | | - |
192 | | -- name: End to end smoke tests |
193 | | - hosts: app_servers:database_servers |
194 | | - gather_facts: true |
195 | | - become: true |
196 | | - tags: |
197 | | - - smoketest |
198 | | - |
199 | | - tasks: |
200 | | - |
201 | | - - debug: |
202 | | - msg: |
203 | | - - "IP is {{ hostvars['appdb1']['ansible_default_ipv4']['address'] }}" |
204 | | - - "First App server {{ groups.app_servers[0] }}" |
205 | | - verbosity: 2 |
206 | | - |
207 | | - - name: Smoketest Postgres database |
208 | | - postgresql_ping: |
209 | | - db: flask_db |
210 | | - login_host: "{{ hostvars['appdb1']['ansible_default_ipv4']['address'] }}" |
211 | | - login_user: flask |
212 | | - login_password: redhat |
213 | | - ssl_mode: disable |
214 | | - delegate_to: "{{ groups.app_servers[0] }}" |
215 | | - run_once: true |
| 52 | + - name: Setup pre-requisite pip3 packages |
| 53 | + pip: |
| 54 | + name: "{{ pip_dependencies }}" |
| 55 | + state: latest |
| 56 | + executable: /usr/bin/pip3 |
| 57 | + |
| 58 | + - name: "Create virtualenv {{ virtualenv_name }} for Flask" |
| 59 | + pip: |
| 60 | + requirements: "{{ flask_home }}/{{ flask_app_name }}/requirements.txt" |
| 61 | + virtualenv: "{{ virtualenv_home }}/{{ flask_app_name }}" |
| 62 | + virtualenv_site_packages: no |
| 63 | + virtualenv_command: /usr/local/bin/virtualenv |
216 | 64 | tags: |
217 | | - - smoketest |
218 | | - |
219 | | -- name: End to end smoke tests |
220 | | - hosts: app_servers |
221 | | - become: false |
222 | | - gather_facts: false |
223 | | - tags: |
224 | | - - smoketest |
225 | | - tasks: |
226 | | - |
227 | | - - name: Check webserver for correct response |
228 | | - uri: |
229 | | - url: "http://frontend1.{{ GUID }}.example.opentlc.com" |
230 | | - return_content: yes |
231 | | - until: '"Welcome to Tomcat" in result.content' |
232 | | - retries: 10 |
233 | | - delay: 1 |
234 | | - register: result |
235 | | - delegate_to: localhost |
236 | | - |
237 | | -... |
| 65 | + - venv |
| 66 | + - virtualenv |
| 67 | + |
| 68 | + - name: Create flask user |
| 69 | + user: |
| 70 | + name: "{{ flask_user }}" |
| 71 | + state: present |
| 72 | + |
| 73 | + - name: |
| 74 | +- name: template systemd service config |
| 75 | + copy: |
| 76 | + src: .service |
| 77 | + dest: /etc/systemd/system/{{ app_name }}.service |
| 78 | + - name: start systemd app service |
| 79 | + systemd: name={{ app_name }}.service state=restarted enabled=yes |
0 commit comments