main.yml 8.4 KB

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