main.yml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. ---
  2. - fail:
  3. msg: "Both 'certfile' and 'keyfile' keys must be supplied when configuring openshift_master_ca_certificate"
  4. when: openshift_master_ca_certificate is defined and ('certfile' not in openshift_master_ca_certificate or 'keyfile' not in openshift_master_ca_certificate)
  5. - name: Install the base package for admin tooling
  6. package:
  7. name: "{{ openshift_service_type }}{{ openshift_pkg_version | default('') | lib_utils_oo_image_tag_to_rpm_version(include_dash=True) }}"
  8. state: present
  9. register: install_result
  10. until: install_result is succeeded
  11. delegate_to: "{{ openshift_ca_host }}"
  12. run_once: true
  13. - name: Reload generated facts
  14. openshift_facts:
  15. when:
  16. - hostvars[openshift_ca_host].install_result | default({'changed':false}) is changed
  17. - name: Create openshift_ca_config_dir if it does not exist
  18. file:
  19. path: "{{ openshift_ca_config_dir }}"
  20. state: directory
  21. delegate_to: "{{ openshift_ca_host }}"
  22. run_once: true
  23. - name: Determine if CA must be created
  24. stat:
  25. path: "{{ openshift_ca_config_dir }}/{{ item }}"
  26. get_checksum: false
  27. get_attributes: false
  28. get_mime: false
  29. register: g_master_ca_stat_result
  30. with_items:
  31. - ca-bundle.crt
  32. - ca.crt
  33. - ca.key
  34. delegate_to: "{{ openshift_ca_host }}"
  35. run_once: true
  36. - name: Determine if front-proxy CA must be created
  37. stat:
  38. path: "{{ openshift_ca_config_dir }}/{{ item }}"
  39. get_checksum: false
  40. get_attributes: false
  41. get_mime: false
  42. register: g_master_front_proxy_ca_stat_result
  43. with_items:
  44. - front-proxy-ca.crt
  45. - front-proxy-ca.key
  46. delegate_to: "{{ openshift_ca_host }}"
  47. run_once: true
  48. - set_fact:
  49. master_ca_missing: "{{ False in (g_master_ca_stat_result.results
  50. | lib_utils_oo_collect(attribute='stat.exists')
  51. | list) }}"
  52. master_front_proxy_ca_missing: "{{ False in (g_master_front_proxy_ca_stat_result.results
  53. | lib_utils_oo_collect(attribute='stat.exists')
  54. | list) }}"
  55. run_once: true
  56. - name: Retain original serviceaccount keys
  57. copy:
  58. src: "{{ item }}"
  59. dest: "{{ item }}.keep"
  60. remote_src: true
  61. with_items:
  62. - "{{ openshift_ca_config_dir }}/serviceaccounts.private.key"
  63. - "{{ openshift_ca_config_dir }}/serviceaccounts.public.key"
  64. when: openshift_certificates_redeploy | default(false) | bool
  65. - name: Deploy master ca certificate
  66. copy:
  67. src: "{{ item.src }}"
  68. dest: "{{ openshift_ca_config_dir }}/{{ item.dest }}"
  69. force: no
  70. with_items:
  71. - src: "{{ (openshift_master_ca_certificate | default({'certfile':none})).certfile }}"
  72. dest: ca.crt
  73. - src: "{{ (openshift_master_ca_certificate | default({'keyfile':none})).keyfile }}"
  74. dest: ca.key
  75. when: openshift_master_ca_certificate is defined
  76. delegate_to: "{{ openshift_ca_host }}"
  77. run_once: true
  78. # Upload additional CA if necessary
  79. - name: Deploy additional ca
  80. copy:
  81. src: "{{ openshift_additional_ca }}"
  82. dest: "{{ openshift.common.config_base }}/master/additional_ca.crt"
  83. mode: 0644
  84. when: openshift_additional_ca is defined
  85. - name: Create ca serial
  86. copy:
  87. content: "00"
  88. dest: "{{ openshift_ca_config_dir }}/ca.serial.txt"
  89. force: "{{ openshift_certificates_redeploy | default(false) | bool }}"
  90. when: openshift_master_ca_certificate is defined
  91. delegate_to: "{{ openshift_ca_host }}"
  92. run_once: true
  93. - find:
  94. paths: "{{ openshift.common.config_base }}/master/legacy-ca/"
  95. patterns: ".*-ca.crt"
  96. use_regex: true
  97. register: g_master_legacy_ca_result
  98. # This should NOT replace the CA due to --overwrite=false when a CA already exists.
  99. - name: Create the front-proxy CA if it does not already exist
  100. command: >
  101. {{ hostvars[openshift_ca_host]['first_master_client_binary'] }} adm ca create-signer-cert
  102. --cert="{{ openshift_ca_config_dir }}/front-proxy-ca.crt"
  103. --key="{{ openshift_ca_config_dir }}/front-proxy-ca.key"
  104. --serial="{{ openshift_ca_config_dir }}/ca.serial.txt"
  105. --expire-days={{ openshift_ca_cert_expire_days }}
  106. --overwrite=false
  107. when: master_front_proxy_ca_missing | bool or openshift_certificates_redeploy | default(false) | bool
  108. delegate_to: "{{ openshift_ca_host }}"
  109. run_once: true
  110. # This should NOT replace the CA due to --overwrite=false when a CA already exists.
  111. - name: Create the master certificates if they do not already exist
  112. command: >
  113. {{ hostvars[openshift_ca_host]['first_master_client_binary'] }} adm ca create-master-certs
  114. {% for named_ca_certificate in openshift.master.named_certificates | default([]) | lib_utils_oo_collect('cafile') %}
  115. --certificate-authority {{ named_ca_certificate }}
  116. {% endfor %}
  117. {% for legacy_ca_certificate in g_master_legacy_ca_result.files | default([]) | lib_utils_oo_collect('path') %}
  118. --certificate-authority {{ legacy_ca_certificate }}
  119. {% endfor %}
  120. {% if openshift_additional_ca is defined %}
  121. --certificate-authority {{ openshift.common.config_base }}/master/additional_ca.crt
  122. {% endif %}
  123. --hostnames={{ hostvars[openshift_ca_host].openshift.common.all_hostnames | join(',') }}
  124. --master={{ openshift.master.api_url }}
  125. --public-master={{ openshift.master.public_api_url }}
  126. --cert-dir={{ openshift_ca_config_dir }}
  127. --expire-days={{ openshift_master_cert_expire_days }}
  128. --signer-expire-days={{ openshift_ca_cert_expire_days }}
  129. --overwrite=false
  130. when: master_ca_missing | bool or openshift_certificates_redeploy | default(false) | bool
  131. delegate_to: "{{ openshift_ca_host }}"
  132. run_once: true
  133. # Create client-ca-bundle.crt containing old and new OpenShift CA
  134. # certificates. This bundle will be used when rolling the OpenShift CA
  135. # certificate.
  136. - name: Create client-ca-bundle.crt
  137. block:
  138. - command: mktemp -d /tmp/openshift-ansible-XXXXXX
  139. register: openshift_ca_clientconfig_tmpdir
  140. delegate_to: "{{ openshift_ca_host }}"
  141. - copy:
  142. src: "{{ item }}"
  143. dest: "{{ openshift_ca_clientconfig_tmpdir.stdout }}/"
  144. remote_src: true
  145. with_items: "{{ g_master_legacy_ca_result.files | default([]) | lib_utils_oo_collect('path') }}"
  146. delegate_to: "{{ openshift_ca_host }}"
  147. run_once: true
  148. - copy:
  149. src: "{{ openshift_ca_config_dir }}/ca.crt"
  150. dest: "{{ openshift_ca_clientconfig_tmpdir.stdout }}/"
  151. remote_src: true
  152. delegate_to: "{{ openshift_ca_host }}"
  153. run_once: true
  154. - assemble:
  155. src: "{{ openshift_ca_clientconfig_tmpdir.stdout }}"
  156. dest: "{{ openshift_ca_config_dir }}/client-ca-bundle.crt"
  157. mode: 0644
  158. owner: root
  159. group: root
  160. delegate_to: "{{ openshift_ca_host }}"
  161. run_once: true
  162. - name: Test local loopback context
  163. command: >
  164. {{ hostvars[openshift_ca_host]['first_master_client_binary'] }} config view
  165. --config={{ openshift_master_loopback_config }}
  166. changed_when: false
  167. register: loopback_config
  168. delegate_to: "{{ openshift_ca_host }}"
  169. run_once: true
  170. # create-api-client-config generates a ca.crt file which will
  171. # overwrite the OpenShift CA certificate. Generate the loopback
  172. # kubeconfig in a temporary directory and then copy files into the
  173. # master config dir to avoid overwriting ca.crt.
  174. - block:
  175. - name: Create temp directory for loopback master client config
  176. command: mktemp -d /tmp/openshift-ansible-XXXXXX
  177. register: openshift_ca_loopback_tmpdir
  178. - name: Generate the loopback master client config
  179. command: >
  180. {{ hostvars[openshift_ca_host]['first_master_client_binary'] }} adm create-api-client-config
  181. --certificate-authority={{ openshift_ca_cert }}
  182. {% for named_ca_certificate in openshift.master.named_certificates | default([]) | lib_utils_oo_collect('cafile') %}
  183. --certificate-authority {{ named_ca_certificate }}
  184. {% endfor %}
  185. --client-dir={{ openshift_ca_loopback_tmpdir.stdout }}
  186. --groups=system:masters,system:openshift-master
  187. --master={{ hostvars[openshift_ca_host].openshift.master.loopback_api_url }}
  188. --public-master={{ hostvars[openshift_ca_host].openshift.master.loopback_api_url }}
  189. --signer-cert={{ openshift_ca_cert }}
  190. --signer-key={{ openshift_ca_key }}
  191. --signer-serial={{ openshift_ca_serial }}
  192. --user=system:openshift-master
  193. --basename=openshift-master
  194. --expire-days={{ openshift_master_cert_expire_days }}
  195. - name: Copy generated loopback master client config to master config dir
  196. copy:
  197. src: "{{ openshift_ca_loopback_tmpdir.stdout }}/{{ item }}"
  198. dest: "{{ openshift_ca_config_dir }}"
  199. remote_src: true
  200. with_items:
  201. - openshift-master.crt
  202. - openshift-master.key
  203. - openshift-master.kubeconfig
  204. - name: Delete temp directory
  205. file:
  206. name: "{{ openshift_ca_loopback_tmpdir.stdout }}"
  207. state: absent
  208. when: loopback_context_string not in loopback_config.stdout
  209. delegate_to: "{{ openshift_ca_host }}"
  210. run_once: true
  211. # create-api-client-config generates a ca.crt file which will
  212. # overwrite the OpenShift CA certificate. Generate the loopback
  213. # kubeconfig in a temporary directory and then copy files into the
  214. # master config dir to avoid overwriting ca.crt.
  215. - block:
  216. - name: Create temp directory for loopback master client config
  217. command: mktemp -d /tmp/openshift-ansible-XXXXXX
  218. register: openshift_ca_loopback_tmpdir
  219. - name: Generate the aggregator api-client config
  220. command: >
  221. {{ hostvars[openshift_ca_host]['first_master_client_binary'] }} adm create-api-client-config
  222. --certificate-authority={{ openshift_ca_cert }}
  223. {% for named_ca_certificate in openshift.master.named_certificates | default([]) | lib_utils_oo_collect('cafile') %}
  224. --certificate-authority {{ named_ca_certificate }}
  225. {% endfor %}
  226. --client-dir={{ openshift_ca_loopback_tmpdir.stdout }}
  227. --user=aggregator-front-proxy
  228. --signer-cert="{{ openshift_ca_config_dir }}/front-proxy-ca.crt"
  229. --signer-key="{{ openshift_ca_config_dir }}/front-proxy-ca.key"
  230. --signer-serial={{ openshift_ca_serial }}
  231. --expire-days={{ openshift_master_cert_expire_days }}
  232. - name: Copy generated loopback master client config to master config dir
  233. copy:
  234. src: "{{ openshift_ca_loopback_tmpdir.stdout }}/{{ item }}"
  235. dest: "{{ openshift_ca_config_dir }}"
  236. remote_src: true
  237. with_items:
  238. - aggregator-front-proxy.crt
  239. - aggregator-front-proxy.key
  240. - aggregator-front-proxy.kubeconfig
  241. - name: Delete temp directory
  242. file:
  243. name: "{{ openshift_ca_loopback_tmpdir.stdout }}"
  244. state: absent
  245. delegate_to: "{{ openshift_ca_host }}"
  246. run_once: true
  247. - name: Restore original serviceaccount keys
  248. copy:
  249. src: "{{ item }}.keep"
  250. dest: "{{ item }}"
  251. remote_src: true
  252. with_items:
  253. - "{{ openshift_ca_config_dir }}/serviceaccounts.private.key"
  254. - "{{ openshift_ca_config_dir }}/serviceaccounts.public.key"
  255. when: openshift_certificates_redeploy | default(false) | bool
  256. - name: Remove backup serviceaccount keys
  257. file:
  258. path: "{{ item }}.keep"
  259. state: absent
  260. with_items:
  261. - "{{ openshift_ca_config_dir }}/serviceaccounts.private.key"
  262. - "{{ openshift_ca_config_dir }}/serviceaccounts.public.key"
  263. when: openshift_certificates_redeploy | default(false) | bool