main.yml 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ---
  2. - fail:
  3. msg: "openshift_ca_host variable must be defined for this role"
  4. when: openshift_ca_host is not defined
  5. - fail:
  6. msg: "Both 'certfile' and 'keyfile' keys must be supplied when configuring openshift_master_ca_certificate"
  7. when: openshift_master_ca_certificate is defined and ('certfile' not in openshift_master_ca_certificate or 'keyfile' not in openshift_master_ca_certificate)
  8. - name: Install the base package for admin tooling
  9. package:
  10. name: "{{ openshift.common.service_type }}{{ openshift_pkg_version | default('') | oo_image_tag_to_rpm_version(include_dash=True) }}"
  11. state: present
  12. when: not openshift.common.is_containerized | bool
  13. register: install_result
  14. delegate_to: "{{ openshift_ca_host }}"
  15. run_once: true
  16. - name: Reload generated facts
  17. openshift_facts:
  18. when: hostvars[openshift_ca_host].install_result | changed
  19. - name: Create openshift_ca_config_dir if it does not exist
  20. file:
  21. path: "{{ openshift_ca_config_dir }}"
  22. state: directory
  23. delegate_to: "{{ openshift_ca_host }}"
  24. run_once: true
  25. - name: Determine if CA must be created
  26. stat:
  27. path: "{{ openshift_ca_config_dir }}/{{ item }}"
  28. register: g_master_ca_stat_result
  29. with_items:
  30. - ca-bundle.crt
  31. - ca.crt
  32. - ca.key
  33. delegate_to: "{{ openshift_ca_host }}"
  34. run_once: true
  35. - set_fact:
  36. master_ca_missing: "{{ False in (g_master_ca_stat_result.results
  37. | oo_collect(attribute='stat.exists')
  38. | list) }}"
  39. run_once: true
  40. - name: Retain original serviceaccount keys
  41. copy:
  42. src: "{{ item }}"
  43. dest: "{{ item }}.keep"
  44. remote_src: true
  45. with_items:
  46. - "{{ openshift_ca_config_dir }}/serviceaccounts.private.key"
  47. - "{{ openshift_ca_config_dir }}/serviceaccounts.public.key"
  48. when: openshift_certificates_redeploy | default(false) | bool
  49. - name: Deploy master ca certificate
  50. copy:
  51. src: "{{ item.src }}"
  52. dest: "{{ openshift_ca_config_dir }}/{{ item.dest }}"
  53. force: no
  54. with_items:
  55. - src: "{{ (openshift_master_ca_certificate | default({'certfile':none})).certfile }}"
  56. dest: ca.crt
  57. - src: "{{ (openshift_master_ca_certificate | default({'keyfile':none})).keyfile }}"
  58. dest: ca.key
  59. when: openshift_master_ca_certificate is defined
  60. delegate_to: "{{ openshift_ca_host }}"
  61. run_once: true
  62. - name: Create ca serial
  63. copy:
  64. content: "00"
  65. dest: "{{ openshift_ca_config_dir }}/ca.serial.txt"
  66. force: "{{ openshift_certificates_redeploy | default(false) | bool }}"
  67. when: openshift_master_ca_certificate is defined
  68. delegate_to: "{{ openshift_ca_host }}"
  69. run_once: true
  70. - find:
  71. paths: "{{ openshift.common.config_base }}/master/legacy-ca/"
  72. patterns: ".*-ca.crt"
  73. use_regex: true
  74. register: g_master_legacy_ca_result
  75. # This should NOT replace the CA due to --overwrite=false when a CA already exists.
  76. - name: Create the master certificates if they do not already exist
  77. command: >
  78. {{ hostvars[openshift_ca_host].openshift.common.client_binary }} adm ca create-master-certs
  79. {% for named_ca_certificate in openshift.master.named_certificates | default([]) | oo_collect('cafile') %}
  80. --certificate-authority {{ named_ca_certificate }}
  81. {% endfor %}
  82. {% for legacy_ca_certificate in g_master_legacy_ca_result.files | default([]) | oo_collect('path') %}
  83. --certificate-authority {{ legacy_ca_certificate }}
  84. {% endfor %}
  85. --hostnames={{ hostvars[openshift_ca_host].openshift.common.all_hostnames | join(',') }}
  86. --master={{ openshift.master.api_url }}
  87. --public-master={{ openshift.master.public_api_url }}
  88. --cert-dir={{ openshift_ca_config_dir }}
  89. {% if openshift_version | oo_version_gte_3_5_or_1_5(openshift.common.deployment_type) | bool %}
  90. --expire-days={{ openshift_master_cert_expire_days }}
  91. --signer-expire-days={{ openshift_ca_cert_expire_days }}
  92. {% endif %}
  93. --overwrite=false
  94. when: master_ca_missing | bool or openshift_certificates_redeploy | default(false) | bool
  95. delegate_to: "{{ openshift_ca_host }}"
  96. run_once: true
  97. - name: Test local loopback context
  98. command: >
  99. {{ hostvars[openshift_ca_host].openshift.common.client_binary }} config view
  100. --config={{ openshift_master_loopback_config }}
  101. changed_when: false
  102. register: loopback_config
  103. delegate_to: "{{ openshift_ca_host }}"
  104. run_once: true
  105. # create-api-client-config generates a ca.crt file which will
  106. # overwrite the OpenShift CA certificate. Generate the loopback
  107. # kubeconfig in a temporary directory and then copy files into the
  108. # master config dir to avoid overwriting ca.crt.
  109. - block:
  110. - name: Create temp directory for loopback master client config
  111. command: mktemp -d /tmp/openshift-ansible-XXXXXX
  112. register: openshift_ca_loopback_tmpdir
  113. - name: Generate the loopback master client config
  114. command: >
  115. {{ hostvars[openshift_ca_host].openshift.common.client_binary }} adm create-api-client-config
  116. --certificate-authority={{ openshift_ca_cert }}
  117. {% for named_ca_certificate in openshift.master.named_certificates | default([]) | oo_collect('cafile') %}
  118. --certificate-authority {{ named_ca_certificate }}
  119. {% endfor %}
  120. --client-dir={{ openshift_ca_loopback_tmpdir.stdout }}
  121. --groups=system:masters,system:openshift-master
  122. --master={{ hostvars[openshift_ca_host].openshift.master.loopback_api_url }}
  123. --public-master={{ hostvars[openshift_ca_host].openshift.master.loopback_api_url }}
  124. --signer-cert={{ openshift_ca_cert }}
  125. --signer-key={{ openshift_ca_key }}
  126. --signer-serial={{ openshift_ca_serial }}
  127. --user=system:openshift-master
  128. --basename=openshift-master
  129. {% if openshift_version | oo_version_gte_3_5_or_1_5(openshift.common.deployment_type) | bool %}
  130. --expire-days={{ openshift_master_cert_expire_days }}
  131. {% endif %}
  132. - name: Copy generated loopback master client config to master config dir
  133. copy:
  134. src: "{{ openshift_ca_loopback_tmpdir.stdout }}/{{ item }}"
  135. dest: "{{ openshift_ca_config_dir }}"
  136. remote_src: true
  137. with_items:
  138. - openshift-master.crt
  139. - openshift-master.key
  140. - openshift-master.kubeconfig
  141. - name: Delete temp directory
  142. file:
  143. name: "{{ openshift_ca_loopback_tmpdir.stdout }}"
  144. state: absent
  145. when: loopback_context_string not in loopback_config.stdout
  146. delegate_to: "{{ openshift_ca_host }}"
  147. run_once: true
  148. - name: Restore original serviceaccount keys
  149. copy:
  150. src: "{{ item }}.keep"
  151. dest: "{{ item }}"
  152. remote_src: true
  153. with_items:
  154. - "{{ openshift_ca_config_dir }}/serviceaccounts.private.key"
  155. - "{{ openshift_ca_config_dir }}/serviceaccounts.public.key"
  156. when: openshift_certificates_redeploy | default(false) | bool
  157. - name: Remove backup serviceaccount keys
  158. file:
  159. path: "{{ item }}.keep"
  160. state: absent
  161. with_items:
  162. - "{{ openshift_ca_config_dir }}/serviceaccounts.private.key"
  163. - "{{ openshift_ca_config_dir }}/serviceaccounts.public.key"
  164. when: openshift_certificates_redeploy | default(false) | bool