openshift-ca.yml 12 KB

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