|
8 | 8 | import_playbook: ssh-keys.yml |
9 | 9 |
|
10 | 10 | - hosts: mongo |
11 | | - name: Prepare for certificates |
12 | | - become: true |
13 | | - become_user: root |
| 11 | + name: Certificate Paths |
| 12 | + vars_prompt: |
| 13 | + - name: input_key |
| 14 | + prompt: "Enter path to certificate private key file on local machine (e.g. /path/to/.ssl-key) [REQUIRED]" |
| 15 | + private: false |
| 16 | + - name: input_cert |
| 17 | + prompt: "Enter path to certificate full chain/certificate file on local machine (e.g. /path/to/.ssl-cert) [REQUIRED]" |
| 18 | + private: false |
| 19 | + - name: input_ca |
| 20 | + prompt: "Enter path to certificate CA bundle file on local machine (e.g. /path/to/.ssl-ca) [REQUIRED]" |
| 21 | + private: false |
14 | 22 | tasks: |
15 | | - - name: Ensure /var/www/production directory exists for certificates |
16 | | - file: |
17 | | - path: /var/www/production |
18 | | - state: directory |
19 | | - owner: deploy |
20 | | - group: deploy |
21 | | - mode: "0755" |
22 | | - |
23 | | -- name: Import certificates playbook |
24 | | - import_playbook: certificates.yml |
| 23 | + - name: Fail if key path is empty |
| 24 | + fail: |
| 25 | + msg: "Certificate private key path is required. Please re-run the playbook and provide a valid path." |
| 26 | + when: (input_key is not defined) or (input_key | length == 0) |
| 27 | + |
| 28 | + - name: Fail if cert path is empty |
| 29 | + fail: |
| 30 | + msg: "Certificate file path is required. Please re-run the playbook and provide a valid path." |
| 31 | + when: (input_cert is not defined) or (input_cert | length == 0) |
| 32 | + |
| 33 | + - name: Fail if CA path is empty |
| 34 | + fail: |
| 35 | + msg: "CA bundle file path is required. Please re-run the playbook and provide a valid path." |
| 36 | + when: (input_ca is not defined) or (input_ca | length == 0) |
| 37 | + |
| 38 | + - name: Check if key file exists locally |
| 39 | + local_action: stat path={{ input_key }} |
| 40 | + become: false |
| 41 | + register: local_key_file |
| 42 | + |
| 43 | + - name: Fail when local key file does not exist |
| 44 | + fail: |
| 45 | + msg: "key file does not exist: {{ input_key }}" |
| 46 | + when: not local_key_file.stat.exists |
| 47 | + |
| 48 | + - name: Check if cert file exists locally |
| 49 | + local_action: stat path={{ input_cert }} |
| 50 | + become: false |
| 51 | + register: local_cert_file |
| 52 | + |
| 53 | + - name: Fail when local cert file does not exist |
| 54 | + fail: |
| 55 | + msg: "cert file does not exist: {{ input_cert }}" |
| 56 | + when: not local_cert_file.stat.exists |
| 57 | + |
| 58 | + - name: Check if CA file exists locally |
| 59 | + local_action: stat path={{ input_ca }} |
| 60 | + become: false |
| 61 | + register: local_ca_file |
| 62 | + |
| 63 | + - name: Fail when local CA file does not exist |
| 64 | + fail: |
| 65 | + msg: "CA file does not exist: {{ input_ca }}" |
| 66 | + when: not local_ca_file.stat.exists |
| 67 | + |
| 68 | + - name: Store certificate paths as facts |
| 69 | + set_fact: |
| 70 | + ssl_key_path: "{{ input_key }}" |
| 71 | + ssl_cert_path: "{{ input_cert }}" |
| 72 | + ssl_ca_path: "{{ input_ca }}" |
25 | 73 |
|
26 | 74 | - hosts: mongo |
27 | 75 | name: Hostname |
|
33 | 81 | that: |
34 | 82 | - lookup('env', 'MONGO_HOST') != '' |
35 | 83 | - lookup('env', 'MONGO_PORT') != '' |
36 | | - - lookup('env', 'SSL_CERT_PATH') != '' |
37 | | - - lookup('env', 'SSL_KEY_PATH') != '' |
38 | | - - lookup('env', 'SSL_CA_PATH') != '' |
39 | 84 | - lookup('env', 'AWS_ACCESS_KEY_ID') != '' |
40 | 85 | - lookup('env', 'AWS_SECRET_ACCESS_KEY') != '' |
41 | 86 | - lookup('env', 'AWS_ENDPOINT_URL') != '' |
|
184 | 229 | group: mongodb |
185 | 230 | mode: "0750" |
186 | 231 |
|
187 | | - - name: Copy SSL certificate from /var/www/production to MongoDB directory |
| 232 | + # Copy SSL certificates directly from local machine to MongoDB directory |
| 233 | + - name: Copy SSL certificate to MongoDB directory |
188 | 234 | copy: |
189 | | - src: "{{ lookup('env', 'SSL_CERT_PATH') }}" |
| 235 | + src: "{{ hostvars[inventory_hostname]['ssl_cert_path'] }}" |
190 | 236 | dest: /etc/mongodb/ssl/mongodb.crt |
191 | 237 | owner: mongodb |
192 | 238 | group: mongodb |
193 | 239 | mode: "0400" |
194 | | - remote_src: yes |
195 | 240 |
|
196 | | - - name: Copy SSL private key from /var/www/production to MongoDB directory |
| 241 | + - name: Copy SSL private key to MongoDB directory |
197 | 242 | copy: |
198 | | - src: "{{ lookup('env', 'SSL_KEY_PATH') }}" |
| 243 | + src: "{{ hostvars[inventory_hostname]['ssl_key_path'] }}" |
199 | 244 | dest: /etc/mongodb/ssl/mongodb.key |
200 | 245 | owner: mongodb |
201 | 246 | group: mongodb |
202 | 247 | mode: "0400" |
203 | | - remote_src: yes |
204 | 248 |
|
205 | | - - name: Copy CA certificate from /var/www/production to MongoDB directory |
| 249 | + - name: Copy CA certificate to MongoDB directory |
206 | 250 | copy: |
207 | | - src: "{{ lookup('env', 'SSL_CA_PATH') }}" |
| 251 | + src: "{{ hostvars[inventory_hostname]['ssl_ca_path'] }}" |
208 | 252 | dest: /etc/mongodb/ssl/ca.pem |
209 | 253 | owner: mongodb |
210 | 254 | group: mongodb |
211 | 255 | mode: "0400" |
212 | | - remote_src: yes |
213 | 256 |
|
214 | 257 | - name: Create MongoDB TLS/SSL certificate PEM file (cert + key) |
215 | 258 | shell: | |
|
219 | 262 | args: |
220 | 263 | creates: /etc/mongodb/ssl/mongodb.pem |
221 | 264 |
|
222 | | - # Clean up certificates from /var/www/production for security |
223 | | - - name: Remove SSL certificate from /var/www/production (security cleanup) |
224 | | - file: |
225 | | - path: "{{ lookup('env', 'SSL_CERT_PATH') }}" |
226 | | - state: absent |
227 | | - |
228 | | - - name: Remove SSL private key from /var/www/production (security cleanup) |
229 | | - file: |
230 | | - path: "{{ lookup('env', 'SSL_KEY_PATH') }}" |
231 | | - state: absent |
232 | | - |
233 | | - - name: Remove CA certificate from /var/www/production (security cleanup) |
234 | | - file: |
235 | | - path: "{{ lookup('env', 'SSL_CA_PATH') }}" |
236 | | - state: absent |
237 | | - |
238 | 265 | - name: Configure MongoDB TLS/SSL in mongod.conf |
239 | 266 | blockinfile: |
240 | 267 | path: /etc/mongod.conf |
|
0 commit comments