playbook.yaml.j2 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ---
  2. - name: {{ playbook_name }}
  3. hosts: {{ target_hosts }}
  4. {% if become %}
  5. become: true
  6. {% endif %}
  7. {% if options_enabled and not gather_facts %}
  8. gather_facts: false
  9. {% endif %}
  10. {% if secrets_enabled %}
  11. vars_files:
  12. - {{ secrets_file }}
  13. {% endif %}
  14. vars:
  15. certs_path: {{ certs_path }}
  16. cert_validity_days: {{ cert_validity_days }}
  17. cn_domain: {{ cn_domain }}
  18. tasks:
  19. - name: Check if docker certs are existing
  20. ansible.builtin.stat:
  21. path: {{ '{{' }} certs_path {{ '}}' }}
  22. register: certs_dir
  23. - name: Create docker certs directory (if needed)
  24. ansible.builtin.file:
  25. path: {{ '{{' }} certs_path {{ '}}' }}
  26. state: directory
  27. mode: '0700'
  28. when: not certs_dir.stat.exists
  29. - name: Check if docker certs directory is empty
  30. ansible.builtin.command: ls -A {{ '{{' }} certs_path {{ '}}' }}
  31. register: certs_list
  32. when: certs_dir.stat.exists
  33. changed_when: false
  34. ignore_errors: true
  35. - name: Fail if docker certs already exist
  36. ansible.builtin.fail:
  37. msg: "Docker certificates already exist in /root/docker-certs."
  38. when: certs_list.stdout | default('') != ''
  39. - name: Get machine's primary internal ip address from eth0 interface
  40. ansible.builtin.setup:
  41. register: ip_address
  42. - name: Set machine's primary internal ip address
  43. ansible.builtin.set_fact:
  44. ip_address: {{ '{{' }} ip_address.ansible_facts.ansible_default_ipv4.address {{ '}}' }}
  45. - name: Check if ip_address is a valid ip address
  46. ansible.builtin.assert:
  47. that:
  48. - ip_address is match("^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$")
  49. fail_msg: "ip_address is not a valid ip address."
  50. success_msg: "ip_address is a valid ip address."
  51. - name: Generate CA private key
  52. ansible.builtin.command:
  53. cmd: >
  54. openssl genrsa -out {{ '{{' }} certs_path {{ '}}' }}/ca-key.pem 4096
  55. args:
  56. creates: {{ '{{' }} certs_path {{ '}}' }}/ca-key.pem
  57. - name: Generate CA certificate
  58. ansible.builtin.command:
  59. cmd: >
  60. openssl req -sha256 -new -x509
  61. -subj "/CN={{ '{{' }} cn_domain {{ '}}' }}"
  62. -days {{ '{{' }} cert_validity_days {{ '}}' }}
  63. -key {{ '{{' }} certs_path {{ '}}' }}/ca-key.pem
  64. -out {{ '{{' }} certs_path {{ '}}' }}/ca.pem
  65. args:
  66. creates: {{ '{{' }} certs_path {{ '}}' }}/ca.pem
  67. - name: Generate server private key
  68. ansible.builtin.command:
  69. cmd: >
  70. openssl genrsa -out {{ '{{' }} certs_path {{ '}}' }}/server-key.pem 4096
  71. creates: {{ '{{' }} certs_path {{ '}}' }}/server-key.pem
  72. - name: Generate server certificate signing request
  73. ansible.builtin.command:
  74. cmd: >
  75. openssl req -sha256 -new
  76. -subj "/CN={{ '{{' }} inventory_hostname {{ '}}' }}"
  77. -key {{ '{{' }} certs_path {{ '}}' }}/server-key.pem
  78. -out {{ '{{' }} certs_path {{ '}}' }}/server.csr
  79. creates: {{ '{{' }} certs_path {{ '}}' }}/server.csr
  80. - name: Generate server certificate extension file
  81. ansible.builtin.shell: |
  82. echo "subjectAltName = DNS:{{ '{{' }} inventory_hostname {{ '}}' }},IP:{{ '{{' }} ip_address {{ '}}' }},IP:127.0.0.1" >> {{ '{{' }} certs_path {{ '}}' }}/extfile.cnf
  83. echo "extendedKeyUsage = serverAuth" >> {{ '{{' }} certs_path {{ '}}' }}/extfile.cnf
  84. args:
  85. creates: {{ '{{' }} certs_path {{ '}}' }}/extfile.cnf
  86. - name: Generate server certificate
  87. ansible.builtin.command:
  88. cmd: >
  89. openssl x509 -req -days {{ '{{' }} cert_validity_days {{ '}}' }} -sha256
  90. -in {{ '{{' }} certs_path {{ '}}' }}/server.csr
  91. -CA {{ '{{' }} certs_path {{ '}}' }}/ca.pem
  92. -CAkey {{ '{{' }} certs_path {{ '}}' }}/ca-key.pem
  93. -CAcreateserial -out {{ '{{' }} certs_path {{ '}}' }}/server-cert.pem
  94. -extfile {{ '{{' }} certs_path {{ '}}' }}/extfile.cnf
  95. creates: {{ '{{' }} certs_path {{ '}}' }}/server-cert.pem
  96. - name: Generate client private key
  97. ansible.builtin.command:
  98. cmd: >
  99. openssl genrsa -out {{ '{{' }} certs_path {{ '}}' }}/key.pem 4096
  100. creates: {{ '{{' }} certs_path {{ '}}' }}/key.pem
  101. - name: Generate client certificate signing request
  102. ansible.builtin.command:
  103. cmd: >
  104. openssl req -sha256 -new
  105. -subj "/CN=client"
  106. -key {{ '{{' }} certs_path {{ '}}' }}/key.pem
  107. -out {{ '{{' }} certs_path {{ '}}' }}/client.csr
  108. creates: {{ '{{' }} certs_path {{ '}}' }}/client.csr
  109. - name: Generate client certificate extension file
  110. ansible.builtin.shell: |
  111. echo "extendedKeyUsage = clientAuth" >> {{ '{{' }} certs_path {{ '}}' }}/client-extfile.cnf
  112. args:
  113. creates: {{ '{{' }} certs_path {{ '}}' }}/client-extfile.cnf
  114. - name: Generate client certificate
  115. ansible.builtin.command:
  116. cmd: >
  117. openssl x509 -req -days {{ '{{' }} cert_validity_days {{ '}}' }}
  118. -sha256 -in {{ '{{' }} certs_path {{ '}}' }}/client.csr
  119. -CA {{ '{{' }} certs_path {{ '}}' }}/ca.pem
  120. -CAkey {{ '{{' }} certs_path {{ '}}' }}/ca-key.pem
  121. -CAcreateserial -out {{ '{{' }} certs_path {{ '}}' }}/cert.pem
  122. -extfile {{ '{{' }} certs_path {{ '}}' }}/client-extfile.cnf
  123. creates: {{ '{{' }} certs_path {{ '}}' }}/cert.pem
  124. - name: Remove client certificate signing request
  125. ansible.builtin.file:
  126. path: {{ '{{' }} certs_path {{ '}}' }}/server.csr
  127. state: absent
  128. - name: Remove client certificate signing request
  129. ansible.builtin.file:
  130. path: {{ '{{' }} certs_path {{ '}}' }}/client.csr
  131. state: absent
  132. - name: Remove server certificate extension file
  133. ansible.builtin.file:
  134. path: {{ '{{' }} certs_path {{ '}}' }}/extfile.cnf
  135. state: absent
  136. - name: Remove client certificate extension file
  137. ansible.builtin.file:
  138. path: {{ '{{' }} certs_path {{ '}}' }}/client-extfile.cnf
  139. state: absent
  140. - name: Set permissions for docker certs
  141. ansible.builtin.file:
  142. path: {{ '{{' }} certs_path {{ '}}' }}
  143. mode: '0700'
  144. recurse: true
  145. follow: true