ca.yml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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_etcd_to_config:oo_masters_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. - name: Backup existing etcd CA certificate directories
  20. hosts: oo_etcd_to_config
  21. roles:
  22. - role: etcd_common
  23. r_etcd_common_etcd_runtime: "{{ openshift.common.etcd_runtime }}"
  24. tasks:
  25. - name: Determine if CA certificate directory exists
  26. stat:
  27. path: "{{ etcd_ca_dir }}"
  28. register: etcd_ca_certs_dir_stat
  29. - name: Backup generated etcd certificates
  30. command: >
  31. tar -czf {{ etcd_conf_dir }}/etcd-ca-certificate-backup-{{ ansible_date_time.epoch }}.tgz
  32. {{ etcd_ca_dir }}
  33. args:
  34. warn: no
  35. when: etcd_ca_certs_dir_stat.stat.exists | bool
  36. - name: Remove CA certificate directory
  37. file:
  38. path: "{{ etcd_ca_dir }}"
  39. state: absent
  40. when: etcd_ca_certs_dir_stat.stat.exists | bool
  41. - name: Generate new etcd CA
  42. hosts: oo_first_etcd
  43. roles:
  44. - role: openshift_etcd_ca
  45. etcd_peers: "{{ groups.oo_etcd_to_config | default([], true) }}"
  46. etcd_ca_host: "{{ groups.oo_etcd_to_config.0 }}"
  47. etcd_certificates_etcd_hosts: "{{ groups.oo_etcd_to_config | default([], true) }}"
  48. - name: Create temp directory for syncing certs
  49. hosts: localhost
  50. connection: local
  51. become: no
  52. gather_facts: no
  53. tasks:
  54. - name: Create local temp directory for syncing certs
  55. local_action: command mktemp -d /tmp/openshift-ansible-XXXXXXX
  56. register: g_etcd_mktemp
  57. changed_when: false
  58. - name: Distribute etcd CA to etcd hosts
  59. hosts: oo_etcd_to_config
  60. vars:
  61. etcd_ca_host: "{{ groups.oo_etcd_to_config.0 }}"
  62. roles:
  63. - role: etcd_common
  64. r_etcd_common_etcd_runtime: "{{ openshift.common.etcd_runtime }}"
  65. tasks:
  66. - name: Create a tarball of the etcd ca certs
  67. command: >
  68. tar -czvf {{ etcd_conf_dir }}/{{ etcd_ca_name }}.tgz
  69. -C {{ etcd_ca_dir }} .
  70. args:
  71. creates: "{{ etcd_conf_dir }}/{{ etcd_ca_name }}.tgz"
  72. warn: no
  73. delegate_to: "{{ etcd_ca_host }}"
  74. run_once: true
  75. - name: Retrieve etcd ca cert tarball
  76. fetch:
  77. src: "{{ etcd_conf_dir }}/{{ etcd_ca_name }}.tgz"
  78. dest: "{{ hostvars['localhost'].g_etcd_mktemp.stdout }}/"
  79. flat: yes
  80. fail_on_missing: yes
  81. validate_checksum: yes
  82. delegate_to: "{{ etcd_ca_host }}"
  83. run_once: true
  84. - name: Ensure ca directory exists
  85. file:
  86. path: "{{ etcd_ca_dir }}"
  87. state: directory
  88. - name: Unarchive etcd ca cert tarballs
  89. unarchive:
  90. src: "{{ hostvars['localhost'].g_etcd_mktemp.stdout }}/{{ etcd_ca_name }}.tgz"
  91. dest: "{{ etcd_ca_dir }}"
  92. - name: Read current etcd CA
  93. slurp:
  94. src: "{{ etcd_conf_dir }}/ca.crt"
  95. register: g_current_etcd_ca_output
  96. - name: Read new etcd CA
  97. slurp:
  98. src: "{{ etcd_ca_dir }}/ca.crt"
  99. register: g_new_etcd_ca_output
  100. - copy:
  101. content: "{{ (g_new_etcd_ca_output.content|b64decode) + (g_current_etcd_ca_output.content|b64decode) }}"
  102. dest: "{{ item }}/ca.crt"
  103. with_items:
  104. - "{{ etcd_conf_dir }}"
  105. - "{{ etcd_ca_dir }}"
  106. - name: Retrieve etcd CA certificate
  107. hosts: oo_first_etcd
  108. roles:
  109. - role: etcd_common
  110. r_etcd_common_etcd_runtime: "{{ openshift.common.etcd_runtime }}"
  111. tasks:
  112. - name: Retrieve etcd CA certificate
  113. fetch:
  114. src: "{{ etcd_conf_dir }}/ca.crt"
  115. dest: "{{ hostvars['localhost'].g_etcd_mktemp.stdout }}/"
  116. flat: yes
  117. fail_on_missing: yes
  118. validate_checksum: yes
  119. - name: Distribute etcd CA to masters
  120. hosts: oo_masters_to_config
  121. vars:
  122. openshift_ca_host: "{{ groups.oo_first_master.0 }}"
  123. tasks:
  124. - name: Deploy CA certificate, key, bundle and serial
  125. copy:
  126. src: "{{ hostvars['localhost'].g_etcd_mktemp.stdout }}/ca.crt"
  127. dest: "{{ openshift.common.config_base }}/master/master.etcd-ca.crt"
  128. when: groups.oo_etcd_to_config | default([]) | length > 0
  129. - name: Delete temporary directory on localhost
  130. hosts: localhost
  131. connection: local
  132. become: no
  133. gather_facts: no
  134. tasks:
  135. - file:
  136. name: "{{ g_etcd_mktemp.stdout }}"
  137. state: absent
  138. changed_when: false
  139. - include: ../../openshift-etcd/restart.yml
  140. # Do not restart etcd when etcd certificates were previously expired.
  141. when: ('expired' not in (hostvars
  142. | oo_select_keys(groups['etcd'])
  143. | oo_collect('check_results.check_results.etcd')
  144. | oo_collect('health')))
  145. # Update master config when ca-bundle not referenced. Services will be
  146. # restarted below after new CA certificate has been distributed.
  147. - name: Ensure ca-bundle.crt is referenced in master configuration
  148. hosts: oo_masters_to_config
  149. tasks:
  150. - slurp:
  151. src: "{{ openshift.common.config_base }}/master/master-config.yaml"
  152. register: g_master_config_output
  153. - modify_yaml:
  154. dest: "{{ openshift.common.config_base }}/master/master-config.yaml"
  155. yaml_key: kubeletClientInfo.ca
  156. yaml_value: ca-bundle.crt
  157. when: (g_master_config_output.content|b64decode|from_yaml).kubeletClientInfo.ca != 'ca-bundle.crt'
  158. - modify_yaml:
  159. dest: "{{ openshift.common.config_base }}/master/master-config.yaml"
  160. yaml_key: serviceAccountConfig.masterCA
  161. yaml_value: ca-bundle.crt
  162. when: (g_master_config_output.content|b64decode|from_yaml).serviceAccountConfig.masterCA != 'ca-bundle.crt'
  163. - modify_yaml:
  164. dest: "{{ openshift.common.config_base }}/master/master-config.yaml"
  165. yaml_key: oauthConfig.masterCA
  166. yaml_value: ca-bundle.crt
  167. when: (g_master_config_output.content|b64decode|from_yaml).oauthConfig.masterCA != 'ca-bundle.crt'
  168. - modify_yaml:
  169. dest: "{{ openshift.common.config_base }}/master/master-config.yaml"
  170. yaml_key: servingInfo.clientCA
  171. yaml_value: ca-bundle.crt
  172. when: (g_master_config_output.content|b64decode|from_yaml).servingInfo.clientCA != 'ca-bundle.crt'
  173. - modify_yaml:
  174. dest: "{{ openshift.common.config_base }}/master/master-config.yaml"
  175. yaml_key: etcdClientInfo.ca
  176. yaml_value: ca-bundle.crt
  177. when:
  178. - groups.oo_etcd_to_config | default([]) | length == 0
  179. - (g_master_config_output.content|b64decode|from_yaml).etcdClientInfo.ca != 'ca-bundle.crt'
  180. - modify_yaml:
  181. dest: "{{ openshift.common.config_base }}/master/master-config.yaml"
  182. yaml_key: etcdConfig.peerServingInfo.clientCA
  183. yaml_value: ca-bundle.crt
  184. when:
  185. - groups.oo_etcd_to_config | default([]) | length == 0
  186. - (g_master_config_output.content|b64decode|from_yaml).etcdConfig.peerServingInfo.clientCA != 'ca-bundle.crt'
  187. - modify_yaml:
  188. dest: "{{ openshift.common.config_base }}/master/master-config.yaml"
  189. yaml_key: etcdConfig.servingInfo.clientCA
  190. yaml_value: ca-bundle.crt
  191. when:
  192. - groups.oo_etcd_to_config | default([]) | length == 0
  193. - (g_master_config_output.content|b64decode|from_yaml).etcdConfig.servingInfo.clientCA != 'ca-bundle.crt'
  194. - name: Copy current OpenShift CA to legacy directory
  195. hosts: oo_masters_to_config
  196. pre_tasks:
  197. - name: Create legacy-ca directory
  198. file:
  199. path: "{{ openshift.common.config_base }}/master/legacy-ca"
  200. state: directory
  201. mode: 0700
  202. owner: root
  203. group: root
  204. - command: mktemp -u XXXXXX
  205. register: g_legacy_ca_mktemp
  206. changed_when: false
  207. # Copy CA certificate, key, serial and bundle to legacy-ca with a
  208. # prefix generated by mktemp, ie. XXXXXX-ca.crt.
  209. #
  210. # The following roles will pick up all CA certificates matching
  211. # /.*-ca.crt/ in the legacy-ca directory and ensure they are present
  212. # in the OpenShift CA bundle.
  213. # - openshift_ca
  214. # - openshift_master_certificates
  215. # - openshift_node_certificates
  216. - name: Copy current OpenShift CA to legacy directory
  217. copy:
  218. src: "{{ openshift.common.config_base }}/master/{{ item }}"
  219. dest: "{{ openshift.common.config_base }}/master/legacy-ca/{{ g_legacy_ca_mktemp.stdout }}-{{ item }}"
  220. remote_src: true
  221. # It is possible that redeploying failed and files may be missing.
  222. # Ignore errors in this case. Files should have been copied to
  223. # legacy-ca directory in previous run.
  224. ignore_errors: true
  225. with_items:
  226. - "ca.crt"
  227. - "ca.key"
  228. - "ca.serial.txt"
  229. - "ca-bundle.crt"
  230. - name: Generate new OpenShift CA certificate
  231. hosts: oo_first_master
  232. pre_tasks:
  233. - name: Create temporary directory for creating new CA certificate
  234. command: >
  235. mktemp -d /tmp/openshift-ansible-XXXXXXX
  236. register: g_new_openshift_ca_mktemp
  237. changed_when: false
  238. roles:
  239. - role: openshift_ca
  240. # Set openshift_ca_config_dir to a temporary directory where CA
  241. # will be created. We'll replace the existing CA with the CA
  242. # created in the temporary directory.
  243. openshift_ca_config_dir: "{{ g_new_openshift_ca_mktemp.stdout }}"
  244. openshift_ca_host: "{{ groups.oo_first_master.0 }}"
  245. openshift_master_hostnames: "{{ hostvars
  246. | oo_select_keys(groups['oo_masters_to_config'] | default([]))
  247. | oo_collect('openshift.common.all_hostnames')
  248. | oo_flatten | unique }}"
  249. - name: Create temp directory for syncing certs
  250. hosts: localhost
  251. connection: local
  252. become: no
  253. gather_facts: no
  254. tasks:
  255. - name: Create local temp directory for syncing certs
  256. local_action: command mktemp -d /tmp/openshift-ansible-XXXXXXX
  257. register: g_master_mktemp
  258. changed_when: false
  259. - name: Retrieve OpenShift CA
  260. hosts: oo_first_master
  261. vars:
  262. openshift_ca_host: "{{ groups.oo_first_master.0 }}"
  263. tasks:
  264. - name: Retrieve CA certificate, key, bundle and serial
  265. fetch:
  266. src: "{{ hostvars[openshift_ca_host].g_new_openshift_ca_mktemp.stdout }}/{{ item }}"
  267. dest: "{{ hostvars['localhost'].g_master_mktemp.stdout }}/"
  268. flat: yes
  269. fail_on_missing: yes
  270. validate_checksum: yes
  271. with_items:
  272. - ca.crt
  273. - ca.key
  274. - ca-bundle.crt
  275. - ca.serial.txt
  276. delegate_to: "{{ openshift_ca_host }}"
  277. run_once: true
  278. changed_when: false
  279. - name: Distribute OpenShift CA to masters
  280. hosts: oo_masters_to_config
  281. vars:
  282. openshift_ca_host: "{{ groups.oo_first_master.0 }}"
  283. tasks:
  284. - name: Deploy CA certificate, key, bundle and serial
  285. copy:
  286. src: "{{ hostvars['localhost'].g_master_mktemp.stdout }}/{{ item }}"
  287. dest: "{{ openshift.common.config_base }}/master/"
  288. with_items:
  289. - ca.crt
  290. - ca.key
  291. - ca-bundle.crt
  292. - ca.serial.txt
  293. - name: Update master client kubeconfig CA data
  294. kubeclient_ca:
  295. client_path: "{{ openshift.common.config_base }}/master/openshift-master.kubeconfig"
  296. ca_path: "{{ openshift.common.config_base }}/master/ca-bundle.crt"
  297. - name: Update admin client kubeconfig CA data
  298. kubeclient_ca:
  299. client_path: "{{ openshift.common.config_base }}/master/admin.kubeconfig"
  300. ca_path: "{{ openshift.common.config_base }}/master/ca-bundle.crt"
  301. - name: Lookup default group for ansible_ssh_user
  302. command: "/usr/bin/id -g {{ ansible_ssh_user | quote }}"
  303. changed_when: false
  304. register: _ansible_ssh_user_gid
  305. - set_fact:
  306. client_users: "{{ [ansible_ssh_user, 'root'] | unique }}"
  307. - name: Create the client config dir(s)
  308. file:
  309. path: "~{{ item }}/.kube"
  310. state: directory
  311. mode: 0700
  312. owner: "{{ item }}"
  313. group: "{{ 'root' if item == 'root' else _ansible_ssh_user_gid.stdout }}"
  314. with_items: "{{ client_users }}"
  315. - name: Copy the admin client config(s)
  316. copy:
  317. src: "{{ openshift.common.config_base }}/master/admin.kubeconfig"
  318. dest: "~{{ item }}/.kube/config"
  319. remote_src: yes
  320. with_items: "{{ client_users }}"
  321. - name: Update the permissions on the admin client config(s)
  322. file:
  323. path: "~{{ item }}/.kube/config"
  324. state: file
  325. mode: 0700
  326. owner: "{{ item }}"
  327. group: "{{ 'root' if item == 'root' else _ansible_ssh_user_gid.stdout }}"
  328. with_items: "{{ client_users }}"
  329. - include: ../../openshift-master/restart.yml
  330. # Do not restart masters when master certificates were previously expired.
  331. when: ('expired' not in hostvars
  332. | oo_select_keys(groups['oo_masters_to_config'])
  333. | oo_collect('check_results.check_results.ocp_certs')
  334. | oo_collect('health', {'path':hostvars[groups.oo_first_master.0].openshift.common.config_base ~ "/master/master.server.crt"}))
  335. and
  336. ('expired' not in hostvars
  337. | oo_select_keys(groups['oo_masters_to_config'])
  338. | oo_collect('check_results.check_results.ocp_certs')
  339. | oo_collect('health', {'path':hostvars[groups.oo_first_master.0].openshift.common.config_base ~ "/master/ca-bundle.crt"}))
  340. - name: Distribute OpenShift CA certificate to nodes
  341. hosts: oo_nodes_to_config
  342. vars:
  343. openshift_ca_host: "{{ groups.oo_first_master.0 }}"
  344. tasks:
  345. - copy:
  346. src: "{{ hostvars['localhost'].g_master_mktemp.stdout }}/ca-bundle.crt"
  347. dest: "{{ openshift.common.config_base }}/node/ca.crt"
  348. - name: Copy OpenShift CA to system CA trust
  349. copy:
  350. src: "{{ item.cert }}"
  351. dest: "/etc/pki/ca-trust/source/anchors/{{ item.id }}-{{ item.cert | basename }}"
  352. remote_src: yes
  353. with_items:
  354. - id: openshift
  355. cert: "{{ openshift.common.config_base }}/node/ca.crt"
  356. notify:
  357. - update ca trust
  358. - name: Update node client kubeconfig CA data
  359. kubeclient_ca:
  360. client_path: "{{ openshift.common.config_base }}/node/system:node:{{ openshift.common.hostname }}.kubeconfig"
  361. ca_path: "{{ openshift.common.config_base }}/node/ca.crt"
  362. handlers:
  363. # Normally this handler would restart docker after updating ca
  364. # trust. We'll do that when we restart nodes to avoid restarting
  365. # docker on all nodes in parallel.
  366. - name: update ca trust
  367. command: update-ca-trust
  368. - name: Delete temporary directory on CA host
  369. hosts: oo_first_master
  370. tasks:
  371. - file:
  372. path: "{{ g_new_openshift_ca_mktemp.stdout }}"
  373. state: absent
  374. - name: Delete temporary directory on localhost
  375. hosts: localhost
  376. connection: local
  377. become: no
  378. gather_facts: no
  379. tasks:
  380. - file:
  381. name: "{{ g_master_mktemp.stdout }}"
  382. state: absent
  383. changed_when: false
  384. - include: ../../openshift-node/restart.yml
  385. # Do not restart nodes when node certificates were previously expired.
  386. when: ('expired' not in hostvars
  387. | oo_select_keys(groups['oo_nodes_to_config'])
  388. | oo_collect('check_results.check_results.ocp_certs')
  389. | oo_collect('health', {'path':hostvars[groups.oo_nodes_to_config.0].openshift.common.config_base ~ "/node/server.crt"}))
  390. and
  391. ('expired' not in hostvars
  392. | oo_select_keys(groups['oo_nodes_to_config'])
  393. | oo_collect('check_results.check_results.ocp_certs')
  394. | oo_collect('health', {'path':hostvars[groups.oo_nodes_to_config.0].openshift.common.config_base ~ "/node/ca.crt"}))