openshift-ca.yml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. ---
  2. - name: Verify OpenShift version is greater than or equal to 1.2 or 3.2
  3. hosts: oo_first_master
  4. tasks:
  5. - fail:
  6. msg: "The current OpenShift version is less than 1.2/3.2 and does not support CA bundles."
  7. when: not openshift.common.version_gte_3_2_or_1_2 | bool
  8. - name: Check cert expirys
  9. hosts: oo_nodes_to_config:oo_masters_to_config:oo_etcd_to_config
  10. vars:
  11. openshift_certificate_expiry_show_all: yes
  12. roles:
  13. # Sets 'check_results' per host which contains health status for
  14. # etcd, master and node certificates. We will use 'check_results'
  15. # to determine if any certificates were expired prior to running
  16. # this playbook. Service restarts will be skipped if any
  17. # certificates were previously expired.
  18. - role: openshift_certificate_expiry
  19. # Update master config when ca-bundle not referenced. Services will be
  20. # restarted below after new CA certificate has been distributed.
  21. - name: Ensure ca-bundle.crt is referenced in master configuration
  22. hosts: oo_masters_to_config
  23. tasks:
  24. - slurp:
  25. src: "{{ openshift.common.config_base }}/master/master-config.yaml"
  26. register: g_master_config_output
  27. - modify_yaml:
  28. dest: "{{ openshift.common.config_base }}/master/master-config.yaml"
  29. yaml_key: kubeletClientInfo.ca
  30. yaml_value: ca-bundle.crt
  31. when: (g_master_config_output.content|b64decode|from_yaml).kubeletClientInfo.ca != 'ca-bundle.crt'
  32. - modify_yaml:
  33. dest: "{{ openshift.common.config_base }}/master/master-config.yaml"
  34. yaml_key: serviceAccountConfig.masterCA
  35. yaml_value: ca-bundle.crt
  36. when: (g_master_config_output.content|b64decode|from_yaml).serviceAccountConfig.masterCA != 'ca-bundle.crt'
  37. - modify_yaml:
  38. dest: "{{ openshift.common.config_base }}/master/master-config.yaml"
  39. yaml_key: oauthConfig.masterCA
  40. yaml_value: ca-bundle.crt
  41. when: (g_master_config_output.content|b64decode|from_yaml).oauthConfig.masterCA != 'ca-bundle.crt'
  42. - modify_yaml:
  43. dest: "{{ openshift.common.config_base }}/master/master-config.yaml"
  44. yaml_key: etcdClientInfo.ca
  45. yaml_value: ca-bundle.crt
  46. when:
  47. - groups.oo_etcd_to_config | default([]) | length == 0
  48. - (g_master_config_output.content|b64decode|from_yaml).etcdClientInfo.ca != 'ca-bundle.crt'
  49. - modify_yaml:
  50. dest: "{{ openshift.common.config_base }}/master/master-config.yaml"
  51. yaml_key: etcdConfig.peerServingInfo.clientCA
  52. yaml_value: ca-bundle.crt
  53. when:
  54. - groups.oo_etcd_to_config | default([]) | length == 0
  55. - (g_master_config_output.content|b64decode|from_yaml).etcdConfig.peerServingInfo.clientCA != 'ca-bundle.crt'
  56. - modify_yaml:
  57. dest: "{{ openshift.common.config_base }}/master/master-config.yaml"
  58. yaml_key: etcdConfig.servingInfo.clientCA
  59. yaml_value: ca-bundle.crt
  60. when:
  61. - groups.oo_etcd_to_config | default([]) | length == 0
  62. - (g_master_config_output.content|b64decode|from_yaml).etcdConfig.servingInfo.clientCA != 'ca-bundle.crt'
  63. # Set servingInfo.clientCA to client-ca-bundle.crt in order to roll the CA certificate.
  64. # This change will be reverted in playbooks/byo/openshift-cluster/redeploy-certificates.yml
  65. - modify_yaml:
  66. dest: "{{ openshift.common.config_base }}/master/master-config.yaml"
  67. yaml_key: servingInfo.clientCA
  68. yaml_value: client-ca-bundle.crt
  69. when: (g_master_config_output.content|b64decode|from_yaml).servingInfo.clientCA != 'client-ca-bundle.crt'
  70. - name: Copy current OpenShift CA to legacy directory
  71. hosts: oo_masters_to_config
  72. pre_tasks:
  73. - name: Create legacy-ca directory
  74. file:
  75. path: "{{ openshift.common.config_base }}/master/legacy-ca"
  76. state: directory
  77. mode: 0700
  78. owner: root
  79. group: root
  80. - command: mktemp -u XXXXXX
  81. register: g_legacy_ca_mktemp
  82. changed_when: false
  83. # Copy CA certificate, key, serial and bundle to legacy-ca with a
  84. # prefix generated by mktemp, ie. XXXXXX-ca.crt.
  85. #
  86. # The following roles will pick up all CA certificates matching
  87. # /.*-ca.crt/ in the legacy-ca directory and ensure they are present
  88. # in the OpenShift CA bundle.
  89. # - openshift_ca
  90. # - openshift_master_certificates
  91. # - openshift_node_certificates
  92. - name: Copy current OpenShift CA to legacy directory
  93. copy:
  94. src: "{{ openshift.common.config_base }}/master/{{ item }}"
  95. dest: "{{ openshift.common.config_base }}/master/legacy-ca/{{ g_legacy_ca_mktemp.stdout }}-{{ item }}"
  96. remote_src: true
  97. # It is possible that redeploying failed and files may be missing.
  98. # Ignore errors in this case. Files should have been copied to
  99. # legacy-ca directory in previous run.
  100. ignore_errors: true
  101. with_items:
  102. - "ca.crt"
  103. - "ca.key"
  104. - "ca.serial.txt"
  105. - "ca-bundle.crt"
  106. - name: Create temporary directory for creating new CA certificate
  107. hosts: oo_first_master
  108. tasks:
  109. - name: Create temporary directory for creating new CA certificate
  110. command: >
  111. mktemp -d /tmp/openshift-ansible-XXXXXXX
  112. register: g_new_openshift_ca_mktemp
  113. changed_when: false
  114. - name: Create OpenShift CA
  115. hosts: oo_first_master
  116. vars:
  117. # Set openshift_ca_config_dir to a temporary directory where CA
  118. # will be created. We'll replace the existing CA with the CA
  119. # created in the temporary directory.
  120. openshift_ca_config_dir: "{{ hostvars[groups.oo_first_master.0].g_new_openshift_ca_mktemp.stdout }}"
  121. roles:
  122. - role: openshift_master_facts
  123. - role: openshift_named_certificates
  124. - role: openshift_ca
  125. openshift_ca_host: "{{ groups.oo_first_master.0 }}"
  126. - name: Create temp directory for syncing certs
  127. hosts: localhost
  128. connection: local
  129. become: no
  130. gather_facts: no
  131. tasks:
  132. - name: Create local temp directory for syncing certs
  133. local_action: command mktemp -d /tmp/openshift-ansible-XXXXXXX
  134. register: g_master_mktemp
  135. changed_when: false
  136. - name: Retrieve OpenShift CA
  137. hosts: oo_first_master
  138. vars:
  139. openshift_ca_host: "{{ groups.oo_first_master.0 }}"
  140. tasks:
  141. - name: Retrieve CA certificate, key, bundle and serial
  142. fetch:
  143. src: "{{ hostvars[openshift_ca_host].g_new_openshift_ca_mktemp.stdout }}/{{ item }}"
  144. dest: "{{ hostvars['localhost'].g_master_mktemp.stdout }}/"
  145. flat: yes
  146. fail_on_missing: yes
  147. validate_checksum: yes
  148. with_items:
  149. - ca.crt
  150. - ca.key
  151. - ca-bundle.crt
  152. - ca.serial.txt
  153. - client-ca-bundle.crt
  154. delegate_to: "{{ openshift_ca_host }}"
  155. run_once: true
  156. changed_when: false
  157. - name: Distribute OpenShift CA to masters
  158. hosts: oo_masters_to_config
  159. vars:
  160. openshift_ca_host: "{{ groups.oo_first_master.0 }}"
  161. tasks:
  162. - name: Deploy CA certificate, key, bundle and serial
  163. copy:
  164. src: "{{ hostvars['localhost'].g_master_mktemp.stdout }}/{{ item }}"
  165. dest: "{{ openshift.common.config_base }}/master/"
  166. with_items:
  167. - ca.crt
  168. - ca.key
  169. - ca-bundle.crt
  170. - ca.serial.txt
  171. - client-ca-bundle.crt
  172. - name: Update master client kubeconfig CA data
  173. kubeclient_ca:
  174. client_path: "{{ openshift.common.config_base }}/master/openshift-master.kubeconfig"
  175. ca_path: "{{ openshift.common.config_base }}/master/ca-bundle.crt"
  176. - name: Update admin client kubeconfig CA data
  177. kubeclient_ca:
  178. client_path: "{{ openshift.common.config_base }}/master/admin.kubeconfig"
  179. ca_path: "{{ openshift.common.config_base }}/master/ca-bundle.crt"
  180. - name: Lookup default group for ansible_ssh_user
  181. command: "/usr/bin/id -g {{ ansible_ssh_user | quote }}"
  182. changed_when: false
  183. register: _ansible_ssh_user_gid
  184. - set_fact:
  185. client_users: "{{ [ansible_ssh_user, 'root'] | unique }}"
  186. - name: Create the client config dir(s)
  187. file:
  188. path: "~{{ item }}/.kube"
  189. state: directory
  190. mode: 0700
  191. owner: "{{ item }}"
  192. group: "{{ 'root' if item == 'root' else _ansible_ssh_user_gid.stdout }}"
  193. with_items: "{{ client_users }}"
  194. - name: Copy the admin client config(s)
  195. copy:
  196. src: "{{ openshift.common.config_base }}/master/admin.kubeconfig"
  197. dest: "~{{ item }}/.kube/config"
  198. remote_src: yes
  199. with_items: "{{ client_users }}"
  200. - name: Update the permissions on the admin client config(s)
  201. file:
  202. path: "~{{ item }}/.kube/config"
  203. state: file
  204. mode: 0700
  205. owner: "{{ item }}"
  206. group: "{{ 'root' if item == 'root' else _ansible_ssh_user_gid.stdout }}"
  207. with_items: "{{ client_users }}"
  208. - include: ../../openshift-master/restart.yml
  209. # Do not restart masters when master or etcd certificates were previously expired.
  210. when:
  211. # masters
  212. - ('expired' not in hostvars
  213. | oo_select_keys(groups['oo_masters_to_config'])
  214. | oo_collect('check_results.check_results.ocp_certs')
  215. | oo_collect('health', {'path':hostvars[groups.oo_first_master.0].openshift.common.config_base ~ "/master/master.server.crt"}))
  216. - ('expired' not in hostvars
  217. | oo_select_keys(groups['oo_masters_to_config'])
  218. | oo_collect('check_results.check_results.ocp_certs')
  219. | oo_collect('health', {'path':hostvars[groups.oo_first_master.0].openshift.common.config_base ~ "/master/ca-bundle.crt"}))
  220. # etcd
  221. - ('expired' not in (hostvars
  222. | oo_select_keys(groups['etcd'])
  223. | oo_collect('check_results.check_results.etcd')
  224. | oo_collect('health')))
  225. - name: Distribute OpenShift CA certificate to nodes
  226. hosts: oo_nodes_to_config
  227. vars:
  228. openshift_ca_host: "{{ groups.oo_first_master.0 }}"
  229. tasks:
  230. - copy:
  231. src: "{{ hostvars['localhost'].g_master_mktemp.stdout }}/ca-bundle.crt"
  232. dest: "{{ openshift.common.config_base }}/node/ca.crt"
  233. - name: Copy OpenShift CA to system CA trust
  234. copy:
  235. src: "{{ item.cert }}"
  236. dest: "/etc/pki/ca-trust/source/anchors/{{ item.id }}-{{ item.cert | basename }}"
  237. remote_src: yes
  238. with_items:
  239. - id: openshift
  240. cert: "{{ openshift.common.config_base }}/node/ca.crt"
  241. notify:
  242. - update ca trust
  243. - name: Update node client kubeconfig CA data
  244. kubeclient_ca:
  245. client_path: "{{ openshift.common.config_base }}/node/system:node:{{ openshift.common.hostname }}.kubeconfig"
  246. ca_path: "{{ openshift.common.config_base }}/node/ca.crt"
  247. handlers:
  248. # Normally this handler would restart docker after updating ca
  249. # trust. We'll do that when we restart nodes to avoid restarting
  250. # docker on all nodes in parallel.
  251. - name: update ca trust
  252. command: update-ca-trust
  253. - name: Delete temporary directory on CA host
  254. hosts: oo_first_master
  255. tasks:
  256. - file:
  257. path: "{{ g_new_openshift_ca_mktemp.stdout }}"
  258. state: absent
  259. - name: Delete temporary directory on localhost
  260. hosts: localhost
  261. connection: local
  262. become: no
  263. gather_facts: no
  264. tasks:
  265. - file:
  266. name: "{{ g_master_mktemp.stdout }}"
  267. state: absent
  268. changed_when: false
  269. - include: ../../openshift-node/restart.yml
  270. # Do not restart nodes when node, master or etcd certificates were previously expired.
  271. when:
  272. # nodes
  273. - ('expired' not in hostvars
  274. | oo_select_keys(groups['oo_nodes_to_config'])
  275. | oo_collect('check_results.check_results.ocp_certs')
  276. | oo_collect('health', {'path':hostvars[groups.oo_nodes_to_config.0].openshift.common.config_base ~ "/node/server.crt"}))
  277. - ('expired' not in hostvars
  278. | oo_select_keys(groups['oo_nodes_to_config'])
  279. | oo_collect('check_results.check_results.ocp_certs')
  280. | oo_collect('health', {'path':hostvars[groups.oo_nodes_to_config.0].openshift.common.config_base ~ "/node/ca.crt"}))
  281. # masters
  282. - ('expired' not in hostvars
  283. | oo_select_keys(groups['oo_masters_to_config'])
  284. | oo_collect('check_results.check_results.ocp_certs')
  285. | oo_collect('health', {'path':hostvars[groups.oo_first_master.0].openshift.common.config_base ~ "/master/master.server.crt"}))
  286. - ('expired' not in hostvars
  287. | oo_select_keys(groups['oo_masters_to_config'])
  288. | oo_collect('check_results.check_results.ocp_certs')
  289. | oo_collect('health', {'path':hostvars[groups.oo_first_master.0].openshift.common.config_base ~ "/master/ca-bundle.crt"}))
  290. # etcd
  291. - ('expired' not in (hostvars
  292. | oo_select_keys(groups['etcd'])
  293. | oo_collect('check_results.check_results.etcd')
  294. | oo_collect('health')))