main.yml 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. ---
  2. - name: Check status of master certificates
  3. stat:
  4. path: "{{ openshift_master_config_dir }}/{{ item }}"
  5. with_items:
  6. - admin.crt
  7. - ca.crt
  8. - ca-bundle.crt
  9. - master.kubelet-client.crt
  10. - master.proxy-client.crt
  11. - master.server.crt
  12. - openshift-master.crt
  13. - service-signer.crt
  14. register: g_master_cert_stat_result
  15. when: not openshift_certificates_redeploy | default(false) | bool
  16. - set_fact:
  17. master_certs_missing: "{{ true if openshift_certificates_redeploy | default(false) | bool
  18. else (False in (g_master_cert_stat_result.results
  19. | default({})
  20. | lib_utils_oo_collect(attribute='stat.exists')
  21. | list)) }}"
  22. - name: Ensure the generated_configs directory present
  23. file:
  24. path: "{{ openshift_master_generated_config_dir }}"
  25. state: directory
  26. mode: 0700
  27. when: master_certs_missing | bool and inventory_hostname != openshift_ca_host
  28. delegate_to: "{{ openshift_ca_host }}"
  29. - find:
  30. paths: "{{ openshift_master_config_dir }}/legacy-ca/"
  31. patterns: ".*-ca.crt"
  32. use_regex: true
  33. register: g_master_legacy_ca_result
  34. delegate_to: "{{ openshift_ca_host }}"
  35. - name: Create the master server certificate
  36. command: >
  37. {{ hostvars[openshift_ca_host]['first_master_client_binary'] }} adm ca create-server-cert
  38. {% for named_ca_certificate in openshift.master.named_certificates | default([]) | lib_utils_oo_collect('cafile') %}
  39. --certificate-authority {{ named_ca_certificate }}
  40. {% endfor %}
  41. {% for legacy_ca_certificate in g_master_legacy_ca_result.files | default([]) | lib_utils_oo_collect('path') %}
  42. --certificate-authority {{ legacy_ca_certificate }}
  43. {% endfor %}
  44. --hostnames={{ hostvars[item].openshift.common.all_hostnames | join(',') }}
  45. --cert={{ openshift_generated_configs_dir }}/master-{{ hostvars[item].openshift.common.hostname }}/master.server.crt
  46. --key={{ openshift_generated_configs_dir }}/master-{{ hostvars[item].openshift.common.hostname }}/master.server.key
  47. --expire-days={{ openshift_master_cert_expire_days }}
  48. --signer-cert={{ openshift_ca_cert }}
  49. --signer-key={{ openshift_ca_key }}
  50. --signer-serial={{ openshift_ca_serial }}
  51. --overwrite=false
  52. when: item != openshift_ca_host
  53. with_items: "{{ hostvars
  54. | lib_utils_oo_select_keys(groups['oo_masters_to_config'])
  55. | lib_utils_oo_collect(attribute='inventory_hostname', filters={'master_certs_missing':True}) }}"
  56. delegate_to: "{{ openshift_ca_host }}"
  57. run_once: true
  58. - name: Generate the loopback master client config
  59. command: >
  60. {{ hostvars[openshift_ca_host]['first_master_client_binary'] }} adm create-api-client-config
  61. --certificate-authority={{ openshift_ca_cert }}
  62. {% for named_ca_certificate in openshift.master.named_certificates | default([]) | lib_utils_oo_collect('cafile') %}
  63. --certificate-authority {{ named_ca_certificate }}
  64. {% endfor %}
  65. --client-dir={{ openshift_generated_configs_dir }}/master-{{ hostvars[item].openshift.common.hostname }}
  66. --groups=system:masters,system:openshift-master
  67. --master={{ hostvars[item].openshift.master.loopback_api_url }}
  68. --public-master={{ hostvars[item].openshift.master.loopback_api_url }}
  69. --signer-cert={{ openshift_ca_cert }}
  70. --signer-key={{ openshift_ca_key }}
  71. --signer-serial={{ openshift_ca_serial }}
  72. --user=system:openshift-master
  73. --basename=openshift-master
  74. --expire-days={{ openshift_master_cert_expire_days }}
  75. args:
  76. creates: "{{ openshift_generated_configs_dir }}/master-{{ hostvars[item].openshift.common.hostname }}/openshift-master.kubeconfig"
  77. with_items: "{{ hostvars
  78. | lib_utils_oo_select_keys(groups['oo_masters_to_config'])
  79. | lib_utils_oo_collect(attribute='inventory_hostname', filters={'master_certs_missing':True}) }}"
  80. when: item != openshift_ca_host
  81. delegate_to: "{{ openshift_ca_host }}"
  82. run_once: true
  83. - file:
  84. src: "{{ openshift_master_config_dir }}/{{ item }}"
  85. dest: "{{ openshift_master_generated_config_dir }}/{{ item }}"
  86. state: hard
  87. force: true
  88. with_items:
  89. # certificates_to_synchronize is a custom filter in lib_utils
  90. - "{{ hostvars[inventory_hostname] | certificates_to_synchronize }}"
  91. when: master_certs_missing | bool and inventory_hostname != openshift_ca_host
  92. delegate_to: "{{ openshift_ca_host }}"
  93. - name: Remove generated etcd client certs when using external etcd
  94. file:
  95. path: "{{ openshift_master_generated_config_dir }}/{{ item }}"
  96. state: absent
  97. when: openshift_master_etcd_hosts | length > 0
  98. with_items:
  99. - master.etcd-client.crt
  100. - master.etcd-client.key
  101. delegate_to: "{{ openshift_ca_host }}"
  102. - name: Create local temp directory for syncing certs
  103. local_action: command mktemp -d /tmp/openshift-ansible-XXXXXXX
  104. register: g_master_certs_mktemp
  105. changed_when: False
  106. when: master_certs_missing | bool
  107. - name: Chmod local temp directory for syncing certs
  108. local_action: command chmod 777 "{{ g_master_certs_mktemp.stdout }}"
  109. changed_when: False
  110. when: master_certs_missing | bool
  111. - name: Create a tarball of the master certs
  112. command: >
  113. tar -czvf {{ openshift_master_generated_config_dir }}.tgz
  114. -C {{ openshift_master_generated_config_dir }} .
  115. args:
  116. creates: "{{ openshift_master_generated_config_dir }}.tgz"
  117. when: master_certs_missing | bool and inventory_hostname != openshift_ca_host
  118. delegate_to: "{{ openshift_ca_host }}"
  119. - name: Retrieve the master cert tarball from the master
  120. fetch:
  121. src: "{{ openshift_master_generated_config_dir }}.tgz"
  122. dest: "{{ g_master_certs_mktemp.stdout }}/"
  123. flat: yes
  124. fail_on_missing: yes
  125. validate_checksum: yes
  126. when: master_certs_missing | bool and inventory_hostname != openshift_ca_host
  127. delegate_to: "{{ openshift_ca_host }}"
  128. - name: Ensure certificate directory exists
  129. file:
  130. path: "{{ openshift_master_config_dir }}"
  131. state: directory
  132. when: master_certs_missing | bool and inventory_hostname != openshift_ca_host
  133. - name: Unarchive the tarball on the master
  134. unarchive:
  135. src: "{{ g_master_certs_mktemp.stdout }}/{{ openshift_master_cert_subdir }}.tgz"
  136. dest: "{{ openshift_master_config_dir }}"
  137. when: master_certs_missing | bool and inventory_hostname != openshift_ca_host
  138. - name: Delete local temp directory
  139. local_action: file path="{{ g_master_certs_mktemp.stdout }}" state=absent
  140. changed_when: False
  141. when: master_certs_missing | bool
  142. - name: Lookup default group for ansible_ssh_user
  143. command: "/usr/bin/id -g {{ ansible_ssh_user | quote }}"
  144. changed_when: false
  145. register: _ansible_ssh_user_gid
  146. - set_fact:
  147. client_users: "{{ [ansible_ssh_user, 'root'] | unique }}"
  148. - name: Create the client config dir(s)
  149. file:
  150. path: "~{{ item }}/.kube"
  151. state: directory
  152. mode: 0700
  153. owner: "{{ item }}"
  154. group: "{{ 'root' if item == 'root' else _ansible_ssh_user_gid.stdout }}"
  155. with_items: "{{ client_users }}"
  156. # TODO: Update this file if the contents of the source file are not present in
  157. # the dest file, will need to make sure to ignore things that could be added
  158. - name: Copy the admin client config(s)
  159. copy:
  160. src: "{{ openshift_master_config_dir }}/admin.kubeconfig"
  161. dest: "~{{ item }}/.kube/config"
  162. remote_src: yes
  163. force: "{{ openshift_certificates_redeploy | default(false) }}"
  164. with_items: "{{ client_users }}"
  165. - name: Update the permissions on the admin client config(s)
  166. file:
  167. path: "~{{ item }}/.kube/config"
  168. state: file
  169. mode: 0700
  170. owner: "{{ item }}"
  171. group: "{{ 'root' if item == 'root' else _ansible_ssh_user_gid.stdout }}"
  172. with_items: "{{ client_users }}"
  173. # Ensure ca-bundle exists for 3.2+ configuration
  174. - name: Check for ca-bundle.crt
  175. stat:
  176. path: "{{ openshift.common.config_base }}/master/ca-bundle.crt"
  177. register: ca_bundle_stat
  178. failed_when: false
  179. - name: Check for ca.crt
  180. stat:
  181. path: "{{ openshift.common.config_base }}/master/ca.crt"
  182. register: ca_crt_stat
  183. failed_when: false
  184. - name: Migrate ca.crt to ca-bundle.crt
  185. command: mv ca.crt ca-bundle.crt
  186. args:
  187. chdir: "{{ openshift.common.config_base }}/master"
  188. when: ca_crt_stat.stat.isreg and not ca_bundle_stat.stat.exists
  189. - name: Link ca.crt to ca-bundle.crt
  190. file:
  191. src: "{{ openshift.common.config_base }}/master/ca-bundle.crt"
  192. path: "{{ openshift.common.config_base }}/master/ca.crt"
  193. state: link
  194. when: ca_crt_stat.stat.isreg and not ca_bundle_stat.stat.exists