ca.yml 13 KB

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