main.yml 8.8 KB

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