docker-certs.yaml 5.3 KB

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