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.

107 lines
4.0 KiB

  1. ---
  2. - name: "Check_certs | check if the certs have already been generated on first master"
  3. find:
  4. paths: "{{ kube_cert_dir }}"
  5. patterns: "*.pem"
  6. get_checksum: true
  7. delegate_to: "{{groups['kube-master'][0]}}"
  8. register: kubecert_master
  9. run_once: true
  10. - name: "Check_certs | Set default value for 'sync_certs', 'gen_certs', and 'secret_changed' to false"
  11. set_fact:
  12. sync_certs: false
  13. gen_certs: false
  14. secret_changed: false
  15. - name: "Check certs | check if a cert already exists on node"
  16. stat:
  17. path: "{{ kube_cert_dir }}/{{ item }}"
  18. register: kubecert_node
  19. with_items:
  20. - ca.pem
  21. - apiserver.pem
  22. - apiserver-key.pem
  23. - kube-scheduler.pem
  24. - kube-scheduler-key.pem
  25. - kube-controller-manager.pem
  26. - kube-controller-manager-key.pem
  27. - admin-{{ inventory_hostname }}.pem
  28. - admin-{{ inventory_hostname }}-key.pem
  29. - node-{{ inventory_hostname }}.pem
  30. - node-{{ inventory_hostname }}-key.pem
  31. - kube-proxy-{{ inventory_hostname }}.pem
  32. - kube-proxy-{{ inventory_hostname }}-key.pem
  33. - name: "Check_certs | Set 'gen_certs' to true"
  34. set_fact:
  35. gen_certs: true
  36. when: "not item in kubecert_master.files|map(attribute='path') | list"
  37. run_once: true
  38. with_items: >-
  39. ['{{ kube_cert_dir }}/ca.pem',
  40. '{{ kube_cert_dir }}/apiserver.pem',
  41. '{{ kube_cert_dir }}/apiserver-key.pem',
  42. '{{ kube_cert_dir }}/kube-scheduler.pem',
  43. '{{ kube_cert_dir }}/kube-scheduler-key.pem',
  44. '{{ kube_cert_dir }}/kube-controller-manager.pem',
  45. '{{ kube_cert_dir }}/kube-controller-manager-key.pem',
  46. {% for host in groups['kube-master'] %}
  47. '{{ kube_cert_dir }}/admin-{{ host }}.pem'
  48. '{{ kube_cert_dir }}/admin-{{ host }}-key.pem'
  49. {% if not loop.last %}{{','}}{% endif %}
  50. {% endfor %}]
  51. {% for host in groups['k8s-cluster'] %}
  52. '{{ kube_cert_dir }}/node-{{ host }}.pem'
  53. '{{ kube_cert_dir }}/node-{{ host }}-key.pem'
  54. '{{ kube_cert_dir }}/kube-proxy-{{ host }}.pem'
  55. '{{ kube_cert_dir }}/kube-proxy-{{ host }}-key.pem'
  56. {% if not loop.last %}{{','}}{% endif %}
  57. {% endfor %}]
  58. - name: "Check_certs | Set 'gen_master_certs' to true"
  59. set_fact:
  60. gen_master_certs: |-
  61. {%- set gen = False -%}
  62. {% set existing_certs = kubecert_master.files|map(attribute='path')|list|sort %}
  63. {% for cert in ['apiserver.pem', 'apiserver-key.pem', 'kube-scheduler.pem',
  64. 'kube-scheduler-key.pem', 'kube-controller-manager.pem',
  65. 'kube-controller-manager-key.pem'] -%}
  66. {% set cert_file = "%s/%s.pem"|format(kube_cert_dir, cert) %}
  67. {% if not cert_file in existing_certs -%}
  68. {%- set gen = True -%}
  69. {% endif -%}
  70. {% endfor %}
  71. {{ gen }}
  72. run_once: true
  73. - name: "Check_certs | Set 'gen_node_certs' to true"
  74. set_fact:
  75. gen_node_certs: |-
  76. {
  77. {% set existing_certs = kubecert_master.files|map(attribute='path')|list|sort %}
  78. {% for host in groups['k8s-cluster'] -%}
  79. {% set host_cert = "%s/node-%s-key.pem"|format(kube_cert_dir, host) %}
  80. {% set kube_proxy_cert = "%s/kube-proxy-%s-key.pem"|format(kube_cert_dir, host) %}
  81. {% if host_cert in existing_certs and kube_proxy_cert in existing_certs -%}
  82. "{{ host }}": False,
  83. {% else -%}
  84. "{{ host }}": True,
  85. {% endif -%}
  86. {% endfor %}
  87. }
  88. run_once: true
  89. - name: "Check_certs | Set 'sync_certs' to true"
  90. set_fact:
  91. sync_certs: true
  92. when: |-
  93. {%- set certs = {'sync': False} -%}
  94. {% if gen_node_certs[inventory_hostname] or
  95. (not kubecert_node.results[0].stat.exists|default(False)) or
  96. (not kubecert_node.results[10].stat.exists|default(False)) or
  97. (kubecert_node.results[10].stat.checksum|default('') != kubecert_master.files|selectattr("path", "equalto", kubecert_node.results[10].stat.path)|map(attribute="checksum")|first|default('')) -%}
  98. {%- set _ = certs.update({'sync': True}) -%}
  99. {% endif %}
  100. {{ certs.sync }}