main.yml 6.6 KB

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