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.

185 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 hostvars[m].sync_certs|default(true) %}
  40. {{ m }}
  41. {% endif %}
  42. {% endfor %}"
  43. - HOSTS: "{% for h in groups['k8s-cluster'] %}
  44. {% if hostvars[h].sync_certs|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. - name: Gen_certs | Write master certs to tempfile
  101. copy:
  102. content: "{{master_cert_data.stdout}}"
  103. dest: "{{cert_tempfile.stdout}}"
  104. owner: root
  105. mode: "0600"
  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 | Unpack certs on masters
  109. shell: "base64 -d < {{ cert_tempfile.stdout }} | tar xz -C {{ kube_cert_dir }}"
  110. no_log: true
  111. changed_when: false
  112. check_mode: no
  113. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  114. inventory_hostname != groups['kube-master'][0]
  115. notify: set secret_changed
  116. - name: Gen_certs | Cleanup tempfile
  117. file:
  118. path: "{{cert_tempfile.stdout}}"
  119. state: absent
  120. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  121. inventory_hostname != groups['kube-master'][0]
  122. - name: Gen_certs | Copy certs on nodes
  123. shell: "base64 -d <<< '{{node_cert_data.stdout|quote}}' | tar xz -C {{ kube_cert_dir }}"
  124. args:
  125. executable: /bin/bash
  126. no_log: true
  127. changed_when: false
  128. check_mode: no
  129. when: inventory_hostname in groups['kube-node'] and
  130. sync_certs|default(false) and
  131. inventory_hostname != groups['kube-master'][0]
  132. notify: set secret_changed
  133. - name: Gen_certs | check certificate permissions
  134. file:
  135. path: "{{ kube_cert_dir }}"
  136. group: "{{ kube_cert_group }}"
  137. owner: kube
  138. recurse: yes
  139. - name: Gen_certs | set permissions on keys
  140. shell: chmod 0600 {{ kube_cert_dir}}/*key.pem
  141. when: inventory_hostname in groups['kube-master']
  142. changed_when: false
  143. - name: Gen_certs | target ca-certificates path
  144. set_fact:
  145. ca_cert_path: |-
  146. {% if ansible_os_family == "Debian" -%}
  147. /usr/local/share/ca-certificates/kube-ca.crt
  148. {%- elif ansible_os_family == "RedHat" -%}
  149. /etc/pki/ca-trust/source/anchors/kube-ca.crt
  150. {%- elif ansible_os_family in ["CoreOS", "Container Linux by CoreOS"] -%}
  151. /etc/ssl/certs/kube-ca.pem
  152. {%- endif %}
  153. tags: facts
  154. - name: Gen_certs | add CA to trusted CA dir
  155. copy:
  156. src: "{{ kube_cert_dir }}/ca.pem"
  157. dest: "{{ ca_cert_path }}"
  158. remote_src: true
  159. register: kube_ca_cert
  160. - name: Gen_certs | update ca-certificates (Debian/Ubuntu/Container Linux by CoreOS)
  161. command: update-ca-certificates
  162. when: kube_ca_cert.changed and ansible_os_family in ["Debian", "CoreOS", "Container Linux by CoreOS"]
  163. - name: Gen_certs | update ca-certificates (RedHat)
  164. command: update-ca-trust extract
  165. when: kube_ca_cert.changed and ansible_os_family == "RedHat"