main.yml 7.8 KB

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