main.yml 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.common.service_type }}{{ openshift_pkg_version | default('') | oo_image_tag_to_rpm_version(include_dash=True) }}"
  11. state: present
  12. when: not openshift.common.is_containerized | bool
  13. register: install_result
  14. delegate_to: "{{ openshift_ca_host }}"
  15. run_once: true
  16. - name: Reload generated facts
  17. openshift_facts:
  18. when: hostvars[openshift_ca_host].install_result | changed
  19. - name: Create openshift_ca_config_dir if it does not exist
  20. file:
  21. path: "{{ openshift_ca_config_dir }}"
  22. state: directory
  23. delegate_to: "{{ openshift_ca_host }}"
  24. run_once: true
  25. - name: Determine if CA must be created
  26. stat:
  27. path: "{{ openshift_ca_config_dir }}/{{ item }}"
  28. register: g_master_ca_stat_result
  29. with_items:
  30. - ca-bundle.crt
  31. - ca.crt
  32. - ca.key
  33. delegate_to: "{{ openshift_ca_host }}"
  34. run_once: true
  35. - set_fact:
  36. master_ca_missing: "{{ False in (g_master_ca_stat_result.results
  37. | oo_collect(attribute='stat.exists')
  38. | list) }}"
  39. run_once: true
  40. - name: Retain original serviceaccount keys
  41. copy:
  42. src: "{{ item }}"
  43. dest: "{{ item }}.keep"
  44. remote_src: true
  45. with_items:
  46. - "{{ openshift_ca_config_dir }}/serviceaccounts.private.key"
  47. - "{{ openshift_ca_config_dir }}/serviceaccounts.public.key"
  48. when: openshift_certificates_redeploy | default(false) | bool
  49. - name: Deploy master ca certificate
  50. copy:
  51. src: "{{ item.src }}"
  52. dest: "{{ openshift_ca_config_dir }}/{{ item.dest }}"
  53. force: no
  54. with_items:
  55. - src: "{{ (openshift_master_ca_certificate | default({'certfile':none})).certfile }}"
  56. dest: ca.crt
  57. - src: "{{ (openshift_master_ca_certificate | default({'keyfile':none})).keyfile }}"
  58. dest: ca.key
  59. when: openshift_master_ca_certificate is defined
  60. delegate_to: "{{ openshift_ca_host }}"
  61. run_once: true
  62. - name: Create ca serial
  63. copy:
  64. content: "00"
  65. dest: "{{ openshift_ca_config_dir }}/ca.serial.txt"
  66. force: "{{ openshift_certificates_redeploy | default(false) | bool }}"
  67. when: openshift_master_ca_certificate is defined
  68. delegate_to: "{{ openshift_ca_host }}"
  69. run_once: true
  70. - find:
  71. paths: "{{ openshift.common.config_base }}/master/legacy-ca/"
  72. patterns: ".*-ca.crt"
  73. use_regex: true
  74. register: g_master_legacy_ca_result
  75. # This should NOT replace the CA due to --overwrite=false when a CA already exists.
  76. - name: Create the master certificates if they do not already exist
  77. command: >
  78. {{ hostvars[openshift_ca_host].openshift.common.client_binary }} adm ca create-master-certs
  79. {% for named_ca_certificate in openshift.master.named_certificates | default([]) | oo_collect('cafile') %}
  80. --certificate-authority {{ named_ca_certificate }}
  81. {% endfor %}
  82. {% for legacy_ca_certificate in g_master_legacy_ca_result.files | default([]) | oo_collect('path') %}
  83. --certificate-authority {{ legacy_ca_certificate }}
  84. {% endfor %}
  85. --hostnames={{ hostvars[openshift_ca_host].openshift.common.all_hostnames | join(',') }}
  86. --master={{ openshift.master.api_url }}
  87. --public-master={{ openshift.master.public_api_url }}
  88. --cert-dir={{ openshift_ca_config_dir }}
  89. {% if openshift_version | oo_version_gte_3_5_or_1_5(openshift.common.deployment_type) | bool %}
  90. --expire-days={{ openshift_master_cert_expire_days }}
  91. --signer-expire-days={{ openshift_ca_cert_expire_days }}
  92. {% endif %}
  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([]) | 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].openshift.common.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].openshift.common.client_binary }} adm create-api-client-config
  145. --certificate-authority={{ openshift_ca_cert }}
  146. {% for named_ca_certificate in openshift.master.named_certificates | default([]) | 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. {% if openshift_version | oo_version_gte_3_5_or_1_5(openshift.common.deployment_type) | bool %}
  159. --expire-days={{ openshift_master_cert_expire_days }}
  160. {% endif %}
  161. - name: Copy generated loopback master client config to master config dir
  162. copy:
  163. src: "{{ openshift_ca_loopback_tmpdir.stdout }}/{{ item }}"
  164. dest: "{{ openshift_ca_config_dir }}"
  165. remote_src: true
  166. with_items:
  167. - openshift-master.crt
  168. - openshift-master.key
  169. - openshift-master.kubeconfig
  170. - name: Delete temp directory
  171. file:
  172. name: "{{ openshift_ca_loopback_tmpdir.stdout }}"
  173. state: absent
  174. when: loopback_context_string not in loopback_config.stdout
  175. delegate_to: "{{ openshift_ca_host }}"
  176. run_once: true
  177. - name: Restore original serviceaccount keys
  178. copy:
  179. src: "{{ item }}.keep"
  180. dest: "{{ item }}"
  181. remote_src: true
  182. with_items:
  183. - "{{ openshift_ca_config_dir }}/serviceaccounts.private.key"
  184. - "{{ openshift_ca_config_dir }}/serviceaccounts.public.key"
  185. when: openshift_certificates_redeploy | default(false) | bool
  186. - name: Remove backup serviceaccount keys
  187. file:
  188. path: "{{ item }}.keep"
  189. state: absent
  190. with_items:
  191. - "{{ openshift_ca_config_dir }}/serviceaccounts.private.key"
  192. - "{{ openshift_ca_config_dir }}/serviceaccounts.public.key"
  193. when: openshift_certificates_redeploy | default(false) | bool