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.

163 lines
5.6 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. check_mode: no
  84. delegate_to: "{{groups['etcd'][0]}}"
  85. when: inventory_hostname in groups['etcd'] and sync_certs|default(false) and
  86. inventory_hostname != groups['etcd'][0]
  87. notify: set etcd_secret_changed
  88. - name: Gen_certs | Gather etcd node certs
  89. shell: "tar cfz - -C {{ etcd_cert_dir }} -T /dev/stdin <<< {{ my_node_certs|join(' ') }} | base64 --wrap=0"
  90. args:
  91. executable: /bin/bash
  92. register: etcd_node_cert_data
  93. check_mode: no
  94. delegate_to: "{{groups['etcd'][0]}}"
  95. when: (('calico-rr' in groups and inventory_hostname in groups['calico-rr']) or
  96. inventory_hostname in groups['k8s-cluster']) and
  97. sync_certs|default(false) and inventory_hostname not in groups['etcd']
  98. notify: set etcd_secret_changed
  99. - name: Gen_certs | Copy certs on masters
  100. shell: "base64 -d <<< '{{etcd_master_cert_data.stdout|quote}}' | tar xz -C {{ etcd_cert_dir }}"
  101. args:
  102. executable: /bin/bash
  103. changed_when: false
  104. when: inventory_hostname in groups['etcd'] and sync_certs|default(false) and
  105. inventory_hostname != groups['etcd'][0]
  106. - name: Gen_certs | Copy certs on nodes
  107. shell: "base64 -d <<< '{{etcd_node_cert_data.stdout|quote}}' | tar xz -C {{ etcd_cert_dir }}"
  108. args:
  109. executable: /bin/bash
  110. changed_when: false
  111. when: sync_certs|default(false) and
  112. inventory_hostname not in groups['etcd']
  113. - name: Gen_certs | check certificate permissions
  114. file:
  115. path={{ etcd_cert_dir }}
  116. group={{ etcd_cert_group }}
  117. state=directory
  118. owner=kube
  119. recurse=yes
  120. - name: Gen_certs | set permissions on keys
  121. shell: chmod 0600 {{ etcd_cert_dir}}/*key.pem
  122. when: inventory_hostname in groups['etcd']
  123. changed_when: false
  124. - name: Gen_certs | target ca-certificate store file
  125. set_fact:
  126. ca_cert_path: |-
  127. {% if ansible_os_family == "Debian" -%}
  128. /usr/local/share/ca-certificates/etcd-ca.crt
  129. {%- elif ansible_os_family == "RedHat" -%}
  130. /etc/pki/ca-trust/source/anchors/etcd-ca.crt
  131. {%- elif ansible_os_family in ["CoreOS", "Container Linux by CoreOS"] -%}
  132. /etc/ssl/certs/etcd-ca.pem
  133. {%- endif %}
  134. tags: facts
  135. - name: Gen_certs | add CA to trusted CA dir
  136. copy:
  137. src: "{{ etcd_cert_dir }}/ca.pem"
  138. dest: "{{ ca_cert_path }}"
  139. remote_src: true
  140. register: etcd_ca_cert
  141. - name: Gen_certs | update ca-certificates (Debian/Ubuntu/Container Linux by CoreOS)
  142. command: update-ca-certificates
  143. when: etcd_ca_cert.changed and ansible_os_family in ["Debian", "CoreOS", "Container Linux by CoreOS"]
  144. - name: Gen_certs | update ca-certificates (RedHat)
  145. command: update-ca-trust extract
  146. when: etcd_ca_cert.changed and ansible_os_family == "RedHat"