main.yml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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' 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. | 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 }}"
  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. - name: Sign and create the client crt
  55. delegated_serial_command:
  56. command: >
  57. openssl ca -name {{ etcd_ca_name }} -config {{ etcd_openssl_conf }}
  58. -out {{ etcd_cert_prefix }}client.crt
  59. -in {{ etcd_cert_prefix }}client.csr
  60. -batch
  61. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  62. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  63. ~ etcd_cert_prefix ~ 'client.crt' }}"
  64. environment:
  65. SAN: "IP:{{ etcd_ip }}"
  66. when: etcd_client_certs_missing | bool
  67. delegate_to: "{{ etcd_ca_host }}"
  68. - file:
  69. src: "{{ etcd_ca_cert }}"
  70. dest: "{{ etcd_generated_certs_dir}}/{{ etcd_cert_subdir }}/{{ etcd_cert_prefix }}ca.crt"
  71. state: hard
  72. when: etcd_client_certs_missing | bool
  73. delegate_to: "{{ etcd_ca_host }}"
  74. - name: Create local temp directory for syncing certs
  75. local_action: command mktemp -d /tmp/etcd_certificates-XXXXXXX
  76. register: g_etcd_client_mktemp
  77. changed_when: False
  78. when: etcd_client_certs_missing | bool
  79. delegate_to: localhost
  80. become: no
  81. - name: Create a tarball of the etcd certs
  82. command: >
  83. tar -czvf {{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz
  84. -C {{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }} .
  85. args:
  86. creates: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz"
  87. when: etcd_client_certs_missing | bool
  88. delegate_to: "{{ etcd_ca_host }}"
  89. - name: Retrieve the etcd cert tarballs
  90. fetch:
  91. src: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz"
  92. dest: "{{ g_etcd_client_mktemp.stdout }}/"
  93. flat: yes
  94. fail_on_missing: yes
  95. validate_checksum: yes
  96. when: etcd_client_certs_missing | bool
  97. delegate_to: "{{ etcd_ca_host }}"
  98. - name: Ensure certificate directory exists
  99. file:
  100. path: "{{ etcd_cert_config_dir }}"
  101. state: directory
  102. when: etcd_client_certs_missing | bool
  103. - name: Unarchive etcd cert tarballs
  104. unarchive:
  105. src: "{{ g_etcd_client_mktemp.stdout }}/{{ etcd_cert_subdir }}.tgz"
  106. dest: "{{ etcd_cert_config_dir }}"
  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
  118. - name: Delete temporary directory
  119. file: name={{ g_etcd_client_mktemp.stdout }} state=absent
  120. changed_when: False
  121. when: etcd_client_certs_missing | bool
  122. delegate_to: localhost
  123. become: no