main.yml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. ---
  2. - name: Check status of etcd certificates
  3. stat:
  4. path: "{{ etcd_cert_config_dir }}/{{ item }}"
  5. with_items:
  6. - "{{ etcd_cert_prefix }}server.crt"
  7. - "{{ etcd_cert_prefix }}peer.crt"
  8. - "{{ etcd_cert_prefix }}ca.crt"
  9. register: g_etcd_server_cert_stat_result
  10. - set_fact:
  11. etcd_server_certs_missing: "{{ False in (g_etcd_server_cert_stat_result.results
  12. | oo_collect(attribute='stat.exists')
  13. | list) }}"
  14. - name: Ensure generated_certs directory present
  15. file:
  16. path: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  17. state: directory
  18. mode: 0700
  19. when: etcd_server_certs_missing | bool
  20. delegate_to: "{{ etcd_ca_host }}"
  21. - name: Create the server csr
  22. command: >
  23. openssl req -new -keyout {{ etcd_cert_prefix }}server.key
  24. -config {{ etcd_openssl_conf }}
  25. -out {{ etcd_cert_prefix }}server.csr
  26. -reqexts {{ etcd_req_ext }} -batch -nodes
  27. -subj /CN={{ etcd_hostname }}
  28. args:
  29. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  30. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  31. ~ etcd_cert_prefix ~ 'server.csr' }}"
  32. environment:
  33. SAN: "IP:{{ etcd_ip }}"
  34. when: etcd_server_certs_missing | bool
  35. delegate_to: "{{ etcd_ca_host }}"
  36. # Certificates must be signed serially in order to avoid competing
  37. # for the serial file.
  38. - name: Sign and create the server crt
  39. delegated_serial_command:
  40. command: >
  41. openssl ca -name {{ etcd_ca_name }} -config {{ etcd_openssl_conf }}
  42. -out {{ etcd_cert_prefix }}server.crt
  43. -in {{ etcd_cert_prefix }}server.csr
  44. -extensions {{ etcd_ca_exts_server }} -batch
  45. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  46. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  47. ~ etcd_cert_prefix ~ 'server.crt' }}"
  48. environment:
  49. SAN: "IP:{{ etcd_ip }}"
  50. delegate_to: "{{ etcd_ca_host }}"
  51. - name: Create the peer csr
  52. command: >
  53. openssl req -new -keyout {{ etcd_cert_prefix }}peer.key
  54. -config {{ etcd_openssl_conf }}
  55. -out {{ etcd_cert_prefix }}peer.csr
  56. -reqexts {{ etcd_req_ext }} -batch -nodes
  57. -subj /CN={{ etcd_hostname }}
  58. args:
  59. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  60. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  61. ~ etcd_cert_prefix ~ 'peer.csr' }}"
  62. environment:
  63. SAN: "IP:{{ etcd_ip }}"
  64. when: etcd_server_certs_missing | bool
  65. delegate_to: "{{ etcd_ca_host }}"
  66. - name: Sign and create the peer crt
  67. delegated_serial_command:
  68. command: >
  69. openssl ca -name {{ etcd_ca_name }} -config {{ etcd_openssl_conf }}
  70. -out {{ etcd_cert_prefix }}peer.crt
  71. -in {{ etcd_cert_prefix }}peer.csr
  72. -extensions {{ etcd_ca_exts_peer }} -batch
  73. chdir: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}"
  74. creates: "{{ etcd_generated_certs_dir ~ '/' ~ etcd_cert_subdir ~ '/'
  75. ~ etcd_cert_prefix ~ 'peer.crt' }}"
  76. environment:
  77. SAN: "IP:{{ etcd_ip }}"
  78. when: etcd_server_certs_missing | bool
  79. delegate_to: "{{ etcd_ca_host }}"
  80. - file:
  81. src: "{{ etcd_ca_cert }}"
  82. dest: "{{ etcd_generated_certs_dir}}/{{ etcd_cert_subdir }}/{{ etcd_cert_prefix }}ca.crt"
  83. state: hard
  84. when: etcd_server_certs_missing | bool
  85. delegate_to: "{{ etcd_ca_host }}"
  86. - name: Create local temp directory for syncing certs
  87. local_action: command mktemp -d /tmp/etcd_certificates-XXXXXXX
  88. register: g_etcd_server_mktemp
  89. changed_when: False
  90. when: etcd_server_certs_missing | bool
  91. delegate_to: localhost
  92. - name: Create a tarball of the etcd certs
  93. command: >
  94. tar -czvf {{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz
  95. -C {{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }} .
  96. args:
  97. creates: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz"
  98. when: etcd_server_certs_missing | bool
  99. delegate_to: "{{ etcd_ca_host }}"
  100. - name: Retrieve etcd cert tarball
  101. fetch:
  102. src: "{{ etcd_generated_certs_dir }}/{{ etcd_cert_subdir }}.tgz"
  103. dest: "{{ g_etcd_server_mktemp.stdout }}/"
  104. flat: yes
  105. fail_on_missing: yes
  106. validate_checksum: yes
  107. when: etcd_server_certs_missing | bool
  108. delegate_to: "{{ etcd_ca_host }}"
  109. - name: Ensure certificate directory exists
  110. file:
  111. path: "{{ etcd_cert_config_dir }}"
  112. state: directory
  113. when: etcd_server_certs_missing | bool
  114. - name: Unarchive cert tarball
  115. unarchive:
  116. src: "{{ g_etcd_server_mktemp.stdout }}/{{ etcd_cert_subdir }}.tgz"
  117. dest: "{{ etcd_cert_config_dir }}"
  118. when: etcd_server_certs_missing | bool
  119. - name: Delete temporary directory
  120. file: name={{ g_etcd_server_mktemp.stdout }} state=absent
  121. changed_when: False
  122. when: etcd_server_certs_missing | bool
  123. delegate_to: localhost