main.yml 11 KB

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