main.yml 8.7 KB

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