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.

180 lines
6.2 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. - bootstrap-os
  16. - apps
  17. - network
  18. - master
  19. - node
  20. - name: "Gen_certs | Create kubernetes script directory (on {{groups['kube-master'][0]}})"
  21. file:
  22. path: "{{ kube_script_dir }}"
  23. state: directory
  24. owner: kube
  25. run_once: yes
  26. delegate_to: "{{groups['kube-master'][0]}}"
  27. when: gen_certs|default(false)
  28. tags:
  29. - k8s-secrets
  30. - bootstrap-os
  31. - name: Gen_certs | write openssl config
  32. template:
  33. src: "openssl.conf.j2"
  34. dest: "{{ kube_config_dir }}/openssl.conf"
  35. run_once: yes
  36. delegate_to: "{{groups['kube-master'][0]}}"
  37. when: gen_certs|default(false)
  38. - name: Gen_certs | copy certs generation script
  39. copy:
  40. src: "make-ssl.sh"
  41. dest: "{{ kube_script_dir }}/make-ssl.sh"
  42. mode: 0700
  43. run_once: yes
  44. delegate_to: "{{groups['kube-master'][0]}}"
  45. when: gen_certs|default(false)
  46. - name: Gen_certs | run cert generation script
  47. command: "{{ kube_script_dir }}/make-ssl.sh -f {{ kube_config_dir }}/openssl.conf -d {{ kube_cert_dir }}"
  48. environment:
  49. - MASTERS: "{% for m in groups['kube-master'] %}
  50. {% if gen_master_certs|default(false) %}
  51. {{ m }}
  52. {% endif %}
  53. {% endfor %}"
  54. - HOSTS: "{% for h in groups['k8s-cluster'] %}
  55. {% if gen_node_certs[h]|default(true) %}
  56. {{ h }}
  57. {% endif %}
  58. {% endfor %}"
  59. run_once: yes
  60. delegate_to: "{{groups['kube-master'][0]}}"
  61. when: gen_certs|default(false)
  62. notify: set secret_changed
  63. - set_fact:
  64. all_master_certs: "['ca-key.pem',
  65. 'apiserver.pem',
  66. 'apiserver-key.pem',
  67. 'kube-scheduler.pem',
  68. 'kube-scheduler-key.pem',
  69. 'kube-controller-manager.pem',
  70. 'kube-controller-manager-key.pem',
  71. {% for node in groups['kube-master'] %}
  72. 'admin-{{ node }}.pem',
  73. 'admin-{{ node }}-key.pem',
  74. {% endfor %}]"
  75. my_master_certs: ['ca-key.pem',
  76. 'admin-{{ inventory_hostname }}.pem',
  77. 'admin-{{ inventory_hostname }}-key.pem',
  78. 'apiserver.pem',
  79. 'apiserver-key.pem',
  80. 'kube-scheduler.pem',
  81. 'kube-scheduler-key.pem',
  82. 'kube-controller-manager.pem',
  83. 'kube-controller-manager-key.pem']
  84. all_node_certs: "['ca.pem',
  85. {% for node in groups['k8s-cluster'] %}
  86. 'node-{{ node }}.pem',
  87. 'node-{{ node }}-key.pem',
  88. 'kube-proxy-{{ node }}.pem',
  89. 'kube-proxy-{{ node }}-key.pem',
  90. {% endfor %}]"
  91. my_node_certs: ['ca.pem',
  92. 'node-{{ inventory_hostname }}.pem',
  93. 'node-{{ inventory_hostname }}-key.pem',
  94. 'kube-proxy-{{ inventory_hostname }}.pem',
  95. 'kube-proxy-{{ inventory_hostname }}-key.pem']
  96. tags:
  97. - facts
  98. - name: Gen_certs | Gather master certs
  99. shell: "tar cfz - -C {{ kube_cert_dir }} -T /dev/stdin <<< {{ my_master_certs|join(' ') }} {{ all_node_certs|join(' ') }} | base64 --wrap=0"
  100. args:
  101. executable: /bin/bash
  102. no_log: true
  103. register: master_cert_data
  104. check_mode: no
  105. delegate_to: "{{groups['kube-master'][0]}}"
  106. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  107. inventory_hostname != groups['kube-master'][0]
  108. - name: Gen_certs | Gather node certs
  109. shell: "tar cfz - -C {{ kube_cert_dir }} -T /dev/stdin <<< {{ my_node_certs|join(' ') }} | base64 --wrap=0"
  110. args:
  111. executable: /bin/bash
  112. no_log: true
  113. register: node_cert_data
  114. check_mode: no
  115. delegate_to: "{{groups['kube-master'][0]}}"
  116. when: inventory_hostname in groups['kube-node'] and
  117. sync_certs|default(false) and
  118. inventory_hostname != groups['kube-master'][0]
  119. # NOTE(mattymo): Use temporary file to copy master certs because we have a ~200k
  120. # char limit when using shell command
  121. # FIXME(mattymo): Use tempfile module in ansible 2.3
  122. - name: Gen_certs | Prepare tempfile for unpacking certs
  123. command: mktemp /tmp/certsXXXXX.tar.gz
  124. register: cert_tempfile
  125. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  126. inventory_hostname != groups['kube-master'][0]
  127. - name: Gen_certs | Write master certs to tempfile
  128. copy:
  129. content: "{{master_cert_data.stdout}}"
  130. dest: "{{cert_tempfile.stdout}}"
  131. owner: root
  132. mode: "0600"
  133. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  134. inventory_hostname != groups['kube-master'][0]
  135. - name: Gen_certs | Unpack certs on masters
  136. shell: "base64 -d < {{ cert_tempfile.stdout }} | tar xz -C {{ kube_cert_dir }}"
  137. no_log: true
  138. changed_when: false
  139. check_mode: no
  140. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  141. inventory_hostname != groups['kube-master'][0]
  142. notify: set secret_changed
  143. - name: Gen_certs | Cleanup tempfile
  144. file:
  145. path: "{{cert_tempfile.stdout}}"
  146. state: absent
  147. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  148. inventory_hostname != groups['kube-master'][0]
  149. - name: Gen_certs | Copy certs on nodes
  150. shell: "base64 -d <<< '{{node_cert_data.stdout|quote}}' | tar xz -C {{ kube_cert_dir }}"
  151. args:
  152. executable: /bin/bash
  153. no_log: true
  154. changed_when: false
  155. check_mode: no
  156. when: inventory_hostname in groups['kube-node'] and
  157. sync_certs|default(false) and
  158. inventory_hostname != groups['kube-master'][0]
  159. notify: set secret_changed
  160. - name: Gen_certs | check certificate permissions
  161. file:
  162. path: "{{ kube_cert_dir }}"
  163. group: "{{ kube_cert_group }}"
  164. owner: kube
  165. mode: "u=rwX,g-rwx,o-rwx"
  166. recurse: yes