main.yml 9.0 KB

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