main.yml 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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: 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. - name: Delete local temp directory
  141. local_action: file path="{{ g_master_certs_mktemp.stdout }}" state=absent
  142. changed_when: False
  143. when: master_certs_missing | bool
  144. - name: Lookup default group for ansible_ssh_user
  145. command: "/usr/bin/id -g {{ ansible_ssh_user | quote }}"
  146. changed_when: false
  147. register: _ansible_ssh_user_gid
  148. - set_fact:
  149. client_users: "{{ [ansible_ssh_user, 'root'] | unique }}"
  150. - name: Create the client config dir(s)
  151. file:
  152. path: "~{{ item }}/.kube"
  153. state: directory
  154. mode: 0700
  155. owner: "{{ item }}"
  156. group: "{{ 'root' if item == 'root' else _ansible_ssh_user_gid.stdout }}"
  157. with_items: "{{ client_users }}"
  158. # TODO: Update this file if the contents of the source file are not present in
  159. # the dest file, will need to make sure to ignore things that could be added
  160. - name: Copy the admin client config(s)
  161. copy:
  162. src: "{{ openshift_master_config_dir }}/admin.kubeconfig"
  163. dest: "~{{ item }}/.kube/config"
  164. remote_src: yes
  165. force: "{{ openshift_certificates_redeploy | default(false) }}"
  166. with_items: "{{ client_users }}"
  167. - name: Update the permissions on the admin client config(s)
  168. file:
  169. path: "~{{ item }}/.kube/config"
  170. state: file
  171. mode: 0700
  172. owner: "{{ item }}"
  173. group: "{{ 'root' if item == 'root' else _ansible_ssh_user_gid.stdout }}"
  174. with_items: "{{ client_users }}"
  175. # Ensure ca-bundle exists for 3.2+ configuration
  176. - name: Check for ca-bundle.crt
  177. stat:
  178. path: "{{ openshift.common.config_base }}/master/ca-bundle.crt"
  179. register: ca_bundle_stat
  180. failed_when: false
  181. - name: Check for ca.crt
  182. stat:
  183. path: "{{ openshift.common.config_base }}/master/ca.crt"
  184. register: ca_crt_stat
  185. failed_when: false
  186. - name: Migrate ca.crt to ca-bundle.crt
  187. command: mv ca.crt ca-bundle.crt
  188. args:
  189. chdir: "{{ openshift.common.config_base }}/master"
  190. when: ca_crt_stat.stat.isreg and not ca_bundle_stat.stat.exists
  191. - name: Link ca.crt to ca-bundle.crt
  192. file:
  193. src: "{{ openshift.common.config_base }}/master/ca-bundle.crt"
  194. path: "{{ openshift.common.config_base }}/master/ca.crt"
  195. state: link
  196. when: ca_crt_stat.stat.isreg and not ca_bundle_stat.stat.exists