main.yml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. args:
  64. creates: "{{ openshift_generated_configs_dir }}/node-{{ hostvars[item].openshift.common.hostname }}"
  65. with_items: "{{ hostvars
  66. | oo_select_keys(groups['oo_nodes_to_config'])
  67. | oo_collect(attribute='inventory_hostname', filters={'node_certs_missing':True}) }}"
  68. delegate_to: "{{ openshift_ca_host }}"
  69. run_once: true
  70. - name: Generate the node server certificate
  71. command: >
  72. {{ hostvars[openshift_ca_host].openshift.common.client_binary }} adm ca create-server-cert
  73. --cert={{ openshift_generated_configs_dir }}/node-{{ hostvars[item].openshift.common.hostname }}/server.crt
  74. --key={{ openshift_generated_configs_dir }}/node-{{ hostvars[item].openshift.common.hostname }}/server.key
  75. --overwrite=true
  76. --hostnames={{ hostvars[item].openshift.common.hostname }},{{ hostvars[item].openshift.common.public_hostname }},{{ hostvars[item].openshift.common.ip }},{{ hostvars[item].openshift.common.public_ip }}
  77. --signer-cert={{ openshift_ca_cert }}
  78. --signer-key={{ openshift_ca_key }}
  79. --signer-serial={{ openshift_ca_serial }}
  80. args:
  81. creates: "{{ openshift_generated_configs_dir }}/node-{{ hostvars[item].openshift.common.hostname }}/server.crt"
  82. with_items: "{{ hostvars
  83. | oo_select_keys(groups['oo_nodes_to_config'])
  84. | oo_collect(attribute='inventory_hostname', filters={'node_certs_missing':True}) }}"
  85. delegate_to: "{{ openshift_ca_host }}"
  86. run_once: true
  87. - name: Create local temp directory for syncing certs
  88. local_action: command mktemp -d /tmp/openshift-ansible-XXXXXXX
  89. register: node_cert_mktemp
  90. changed_when: False
  91. when: node_certs_missing | bool
  92. delegate_to: localhost
  93. become: no
  94. - name: Create a tarball of the node config directories
  95. command: >
  96. tar -czvf {{ openshift_node_generated_config_dir }}.tgz
  97. --transform 's|system:{{ openshift_node_cert_subdir }}|node|'
  98. -C {{ openshift_node_generated_config_dir }} .
  99. args:
  100. creates: "{{ openshift_node_generated_config_dir }}.tgz"
  101. # Disables the following warning:
  102. # Consider using unarchive module rather than running tar
  103. warn: no
  104. when: node_certs_missing | bool
  105. delegate_to: "{{ openshift_ca_host }}"
  106. - name: Retrieve the node config tarballs from the master
  107. fetch:
  108. src: "{{ openshift_node_generated_config_dir }}.tgz"
  109. dest: "{{ node_cert_mktemp.stdout }}/"
  110. flat: yes
  111. fail_on_missing: yes
  112. validate_checksum: yes
  113. when: node_certs_missing | bool
  114. delegate_to: "{{ openshift_ca_host }}"
  115. - name: Ensure certificate directory exists
  116. file:
  117. path: "{{ openshift_node_cert_dir }}"
  118. state: directory
  119. when: node_certs_missing | bool
  120. - name: Unarchive the tarball on the node
  121. unarchive:
  122. src: "{{ node_cert_mktemp.stdout }}/{{ openshift_node_cert_subdir }}.tgz"
  123. dest: "{{ openshift_node_cert_dir }}"
  124. when: node_certs_missing | bool
  125. - file: name={{ node_cert_mktemp.stdout }} state=absent
  126. changed_when: False
  127. when: node_certs_missing | bool
  128. delegate_to: localhost
  129. become: no
  130. - name: Copy OpenShift CA to system CA trust
  131. copy:
  132. src: "{{ item.cert }}"
  133. dest: "/etc/pki/ca-trust/source/anchors/{{ item.id }}-{{ item.cert | basename }}"
  134. remote_src: yes
  135. with_items:
  136. - id: openshift
  137. cert: "{{ openshift_node_cert_dir }}/ca.crt"
  138. notify:
  139. - update ca trust