main.yml 6.9 KB

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