main.yml 8.8 KB

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