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.

181 lines
6.3 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. register: master_cert_data
  78. check_mode: no
  79. delegate_to: "{{groups['kube-master'][0]}}"
  80. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  81. inventory_hostname != groups['kube-master'][0]
  82. - name: Gen_certs | Gather node certs
  83. shell: "tar cfz - -C {{ kube_cert_dir }} -T /dev/stdin <<< {{ my_node_certs|join(' ') }} | base64 --wrap=0"
  84. args:
  85. executable: /bin/bash
  86. register: node_cert_data
  87. check_mode: no
  88. delegate_to: "{{groups['kube-master'][0]}}"
  89. when: inventory_hostname in groups['kube-node'] and
  90. sync_certs|default(false) and
  91. inventory_hostname != groups['kube-master'][0]
  92. #NOTE(mattymo): Use temporary file to copy master certs because we have a ~200k
  93. #char limit when using shell command
  94. #FIXME(mattymo): Use tempfile module in ansible 2.3
  95. - name: Gen_certs | Prepare tempfile for unpacking certs
  96. shell: mktemp /tmp/certsXXXXX.tar.gz
  97. register: cert_tempfile
  98. - name: Gen_certs | Write master certs to tempfile
  99. copy:
  100. content: "{{master_cert_data.stdout}}"
  101. dest: "{{cert_tempfile.stdout}}"
  102. owner: root
  103. mode: "0600"
  104. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  105. inventory_hostname != groups['kube-master'][0]
  106. - name: Gen_certs | Unpack certs on masters
  107. shell: "base64 -d < {{ cert_tempfile.stdout }} | tar xz -C {{ kube_cert_dir }}"
  108. changed_when: false
  109. check_mode: no
  110. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  111. inventory_hostname != groups['kube-master'][0]
  112. notify: set secret_changed
  113. - name: Gen_certs | Cleanup tempfile
  114. file:
  115. path: "{{cert_tempfile.stdout}}"
  116. state: absent
  117. when: inventory_hostname in groups['kube-master'] and sync_certs|default(false) and
  118. inventory_hostname != groups['kube-master'][0]
  119. - name: Gen_certs | Copy certs on nodes
  120. shell: "base64 -d <<< '{{node_cert_data.stdout|quote}}' | tar xz -C {{ kube_cert_dir }}"
  121. args:
  122. executable: /bin/bash
  123. changed_when: false
  124. check_mode: no
  125. when: inventory_hostname in groups['kube-node'] and
  126. sync_certs|default(false) and
  127. inventory_hostname != groups['kube-master'][0]
  128. notify: set secret_changed
  129. - name: Gen_certs | check certificate permissions
  130. file:
  131. path={{ kube_cert_dir }}
  132. group={{ kube_cert_group }}
  133. owner=kube
  134. recurse=yes
  135. - name: Gen_certs | set permissions on keys
  136. shell: chmod 0600 {{ kube_cert_dir}}/*key.pem
  137. when: inventory_hostname in groups['kube-master']
  138. changed_when: false
  139. - name: Gen_certs | target ca-certificates path
  140. set_fact:
  141. ca_cert_path: |-
  142. {% if ansible_os_family == "Debian" -%}
  143. /usr/local/share/ca-certificates/kube-ca.crt
  144. {%- elif ansible_os_family == "RedHat" -%}
  145. /etc/pki/ca-trust/source/anchors/kube-ca.crt
  146. {%- elif ansible_os_family in ["CoreOS", "Container Linux by CoreOS"] -%}
  147. /etc/ssl/certs/kube-ca.pem
  148. {%- endif %}
  149. tags: facts
  150. - name: Gen_certs | add CA to trusted CA dir
  151. copy:
  152. src: "{{ kube_cert_dir }}/ca.pem"
  153. dest: "{{ ca_cert_path }}"
  154. remote_src: true
  155. register: kube_ca_cert
  156. - name: Gen_certs | update ca-certificates (Debian/Ubuntu/Container Linux by CoreOS)
  157. command: update-ca-certificates
  158. when: kube_ca_cert.changed and ansible_os_family in ["Debian", "CoreOS", "Container Linux by CoreOS"]
  159. - name: Gen_certs | update ca-certificates (RedHat)
  160. command: update-ca-trust extract
  161. when: kube_ca_cert.changed and ansible_os_family == "RedHat"