main.yml 5.6 KB

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