Playbooks to a new Lilik
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.5 KiB

  1. ---
  2. - name: 'TLS | verify if cert is valid'
  3. command: >
  4. openssl verify
  5. -CAfile {{ ca_cert_tls_ca_path }}
  6. -untrusted {{ ca_cert_tls_cert_path }}
  7. -verify_hostname {{ ca_cert_common_name }}
  8. {{ ca_cert_tls_cert_path }}
  9. register: ca_cert_tls_cert_is_valid
  10. check_mode: false
  11. changed_when: ca_cert_tls_cert_is_valid.rc != 0
  12. failed_when: false
  13. when: ca_cert_proto == 'tls'
  14. - name: 'SSH | verify if cert is valid and get info'
  15. ssh_cert:
  16. path: '{{ ca_cert_ssh_key_path }}-cert.pub'
  17. ca_path: '{{ ca_cert_ssh_ca_path }}'
  18. principals: [ '{{ ca_cert_common_name }}' ]
  19. register: ca_cert_ssh_cert_is_valid
  20. changed_when: ca_cert_ssh_cert_is_valid.rc != 0
  21. ignore_errors: true
  22. check_mode: false
  23. when: ca_cert_proto == 'ssh'
  24. - name: 'TLS | get remaining validity'
  25. shell: >
  26. {% if ansible_distribution != 'OpenWrt' %}
  27. echo $(( ($(date -d "$(openssl x509 -in {{ ca_cert_tls_cert_path }} -enddate -noout | sed "s/.*=\(.*\)/\1/")" +%s)-$(date -d now +%s))/86400 ))
  28. {% else %}
  29. echo $(( ($(date -D '%b %e %H:%M:%S %Y' -d "$(openssl x509 -in {{ ca_cert_tls_cert_path }} -enddate -noout | sed "s/.*=\(.*\)/\1/")" +%s)-$(date +%s))/86400 ))
  30. {% endif %}
  31. register: ca_cert_cert_remaining_days
  32. changed_when: false
  33. check_mode: false
  34. when: ca_cert_proto == 'tls' and not ca_cert_tls_cert_is_valid.changed
  35. - name: 'set cert validity'
  36. set_fact:
  37. ca_cert_cert_is_valid: >-
  38. {% if ca_cert_proto == 'tls' %}{{ ca_cert_tls_cert_is_valid }}{%
  39. elif ca_cert_proto == 'ssh' %}{{ ca_cert_ssh_cert_is_valid }}{% endif %}
  40. - name: 'set remaning validity'
  41. set_fact:
  42. ca_cert_cert_remaining_days: >-
  43. {% if ca_cert_proto == 'tls' %}{{ ca_cert_cert_remaining_days.stdout }}{%
  44. elif ca_cert_proto == 'ssh' %}{{ ca_cert_cert_is_valid.certificate.valid.remaining_days }}{% endif %}
  45. when: ca_cert_cert_is_valid.rc|d(1) == 0
  46. - name: 'renew'
  47. block:
  48. - name: 'RENEW | backup existing private keys'
  49. copy:
  50. remote_src: true
  51. src: '{{ item }}'
  52. dest: '{{ item }}-backup'
  53. failed_when: false
  54. register: ca_cert_key_backup
  55. loop: '{{ keypair[ca_cert_proto] }}'
  56. vars:
  57. keypair:
  58. ssh:
  59. - '{{ ca_cert_ssh_key_path }}'
  60. - '{{ ca_cert_ssh_key_path }}.pub'
  61. tls:
  62. - '{{ ca_cert_tls_key_path }}'
  63. - name: 'RENEW | TLS | create private key (if not exists)'
  64. command: >
  65. openssl genpkey
  66. -algorithm {{ ca_cert_tls_key_algorithm }}
  67. -out {{ ca_cert_tls_key_path }}
  68. args:
  69. creates: >-
  70. {{ "" if ca_cert_renew_private_key else ca_cert_tls_key_path }}
  71. when: ca_cert_proto == 'tls'
  72. - name: 'RENEW | SSH | create key pair'
  73. openssh_keypair:
  74. force: '{{ ca_cert_renew_private_key }}'
  75. path: '{{ ca_cert_ssh_key_path }}'
  76. type: 'ed25519'
  77. when: ca_cert_proto == 'ssh'
  78. - name: 'RENEW | TLS | create cert signing request'
  79. command: >
  80. openssl req
  81. -new
  82. -subj '{{ ca_cert_tls_subj }}'
  83. -key '{{ ca_cert_tls_key_path }}'
  84. -out '{{ ca_cert_tls_csr_path }}'
  85. when: ca_cert_proto == 'tls'
  86. - name: 'RENEW | CA_MANAGER | generate json signing request'
  87. cert_request:
  88. host: '{{ ca_cert_common_name }}'
  89. path: >-
  90. {% if ca_cert_proto == 'tls' %}{{ ca_cert_tls_csr_path }}{%
  91. elif ca_cert_proto == 'ssh' %}{{ ca_cert_ssh_key_path+'.pub' }}{% endif %}
  92. proto: '{{ "ssl" if ca_cert_proto == "tls" else ca_cert_proto }}'
  93. client: '{{ ca_cert_client }}'
  94. register: ca_cert_signing_request
  95. - name: 'RENEW | CA_MANAGER | send signing request'
  96. raw: '{{ ca_cert_signing_request | to_json }}'
  97. delegate_to: '{{ ca_cert_ca_manager_host }}'
  98. delegate_facts: true
  99. register: ca_cert_signing_request_results
  100. failed_when: (ca_cert_signing_request_results.stdout|from_json).failed
  101. - name: 'RENEW | CA_MANAGER | set signing request id'
  102. set_fact:
  103. ca_cert_request_id: >-
  104. {{ (ca_cert_signing_request_results.stdout|from_json).requestID }}
  105. - name: 'RENEW | CA_MANAGER | generate json get request'
  106. set_fact:
  107. ca_cert_get_request:
  108. type: 'get_certificate'
  109. requestID: '{{ ca_cert_request_id }}'
  110. - name: 'RENEW | CA_MANAGER | prompt for signature'
  111. debug:
  112. msg: >-
  113. Please manually confirm sign request with id {{ ca_cert_request_id }}.
  114. - name: 'RENEW | CA_MANAGER | send get request'
  115. raw: '{{ ca_cert_get_request | to_json }}'
  116. delegate_to: '{{ ca_cert_ca_manager_host }}'
  117. delegate_facts: true
  118. register: ca_cert_get_request_results
  119. failed_when: (ca_cert_get_request_results.stdout|from_json).failed
  120. - name: 'RENEW | store new certificate'
  121. copy:
  122. content: '{{ (ca_cert_get_request_results.stdout|from_json).result }}'
  123. dest: >-
  124. {% if ca_cert_proto == 'tls' %}{{ ca_cert_tls_cert_path }}{%
  125. elif ca_cert_proto == 'ssh' %}{{ ca_cert_ssh_key_path }}-cert.pub{% endif %}
  126. rescue:
  127. - name: 'RENEW FAILED | restore backup'
  128. copy:
  129. remote_src: true
  130. src: '{{ item.dest }}'
  131. dest: '{{ item.src }}'
  132. when: not item.failed
  133. loop: '{{ ca_cert_key_backup.results }}'
  134. always:
  135. - name: 'RENEW | clean backup'
  136. file:
  137. path: '{{ item.dest }}'
  138. state: 'absent'
  139. when: not item.failed
  140. loop: '{{ ca_cert_key_backup.results }}'
  141. when: ca_cert_cert_is_valid.changed or ca_cert_cert_remaining_days|int < ca_cert_min_days_validity
  142. ...