update_domain_wildcard.yaml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. ---
  2. # Update _domain wildcard certificate and private key on web servers running Apache, Gitlab, Nginx & Tomcat"
  3. - name: "Apache group: set group-wide variables"
  4. hosts: apache
  5. vars:
  6. new_cert: "include/current/_domain.cert"
  7. new_bundle: "include/current/root-chain_domain.pem"
  8. new_fullchain_root_bundle: "include/current/fullchain_bundle_domain.pem"
  9. new_private_key: "include/current/_domain.key"
  10. ##########################################################################
  11. apache_config_test_command: "apachectl -t"
  12. apache_apply_command: "systemctl reload httpd.service"
  13. tasks:
  14. - name: "Apache group: dump group-wide variables"
  15. debug:
  16. msg:
  17. - "host is: {{ ansible_host }}"
  18. - "Group from inventory file is: {{ group_names }} "
  19. - "------------------------------------------------------------------"
  20. - "new_cert is: {{ new_cert }}"
  21. - "new_bundle is: {{ new_bundle }}"
  22. - "new_fullchain_root_bundle is: {{ new_fullchain_root_bundle }}"
  23. - "new_private_key is: {{ new_private_key }}"
  24. - "------------------------------------------------------------------"
  25. - "apache_config_test_command is: {{ apache_config_test_command }}"
  26. - "apache_apply_command is: {{ apply_command }}"
  27. - name: "Apache group: dump host-specific variables"
  28. debug:
  29. msg:
  30. - "host is: {{ ansible_host }}"
  31. - "--- Running for group apache only ---"
  32. - "------------------------------------------------------------------"
  33. - "cert_path is: {{ cert_path }}"
  34. - "private_key_path is: {{ private_key_path }}"
  35. - "post_test_command is: {{ post_test_command }}"
  36. - "webserver_hostname is: {{ webserver_hostname }}"
  37. - "------------------------------------------------------------------"
  38. - name: "Apache group: dump OPTIONAL host-specific variable: root_bundle_path"
  39. debug:
  40. msg:
  41. - "host is: {{ ansible_host }}"
  42. - "--- Running for group apache only ---"
  43. - "root_bundle_path is: {{ root_bundle_path }}"
  44. when: root_bundle_path is defined
  45. ################# copy cert section ##########
  46. - name: "Apache hosts: Copy the new certificate file"
  47. copy:
  48. backup: true
  49. src: "{{ new_cert }}"
  50. dest: "{{ cert_path }}"
  51. - name: "Apache: Copy the intermediate certs file"
  52. copy:
  53. backup: true
  54. src: "{{ new_bundle }}"
  55. dest: "{{ root_bundle_path }}"
  56. when: root_bundle_path is defined
  57. - name: "Apache: Copy the private key"
  58. copy:
  59. backup: true
  60. src: "{{ new_private_key }}"
  61. dest: "{{ private_key_path }}"
  62. ################# test section ##############
  63. - name: "Apache: run apache_config_test_command and register output"
  64. command: "{{ apache_config_test_command }}"
  65. register: apache_config_test_result
  66. ignore_errors: yes
  67. - debug:
  68. msg: "{{ apache_config_test_result.stderr }}"
  69. - name: "Set test_outcome to pass if criteria are met"
  70. set_fact:
  71. config_test_outcome: pass
  72. when: '"Syntax OK" in apache_config_test_result.stderr'
  73. ignore_errors: yes
  74. - debug:
  75. msg: "{{ config_test_outcome }}"
  76. ignore_errors: yes
  77. #################### restart embedded nginx if test is ok $$$$$$$$$
  78. - name: "Apache: restart apache if config test is successful"
  79. command: "{{ apache_apply_command }}"
  80. when: config_test_outcome == "pass"
  81. ignore_errors: yes
  82. - name: "Apache: fail if config test is NOT successful"
  83. debug:
  84. msg:
  85. - '======================================================================================'
  86. - '=== !!!FAILURE!!!'
  87. - '=== APACHE CONFIG MODIFICATIONS ON "{{ inventory_hostname }}"AT"{{ ansible_host }}" FAILED.'
  88. - '=== THESE WILL HAVE TO BE FIXED MANUALLY. THESE FILES WERE MODIFIED but also backed up:'
  89. - '==='
  90. - === "{{ cert_path }}"
  91. - === "{{ root_bundle_path }}"
  92. - === "{{ private_key_path }}"
  93. - "=== *** The actual apache configuration wasn't changed, just the cert & key files the config points to."
  94. - "======================================================================================="
  95. when: config_test_outcome is undefined
  96. - name: "Get new cert info by contacting site locally"
  97. get_certificate:
  98. host: "{{ ansible_host }}"
  99. port: "{{ webserver_port }}"
  100. server_name: "{{ webserver_hostname }}"
  101. register: new_cert_info
  102. delegate_to: localhost
  103. when: config_test_outcome == "pass"
  104. - name: "Show results of locally connecting to remote site"
  105. debug:
  106. msg:
  107. - "New cert expires in: {{ new_cert_days_left }} days."
  108. vars:
  109. new_cert_days_left: >-
  110. {{ (
  111. (new_cert_info.not_after | ansible.builtin.to_datetime('%Y%m%d%H%M%SZ')) -
  112. (ansible_date_time.iso8601 | ansible.builtin.to_datetime('%Y-%m-%dT%H:%M:%SZ'))
  113. ).days }}
  114. register: new_cert_days_left
  115. when: config_test_outcome == "pass"
  116. - name: "set_fact from this host's test outputs to display at end of playbook run"
  117. set_fact:
  118. changed_site_hostname_and_new_exp_date: "{{ webserver_hostname }} {{ new_cert_days_left }}"
  119. when: config_test_outcome == "pass"
  120. - name: "Gitlab group: set group-wide variables"
  121. hosts: gitlab
  122. vars:
  123. new_bundle: "include/current/root-chain_domain.pem"
  124. new_fullchain_root_bundle: "include/current/fullchain_bundle_domain.pem"
  125. new_private_key: "include/current/_domain.key"
  126. gitlab_apply_command: "gitlab-ctl restart nginx"
  127. #gitlab_apply_command: " gitlab-ctl hup nginx; gitlab-ctl hup registry"
  128. tasks:
  129. - name: "GitLab group: dump group-wide variables"
  130. debug:
  131. msg:
  132. - "host is: {{ ansible_host }}"
  133. - "This is only showing info from GitLab group"
  134. - "------------------------------------------------------------------"
  135. - "new_bundle is: {{ new_bundle }}"
  136. - "new_fullchain_root_bundle is: {{ new_fullchain_root_bundle }}"
  137. - "new_private_key is: {{ new_private_key }}"
  138. - name: "Gitlab group: dump host-specific variables"
  139. debug:
  140. msg:
  141. - "host is: {{ ansible_host }}"
  142. - "This is only showing info from GitLab group"
  143. - "------------------------------------------------------------------"
  144. - "fullchain_root_bundle_path is: {{ fullchain_root_bundle_path }}"
  145. - "private_key_path path is: {{ private_key_path }}"
  146. - "------------------------------------------------------------------"
  147. - "gitlab_nginx_config_test_command is: {{ gitlab_nginx_config_test_command }}"
  148. - "healthcheck_url is: {{ healthcheck_url }}"
  149. - "new_fullchain_root_bundle is: {{ new_fullchain_root_bundle }}"
  150. - "fullchain_root_bundle_path is: {{ fullchain_root_bundle_path }}"
  151. ############# copy new cert files ##############
  152. - name: "GitLab hosts: Copy the new_fullchain_root_bundle"
  153. copy:
  154. backup: true
  155. # follow: true
  156. src: "{{ new_fullchain_root_bundle }}"
  157. dest: "{{ fullchain_root_bundle_path }}"
  158. - name: "GitLab: Copy the private key"
  159. copy:
  160. backup: true
  161. src: "{{ new_private_key }}"
  162. dest: "{{ private_key_path }}"
  163. ################# test section ##############
  164. - name: "GitLab: run embedded nginx config check command and register output"
  165. command: "{{ gitlab_nginx_config_test_command }}"
  166. register: gitlab_nginx_config_test_result
  167. ignore_errors: yes
  168. - name: "Dump output from gitlab_nginx_config_test_result"
  169. debug:
  170. msg: "gitlab_nginx_config_test_result is {{ gitlab_nginx_config_test_result }}"
  171. - name: "Dump gitlab_nginx_config_test_result"
  172. debug:
  173. msg: "gitlab_nginx_config_test_result is {{ gitlab_nginx_config_test_result }}"
  174. - name: "dump gitlab_nginx_config_test_result.stderr"
  175. debug:
  176. msg: "{{ gitlab_nginx_config_test_result.stderr }}"
  177. - name: "Set test_outcome to pass if criteria are met"
  178. set_fact:
  179. config_test_outcome: pass
  180. when: '"test is successful" in gitlab_nginx_config_test_result.stderr'
  181. ignore_errors: yes
  182. - debug:
  183. msg: "{{ config_test_outcome }}"
  184. ignore_errors: yes
  185. - name: "Get new cert info by contacting site remotely"
  186. get_certificate:
  187. host: "{{ ansible_host }}"
  188. port: "{{ webserver_port }}"
  189. server_name: "{{ webserver_hostname }}"
  190. register: new_cert_info
  191. delegate_to: localhost
  192. when: config_test_outcome == "pass"
  193. - name: "Show results of remotely connecting to remote site"
  194. debug:
  195. msg:
  196. - "New cert expires in: {{ new_cert_days_left }} days."
  197. vars:
  198. new_cert_days_left: >-
  199. {{ (
  200. (new_cert_info.not_after | ansible.builtin.to_datetime('%Y%m%d%H%M%SZ')) -
  201. (ansible_date_time.iso8601 | ansible.builtin.to_datetime('%Y-%m-%dT%H:%M:%SZ'))
  202. ).days }}
  203. register: new_cert_days_left
  204. when: config_test_outcome == "pass"
  205. - name: "set_fact from this host's test outputs to display at end of playbook run"
  206. set_fact:
  207. changed_site_hostname_and_new_exp_date: "{{ webserver_hostname }} {{ new_cert_days_left }}"
  208. when: config_test_outcome == "pass"
  209. ################## apply changes if successful ###############
  210. - name: "GitLab: restart GitLab embedded Nginx if config test is successful"
  211. command: "{{ gitlab_apply_command }}"
  212. when: config_test_outcome == "pass"
  213. ignore_errors: yes
  214. ########## nginx group ###################################################################################################################
  215. - name: "Nginx group: set group-wide variables"
  216. hosts: nginx
  217. vars:
  218. new_bundle: "include/current/root-chain_domain.pem"
  219. new_fullchain_root_bundle: "include/current/fullchain_bundle_domain.pem"
  220. new_private_key: "include/current/_domain.key"
  221. nginx_config_test_command: "nginx -t -c /etc/nginx/nginx.conf"
  222. nginx_apply_command: "systemctl reload nginx.service"
  223. tasks:
  224. - name: "nginx group: dump group-wide variables"
  225. debug:
  226. msg:
  227. - "host is: {{ ansible_host }}"
  228. - "This is only showing info from GitLab group"
  229. - "------------------------------------------------------------------"
  230. - "new_bundle is: {{ new_bundle }}"
  231. - "new_fullchain_root_bundle is: {{ new_fullchain_root_bundle }}"
  232. - "new_private_key is: {{ new_private_key }}"
  233. - "nginx_config_test_command is: {{ nginx_config_test_command }}"
  234. - "nginx_apply_command is: {{ nginx_apply_command }}"
  235. - name: "nginx group: dump host-specific variables"
  236. debug:
  237. msg:
  238. - "host is: {{ ansible_host }}"
  239. - "This is only showing info from GitLab group"
  240. - "------------------------------------------------------------------"
  241. - "fullchain_root_bundle_path is: {{ fullchain_root_bundle_path }}"
  242. - "private_key is: {{ private_key }}"
  243. - "------------------------------------------------------------------"
  244. - "healthcheck_url is: {{ healthcheck_url }}"
  245. - name: "nginx group: dump OPTIONAL host-specific variables"
  246. debug:
  247. msg:
  248. - "host is: {{ ansible_host }}"
  249. - "------------------------------------------------------------------"
  250. - name: "nginx hosts: Copy the new_fullchain_root_bundle"
  251. copy:
  252. backup: true
  253. # follow: true
  254. src: "{{ new_fullchain_root_bundle }}"
  255. dest: "{{ fullchain_root_bundle_path }}"
  256. - name: "nginx: Copy the private key"
  257. copy:
  258. backup: true
  259. src: "{{ new_private_key }}"
  260. dest: "{{ private_key }}"
  261. ################# test section ##############
  262. - name: "Nginx: run nginx config check command and register output"
  263. command: "{{ nginx_config_test_command }}"
  264. register: nginx_config_test_command_result
  265. ignore_errors: yes
  266. - name: "Dump nginx_config_test_command_result"
  267. debug:
  268. msg: "nginx_config_test_command_result is {{ nginx_config_test_command_result }}"
  269. - name: "dump nginx_config_test_command_result.stderr"
  270. debug:
  271. msg: "{{ nginx_config_test_command_result.stderr }}"
  272. - name: "Set test_outcome to pass if criteria are met"
  273. set_fact:
  274. config_test_outcome: pass
  275. when: '"test is successful" in nginx_config_test_command_result.stderr'
  276. ignore_errors: yes
  277. - debug:
  278. msg: "{{ config_test_outcome }}"
  279. ignore_errors: yes
  280. ################# apply section ##############
  281. - name: "Nginx: reload nginx service if config test is successful"
  282. command: "{{ nginx_apply_command }}"
  283. ignore_errors: yes
  284. when: config_test_outcome == "pass"
  285. ########## others group ###################################################################################################################
  286. - name: "others group"
  287. hosts: others
  288. vars:
  289. new_cert: "include/current/_domain.cert"
  290. new_bundle: "include/current/root-chain_domain.pem"
  291. new_fullchain_root_bundle: "include/current/fullchain_bundle_domain.pem"
  292. new_private_key: "include/current/_domain.key"
  293. tasks:
  294. - name: "others: dump debugging messages"
  295. debug:
  296. msg:
  297. - "host is: {{ ansible_host }}"
  298. - "Group from inventory file is: {{ group_names }} "
  299. - "This host's post_test_command is: {{ post_test_command }}"
  300. - "This host's apply_command is: {{ apply_command }}"
  301. - "------------------------------------------------------------------"
  302. - "fullchain_root_bundle_path: {{ fullchain_root_bundle_path }}"
  303. - "private_key_path: {{ private_key_path }}"
  304. - name: "other hosts: Copy the new_fullchain_root_bundle"
  305. copy:
  306. backup: true
  307. # follow: true
  308. src: "{{ new_fullchain_root_bundle }}"
  309. dest: "{{ fullchain_root_bundle_path }}"
  310. - name: "other: Copy the private key"
  311. copy:
  312. backup: true
  313. src: "{{ new_private_key }}"
  314. dest: "{{ private_key }}"
  315. ################# other section ##############
  316. - name: "other: run other host-specific config check command and register output"
  317. command: "{{ post_test_command }}"
  318. register: other_hosts_config_test_command_result
  319. ignore_errors: yes
  320. - name: "Dump other_hosts_config_test_command_result"
  321. debug:
  322. msg: "other_hosts_config_test_command_result is {{ other_hosts_config_test_command_result }}"
  323. - name: "dump other_hosts_config_test_command_result.stderr"
  324. debug:
  325. msg: "{{ other_hosts_config_test_command_result.stderr }}"
  326. - name: "Set test_outcome to pass if criteria are met"
  327. set_fact:
  328. config_test_outcome: pass
  329. when: '{{ post_test_command_success_output }} in other_hosts_config_test_command_result.stderr'
  330. ignore_errors: yes
  331. - debug:
  332. msg: "{{ config_test_outcome }}"
  333. ignore_errors: yes
  334. ################# other apply section ##############
  335. - name: "others: host-specific command"
  336. command: "{{ apply_command }}"
  337. ignore_errors: yes
  338. when: config_test_outcome == "pass"