ca.yml 13 KB

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