main.yml 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. - name: "create repository's root directory {{ borgbackup_sshd_repository_path }}"
  2. file:
  3. path: '{{ borgbackup_sshd_repository_path }}'
  4. state: directory
  5. mode: u=rwx,go=x
  6. register: _repo_dir
  7. - name: fetch host's architecture to select container image
  8. setup:
  9. gather_subset: min
  10. when: not borgbackup_sshd_container_image and ansible_architecture is not defined
  11. - docker_container:
  12. name: '{{ borgbackup_sshd_container_name }}'
  13. image: >-
  14. {{ borgbackup_sshd_container_image or _default_container_images[ansible_architecture] }}
  15. env:
  16. SSH_CLIENT_PUBLIC_KEYS: "{{ borgbackup_sshd_client_public_keys }}"
  17. SSH_CLIENT_PUBLIC_KEYS_APPEND_ONLY: "{{ borgbackup_sshd_client_public_keys_append_only }}"
  18. read_only: yes
  19. mounts:
  20. - type: volume
  21. source: '{{ borgbackup_sshd_container_name }}_host_keys'
  22. target: /etc/ssh/host_keys
  23. read_only: no
  24. - type: bind
  25. source: '{{ _repo_dir.path }}'
  26. target: /repository
  27. read_only: no
  28. - type: tmpfs
  29. target: /home/borg/.ssh # authorized_keys
  30. tmpfs_size: 16k
  31. tmpfs_mode: '1777'
  32. - type: tmpfs
  33. # > FileNotFoundError: [Errno 2] No usable temporary directory found [...]
  34. target: /tmp
  35. tmpfs_size: 1M
  36. tmpfs_mode: '1777'
  37. # > *Note* that from community.general 3.0.0 on, if `networks_cli_compatible' is `true'
  38. # > and `networks' contains at least one network, the default value for `network_mode'
  39. # will be the name of the first network in the `networks' list.
  40. network_mode: "{{ borgbackup_sshd_network_name | default('default') }}"
  41. networks_cli_compatible: yes
  42. published_ports: >-
  43. {{ ['0.0.0.0:%d:2200' % borgbackup_sshd_published_port]
  44. if (borgbackup_sshd_published_port is defined) else [] }}
  45. cap_drop: [ALL]
  46. security_opts: [no-new-privileges]
  47. cpu_quota: 8000
  48. cpu_period: 10000
  49. # 64MiB was insufficient for two parallel operations, e.g. `borg create` & `borg list`
  50. memory: 128M
  51. restart_policy: unless-stopped
  52. state: started
  53. register: _container
  54. - name: determine offset of user namespace remapping
  55. stat:
  56. path: '{{ _container.container.ResolvConfPath }}'
  57. register: _container_resolvconf
  58. - name: adapt ownership of repository's root directory
  59. file:
  60. path: '{{ _repo_dir.path }}'
  61. owner: '{{ _container_resolvconf.stat.uid + 100 }}'
  62. - name: wait for host keys
  63. wait_for:
  64. path: "{{ (_container.container.Mounts | items2dict(key_name='Destination', value_name='Source'))['/etc/ssh/host_keys'] }}/{{ item }}.pub"
  65. loop: [rsa, ed25519]
  66. register: _host_keys_files
  67. - name: read host keys
  68. slurp:
  69. src: '{{ item }}'
  70. loop: "{{ _host_keys_files.results | map(attribute='path') | list }}"
  71. register: _host_keys_base64
  72. - name: check if repository is initialized
  73. stat:
  74. path: '{{ _repo_dir.path }}/config'
  75. register: _repository_config_file
  76. - name: decode host keys & set return variables
  77. set_fact:
  78. borgbackup_sshd_host_keys: >-
  79. {{ _host_keys_base64.results | map(attribute='content') | map('b64decode') | map('trim') | list }}
  80. borgbackup_sshd_repository_initialized: '{{ _repository_config_file.stat.exists }}'