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.

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