main.yml 8.1 KB

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