main.yml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ---
  2. - name: Ensure CA certificate exists on openshift_ca_host
  3. stat:
  4. path: "{{ openshift_ca_cert }}"
  5. register: g_ca_cert_stat_result
  6. delegate_to: "{{ openshift_ca_host }}"
  7. run_once: true
  8. - fail:
  9. msg: >
  10. CA certificate {{ openshift_ca_cert }} doesn't exist on CA host
  11. {{ openshift_ca_host }}. Apply 'openshift_ca' role to
  12. {{ openshift_ca_host }}.
  13. when: not g_ca_cert_stat_result.stat.exists | bool
  14. run_once: true
  15. - name: Check status of node certificates
  16. stat:
  17. path: "{{ openshift.common.config_base }}/node/{{ item }}"
  18. with_items:
  19. - "system:node:{{ openshift.common.hostname }}.crt"
  20. - "system:node:{{ openshift.common.hostname }}.key"
  21. - "system:node:{{ openshift.common.hostname }}.kubeconfig"
  22. - ca.crt
  23. - server.key
  24. - server.crt
  25. register: g_node_cert_stat_result
  26. when: not openshift_certificates_redeploy | default(false) | bool
  27. - set_fact:
  28. node_certs_missing: "{{ true if openshift_certificates_redeploy | default(false) | bool
  29. else (False in (g_node_cert_stat_result.results
  30. | default({})
  31. | oo_collect(attribute='stat.exists')
  32. | list)) }}"
  33. - name: Create openshift_generated_configs_dir if it does not exist
  34. file:
  35. path: "{{ openshift_generated_configs_dir }}"
  36. state: directory
  37. mode: 0700
  38. when: node_certs_missing | bool
  39. delegate_to: "{{ openshift_ca_host }}"
  40. - find:
  41. paths: "{{ openshift.common.config_base }}/master/legacy-ca/"
  42. patterns: ".*-ca.crt"
  43. use_regex: true
  44. register: g_master_legacy_ca_result
  45. delegate_to: "{{ openshift_ca_host }}"
  46. - name: Generate the node client config
  47. command: >
  48. {{ hostvars[openshift_ca_host].openshift.common.client_binary }} adm create-api-client-config
  49. {% for named_ca_certificate in hostvars[openshift_ca_host].openshift.master.named_certificates | default([]) | oo_collect('cafile') %}
  50. --certificate-authority {{ named_ca_certificate }}
  51. {% endfor %}
  52. {% for legacy_ca_certificate in g_master_legacy_ca_result.files | default([]) | oo_collect('path') %}
  53. --certificate-authority {{ legacy_ca_certificate }}
  54. {% endfor %}
  55. --certificate-authority={{ openshift_ca_cert }}
  56. --client-dir={{ openshift_generated_configs_dir }}/node-{{ hostvars[item].openshift.common.hostname }}
  57. --groups=system:nodes
  58. --master={{ hostvars[openshift_ca_host].openshift.master.api_url }}
  59. --signer-cert={{ openshift_ca_cert }}
  60. --signer-key={{ openshift_ca_key }}
  61. --signer-serial={{ openshift_ca_serial }}
  62. --user=system:node:{{ hostvars[item].openshift.common.hostname }}
  63. {% if openshift_version | oo_version_gte_3_5_or_1_5(openshift.common.deployment_type) | bool %}
  64. --expire-days={{ openshift_node_cert_expire_days }}
  65. {% endif %}
  66. args:
  67. creates: "{{ openshift_generated_configs_dir }}/node-{{ hostvars[item].openshift.common.hostname }}"
  68. with_items: "{{ hostvars
  69. | oo_select_keys(groups['oo_nodes_to_config'])
  70. | oo_collect(attribute='inventory_hostname', filters={'node_certs_missing':True}) }}"
  71. delegate_to: "{{ openshift_ca_host }}"
  72. run_once: true
  73. - name: Generate the node server certificate
  74. command: >
  75. {{ hostvars[openshift_ca_host].openshift.common.client_binary }} adm ca create-server-cert
  76. --cert={{ openshift_generated_configs_dir }}/node-{{ hostvars[item].openshift.common.hostname }}/server.crt
  77. --key={{ openshift_generated_configs_dir }}/node-{{ hostvars[item].openshift.common.hostname }}/server.key
  78. {% if openshift_version | oo_version_gte_3_5_or_1_5(openshift.common.deployment_type) | bool %}
  79. --expire-days={{ openshift_node_cert_expire_days }}
  80. {% endif %}
  81. --overwrite=true
  82. --hostnames={{ hostvars[item].openshift.common.hostname }},{{ hostvars[item].openshift.common.public_hostname }},{{ hostvars[item].openshift.common.ip }},{{ hostvars[item].openshift.common.public_ip }}
  83. --signer-cert={{ openshift_ca_cert }}
  84. --signer-key={{ openshift_ca_key }}
  85. --signer-serial={{ openshift_ca_serial }}
  86. args:
  87. creates: "{{ openshift_generated_configs_dir }}/node-{{ hostvars[item].openshift.common.hostname }}/server.crt"
  88. with_items: "{{ hostvars
  89. | oo_select_keys(groups['oo_nodes_to_config'])
  90. | oo_collect(attribute='inventory_hostname', filters={'node_certs_missing':True}) }}"
  91. delegate_to: "{{ openshift_ca_host }}"
  92. run_once: true
  93. - name: Create local temp directory for syncing certs
  94. local_action: command mktemp -d /tmp/openshift-ansible-XXXXXXX
  95. register: node_cert_mktemp
  96. changed_when: False
  97. when: node_certs_missing | bool
  98. become: no
  99. - name: Create a tarball of the node config directories
  100. command: >
  101. tar -czvf {{ openshift_node_generated_config_dir }}.tgz
  102. --transform 's|system:{{ openshift_node_cert_subdir }}|node|'
  103. -C {{ openshift_node_generated_config_dir }} .
  104. args:
  105. creates: "{{ openshift_node_generated_config_dir }}.tgz"
  106. # Disables the following warning:
  107. # Consider using unarchive module rather than running tar
  108. warn: no
  109. when: node_certs_missing | bool
  110. delegate_to: "{{ openshift_ca_host }}"
  111. - name: Retrieve the node config tarballs from the master
  112. fetch:
  113. src: "{{ openshift_node_generated_config_dir }}.tgz"
  114. dest: "{{ node_cert_mktemp.stdout }}/"
  115. flat: yes
  116. fail_on_missing: yes
  117. validate_checksum: yes
  118. when: node_certs_missing | bool
  119. delegate_to: "{{ openshift_ca_host }}"
  120. - name: Ensure certificate directory exists
  121. file:
  122. path: "{{ openshift_node_cert_dir }}"
  123. state: directory
  124. when: node_certs_missing | bool
  125. - name: Unarchive the tarball on the node
  126. unarchive:
  127. src: "{{ node_cert_mktemp.stdout }}/{{ openshift_node_cert_subdir }}.tgz"
  128. dest: "{{ openshift_node_cert_dir }}"
  129. when: node_certs_missing | bool
  130. - name: Delete local temp directory
  131. local_action: file path="{{ node_cert_mktemp.stdout }}" state=absent
  132. changed_when: False
  133. when: node_certs_missing | bool
  134. become: no
  135. - name: Copy OpenShift CA to system CA trust
  136. copy:
  137. src: "{{ item.cert }}"
  138. dest: "/etc/pki/ca-trust/source/anchors/{{ item.id }}-{{ item.cert | basename }}"
  139. remote_src: yes
  140. with_items:
  141. - id: openshift
  142. cert: "{{ openshift_node_cert_dir }}/ca.crt"
  143. notify:
  144. - update ca trust