You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
8.1 KiB

  1. ---
  2. - name: "Gen_certs | Create kubernetes config directory (on {{groups['kube-master'][0]}})"
  3. file:
  4. path: "{{ kube_config_dir }}"
  5. state: directory
  6. owner: kube
  7. run_once: yes
  8. delegate_to: "{{groups['kube-master'][0]}}"
  9. when: gen_certs|default(false)
  10. tags:
  11. - kubelet
  12. - k8s-secrets
  13. - kube-controller-manager
  14. - kube-apiserver
  15. - apps
  16. - network
  17. - master
  18. - node
  19. - name: "Gen_certs | Create kubernetes script directory (on {{groups['kube-master'][0]}})"
  20. file:
  21. path: "{{ kube_script_dir }}"
  22. state: directory
  23. owner: kube
  24. run_once: yes
  25. delegate_to: "{{groups['kube-master'][0]}}"
  26. when: gen_certs|default(false)
  27. tags:
  28. - k8s-secrets
  29. - name: Gen_certs | write openssl config
  30. template:
  31. src: "openssl.conf.j2"
  32. dest: "{{ kube_config_dir }}/openssl.conf"
  33. run_once: yes
  34. delegate_to: "{{groups['kube-master'][0]}}"
  35. when: gen_certs|default(false)
  36. - name: Gen_certs | copy certs generation script
  37. copy:
  38. src: "make-ssl.sh"
  39. dest: "{{ kube_script_dir }}/make-ssl.sh"
  40. mode: 0700
  41. run_once: yes
  42. delegate_to: "{{groups['kube-master'][0]}}"
  43. when: gen_certs|default(false)
  44. - name: Gen_certs | run cert generation script
  45. command: "{{ kube_script_dir }}/make-ssl.sh -f {{ kube_config_dir }}/openssl.conf -d {{ kube_cert_dir }}"
  46. environment:
  47. - MASTERS: "{% for m in groups['kube-master'] %}
  48. {% if gen_master_certs|default(false) %}
  49. {{ m }}
  50. {% endif %}
  51. {% endfor %}"
  52. - HOSTS: "{% for h in groups['k8s-cluster'] %}
  53. {% if gen_node_certs[h]|default(true) %}
  54. {{ h }}
  55. {% endif %}
  56. {% endfor %}"
  57. run_once: yes
  58. delegate_to: "{{groups['kube-master'][0]}}"
  59. when: gen_certs|default(false)
  60. notify: set secret_changed
  61. - set_fact:
  62. all_master_certs: "['ca-key.pem',
  63. 'apiserver.pem',
  64. 'apiserver-key.pem',
  65. 'kube-scheduler.pem',
  66. 'kube-scheduler-key.pem',
  67. 'kube-controller-manager.pem',
  68. 'kube-controller-manager-key.pem',
  69. 'front-proxy-ca.pem',
  70. 'front-proxy-ca-key.pem',
  71. 'front-proxy-client.pem',
  72. 'front-proxy-client-key.pem',
  73. 'service-account-key.pem',
  74. {% for node in groups['kube-master'] %}
  75. 'admin-{{ node }}.pem',
  76. 'admin-{{ node }}-key.pem',
  77. {% endfor %}]"
  78. my_master_certs: ['ca-key.pem',
  79. 'admin-{{ inventory_hostname }}.pem',
  80. 'admin-{{ inventory_hostname }}-key.pem',
  81. 'apiserver.pem',
  82. 'apiserver-key.pem',
  83. 'front-proxy-ca.pem',
  84. 'front-proxy-ca-key.pem',
  85. 'front-proxy-client.pem',
  86. 'front-proxy-client-key.pem',
  87. 'service-account-key.pem',
  88. 'kube-scheduler.pem',
  89. 'kube-scheduler-key.pem',
  90. 'kube-controller-manager.pem',
  91. 'kube-controller-manager-key.pem']
  92. all_node_certs: "['ca.pem',
  93. {% for node in groups['k8s-cluster'] %}
  94. 'node-{{ node }}.pem',
  95. 'node-{{ node }}-key.pem',
  96. 'kube-proxy-{{ node }}.pem',
  97. 'kube-proxy-{{ node }}-key.pem',
  98. {% endfor %}]"
  99. my_node_certs: ['ca.pem',
  100. 'node-{{ inventory_hostname }}.pem',
  101. 'node-{{ inventory_hostname }}-key.pem',
  102. 'kube-proxy-{{ inventory_hostname }}.pem',
  103. 'kube-proxy-{{ inventory_hostname }}-key.pem']
  104. tags:
  105. - facts
  106. - name: "Check certs | check if a cert already exists on node"
  107. find:
  108. paths: "{{ kube_cert_dir }}"
  109. patterns: "*.pem"
  110. get_checksum: true
  111. register: kubecert_node
  112. when: inventory_hostname != groups['kube-master'][0]
  113. - name: "Check_certs | Set 'sync_certs' to true on masters"
  114. set_fact:
  115. sync_certs: true
  116. when: inventory_hostname in groups['kube-master'] and
  117. inventory_hostname != groups['kube-master'][0] and
  118. (not item in kubecert_node.files | map(attribute='path') | map("basename") | list or
  119. kubecert_node.files | selectattr("path", "equalto", "{{ kube_cert_dir }}/{{ item }}") | map(attribute="checksum")|first|default('') != kubecert_master.files | selectattr("path", "equalto", "{{ kube_cert_dir }}/{{ item }}") | map(attribute="checksum")|first|default(''))
  120. with_items:
  121. - "{{ my_master_certs + all_node_certs }}"
  122. - name: "Check_certs | Set 'sync_certs' to true on nodes"
  123. set_fact:
  124. sync_certs: true
  125. when: inventory_hostname in groups['kube-node'] and
  126. inventory_hostname != groups['kube-master'][0] and
  127. (not item in kubecert_node.files | map(attribute='path') | map("basename") | list or
  128. kubecert_node.files | selectattr("path", "equalto", "{{ kube_cert_dir }}/{{ item }}") | map(attribute="checksum")|first|default('') != kubecert_master.files | selectattr("path", "equalto", "{{ kube_cert_dir }}/{{ item }}") | map(attribute="checksum")|first|default(''))
  129. with_items:
  130. - "{{ my_node_certs }}"
  131. - name: Gen_certs | Gather master certs
  132. shell: "tar cfz - -C {{ kube_cert_dir }} -T /dev/stdin <<< {{ my_master_certs|join(' ') }} {{ all_node_certs|join(' ') }} | base64 --wrap=0"
  133. args:
  134. executable: /bin/bash
  135. no_log: true
  136. register: master_cert_data
  137. check_mode: no
  138. delegate_to: "{{groups['kube-master'][0]}}"
  139. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  140. inventory_hostname != groups['kube-master'][0]
  141. - name: Gen_certs | Gather node certs
  142. shell: "tar cfz - -C {{ kube_cert_dir }} -T /dev/stdin <<< {{ my_node_certs|join(' ') }} | base64 --wrap=0"
  143. args:
  144. executable: /bin/bash
  145. no_log: true
  146. register: node_cert_data
  147. check_mode: no
  148. delegate_to: "{{groups['kube-master'][0]}}"
  149. when: inventory_hostname in groups['kube-node'] and
  150. sync_certs|default(false) and
  151. inventory_hostname != groups['kube-master'][0]
  152. # NOTE(mattymo): Use temporary file to copy master certs because we have a ~200k
  153. # char limit when using shell command
  154. # FIXME(mattymo): Use tempfile module in ansible 2.3
  155. - name: Gen_certs | Prepare tempfile for unpacking certs on masters
  156. command: mktemp /tmp/certsXXXXX.tar.gz
  157. register: cert_tempfile
  158. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  159. inventory_hostname != groups['kube-master'][0]
  160. - name: Gen_certs | Write master certs to tempfile
  161. copy:
  162. content: "{{master_cert_data.stdout}}"
  163. dest: "{{cert_tempfile.stdout}}"
  164. owner: root
  165. mode: "0600"
  166. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  167. inventory_hostname != groups['kube-master'][0]
  168. - name: Gen_certs | Unpack certs on masters
  169. shell: "base64 -d < {{ cert_tempfile.stdout }} | tar xz -C {{ kube_cert_dir }}"
  170. no_log: true
  171. changed_when: false
  172. check_mode: no
  173. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  174. inventory_hostname != groups['kube-master'][0]
  175. notify: set secret_changed
  176. - name: Gen_certs | Cleanup tempfile on masters
  177. file:
  178. path: "{{cert_tempfile.stdout}}"
  179. state: absent
  180. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  181. inventory_hostname != groups['kube-master'][0]
  182. - name: Gen_certs | Copy certs on nodes
  183. shell: "base64 -d <<< '{{node_cert_data.stdout|quote}}' | tar xz -C {{ kube_cert_dir }}"
  184. args:
  185. executable: /bin/bash
  186. no_log: true
  187. changed_when: false
  188. check_mode: no
  189. when: inventory_hostname in groups['kube-node'] and
  190. sync_certs|default(false) and
  191. inventory_hostname != groups['kube-master'][0]
  192. notify: set secret_changed
  193. - name: Gen_certs | check certificate permissions
  194. file:
  195. path: "{{ kube_cert_dir }}"
  196. group: "{{ kube_cert_group }}"
  197. state: directory
  198. owner: kube
  199. mode: "u=rwX,g-rwx,o-rwx"
  200. recurse: yes