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.

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