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.

191 lines
7.1 KiB

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