fetch_client_certificates_from_ca.yml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. ---
  2. - name: Ensure CA certificate exists on etcd_ca_host
  3. stat:
  4. path: "{{ etcd_ca_cert }}"
  5. register: g_ca_cert_stat_result
  6. delegate_to: "{{ etcd_ca_host }}"
  7. run_once: true
  8. - fail:
  9. msg: >
  10. CA certificate {{ etcd_ca_cert }} doesn't exist on CA host
  11. {{ etcd_ca_host }}. Apply 'etcd_ca' action from `etcd` role to
  12. {{ etcd_ca_host }}.
  13. when: not g_ca_cert_stat_result.stat.exists | bool
  14. run_once: true
  15. - name: Check status of external etcd certificatees
  16. stat:
  17. path: "{{ etcd_cert_config_dir }}/{{ item }}"
  18. with_items:
  19. - "{{ etcd_cert_prefix }}client.crt"
  20. - "{{ etcd_cert_prefix }}client.key"
  21. - "{{ etcd_cert_prefix }}ca.crt"
  22. register: g_external_etcd_cert_stat_result
  23. when: not etcd_certificates_redeploy | default(false) | bool
  24. - set_fact:
  25. etcd_client_certs_missing: "{{ true if etcd_certificates_redeploy | default(false) | bool
  26. else (False in (g_external_etcd_cert_stat_result.results
  27. | default({})
  28. | lib_utils_oo_collect(attribute='stat.exists')
  29. | list)) }}"
  30. - name: Ensure generated_certs directory present
  31. file:
  32. path: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  33. state: directory
  34. mode: 0700
  35. when: etcd_client_certs_missing | bool
  36. delegate_to: "{{ etcd_ca_host }}"
  37. - name: Create the client csr
  38. command: >
  39. openssl req -new -keyout {{ etcd_cert_prefix }}client.key
  40. -config {{ etcd_openssl_conf }}
  41. -out {{ etcd_cert_prefix }}client.csr
  42. -reqexts {{ etcd_req_ext }} -batch -nodes
  43. -subj /CN={{ etcd_hostname }}
  44. args:
  45. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  46. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  47. ~ etcd_cert_prefix ~ 'client.csr' }}"
  48. environment:
  49. SAN: "IP:{{ etcd_ip }},DNS:{{ etcd_hostname }}"
  50. when: etcd_client_certs_missing | bool
  51. delegate_to: "{{ etcd_ca_host }}"
  52. # Certificates must be signed serially in order to avoid competing
  53. # for the serial file.
  54. # delegated_serial_command is a custom module in lib_utils
  55. - name: Sign and create the client crt
  56. delegated_serial_command:
  57. command: >
  58. openssl ca -name {{ etcd_ca_name }} -config {{ etcd_openssl_conf }}
  59. -out {{ etcd_cert_prefix }}client.crt
  60. -in {{ etcd_cert_prefix }}client.csr
  61. -batch
  62. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  63. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  64. ~ etcd_cert_prefix ~ 'client.crt' }}"
  65. environment:
  66. SAN: "IP:{{ etcd_ip }}"
  67. when: etcd_client_certs_missing | bool
  68. delegate_to: "{{ etcd_ca_host }}"
  69. - file:
  70. src: "{{ etcd_ca_cert }}"
  71. dest: "{{ etcd_generated_certs_dir}}/{{ etcd_cert_subdir }}/{{ etcd_cert_prefix }}ca.crt"
  72. state: hard
  73. when: etcd_client_certs_missing | bool
  74. delegate_to: "{{ etcd_ca_host }}"
  75. - name: Create a tarball of the etcd certs
  76. command: >
  77. tar -czvf {{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz
  78. -C {{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }} .
  79. args:
  80. creates: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz"
  81. # Disables the following warning:
  82. # Consider using unarchive module rather than running tar
  83. warn: no
  84. when: etcd_client_certs_missing | bool
  85. delegate_to: "{{ etcd_ca_host }}"
  86. - name: Retrieve the etcd cert tarballs
  87. fetch:
  88. src: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz"
  89. dest: "/tmp"
  90. fail_on_missing: yes
  91. validate_checksum: yes
  92. when: etcd_client_certs_missing | bool
  93. delegate_to: "{{ etcd_ca_host }}"
  94. - name: Ensure certificate directory exists
  95. file:
  96. path: "{{ etcd_cert_config_dir }}"
  97. state: directory
  98. when: etcd_client_certs_missing | bool
  99. - name: Unarchive etcd cert tarballs
  100. unarchive:
  101. src: "/tmp/{{ inventory_hostname }}/{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz"
  102. dest: "{{ etcd_cert_config_dir }}"
  103. when: etcd_client_certs_missing | bool
  104. - name: Delete temporary directory
  105. local_action: file path="/tmp/{{ inventory_hostname }}" state=absent
  106. changed_when: False
  107. when: etcd_client_certs_missing | bool
  108. - file:
  109. path: "{{ etcd_cert_config_dir }}/{{ item }}"
  110. owner: root
  111. group: root
  112. mode: 0600
  113. with_items:
  114. - "{{ etcd_cert_prefix }}client.crt"
  115. - "{{ etcd_cert_prefix }}client.key"
  116. - "{{ etcd_cert_prefix }}ca.crt"
  117. when: etcd_client_certs_missing | bool