playbook.yaml.j2 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. tasks:
  17. - name: Check if docker certs are existing
  18. ansible.builtin.stat:
  19. path: {{ '{{' }} certs_path {{ '}}' }}
  20. register: certs_dir
  21. - name: Fail if docker certs are not existing
  22. ansible.builtin.fail:
  23. msg: "Docker certificates are not existing in /root/docker-certs."
  24. when: not certs_dir.stat.exists
  25. - name: Get machine's primary internal ip address from eth0 interface
  26. ansible.builtin.setup:
  27. register: ip_address
  28. - name: Set machine's primary internal ip address
  29. ansible.builtin.set_fact:
  30. ip_address: {{ '{{' }} ip_address.ansible_facts.ansible_default_ipv4.address {{ '}}' }}
  31. - name: Check if ip_address is a valid ip address
  32. ansible.builtin.assert:
  33. that:
  34. - ip_address is match("^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$")
  35. fail_msg: "ip_address is not a valid ip address."
  36. success_msg: "ip_address is a valid ip address."
  37. - name: Change docker daemon to use certs
  38. ansible.builtin.lineinfile:
  39. path: /lib/systemd/system/docker.service
  40. line: >
  41. ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
  42. -H tcp://{{ '{{' }} ip_address {{ '}}' }}:2376 --tlsverify --tlscacert={{ '{{' }} certs_path {{ '}}' }}/ca.pem
  43. --tlscert={{ '{{' }} certs_path {{ '}}' }}/server-cert.pem --tlskey={{ '{{' }} certs_path {{ '}}' }}/server-key.pem
  44. regexp: '^ExecStart='
  45. state: present
  46. - name: Reload systemd daemon
  47. ansible.builtin.systemd:
  48. daemon_reload: true
  49. - name: Restart docker daemon
  50. ansible.builtin.systemd:
  51. name: docker
  52. state: restarted
  53. enabled: true