1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# https://apt.syncthing.net/

- name: Install Syncthing
  tags: syncthing
  hosts: "{{ ansible_limit | default([]) }}"
  become: true
  tasks:

    - name: Assert distribution is Debian
      ansible.builtin.assert:
        that:
          - ansible_facts["distribution"] == "Debian"
        fail_msg: "This playbook only supports Debian distributions"

    # ansible.builtin.deb822_repository needs python-debian on the target.
    - name: Install required packages
      ansible.builtin.apt:
        name: python3-debian
        state: present
        update_cache: true

    # sudo mkdir -p /etc/apt/keyrings
    - name: Create APT keyrings directory
      ansible.builtin.file:
        path: /etc/apt/keyrings
        state: directory
        mode: '0755'

    # sudo curl -L -o /etc/apt/keyrings/syncthing-archive-keyring.gpg https://syncthing.net/release-key.gpg
    - name: Download Syncthing GPG key
      ansible.builtin.get_url:
        url: https://syncthing.net/release-key.gpg
        dest: /etc/apt/keyrings/syncthing-archive-keyring.gpg
        mode: '0644'

    # Left behind by ansible.builtin.apt_repository; apt reads both formats, so
    # leaving it would configure the repository twice.
    - name: Remove one-line Syncthing repository
      ansible.builtin.file:
        path: /etc/apt/sources.list.d/apt_syncthing_net.list
        state: absent

    # echo "deb [signed-by=/etc/apt/keyrings/syncthing-archive-keyring.gpg] \
    #   https://apt.syncthing.net/ syncthing stable-v2" \
    #   | sudo tee /etc/apt/sources.list.d/syncthing.list
    - name: Add Syncthing repository
      ansible.builtin.deb822_repository:
        name: syncthing
        types: deb
        uris: https://apt.syncthing.net/
        suites: syncthing
        components: stable-v2
        signed_by: /etc/apt/keyrings/syncthing-archive-keyring.gpg
        state: present

    # sudo apt-get update
    # sudo apt-get install syncthing
    - name: Install Syncthing
      ansible.builtin.apt:
        name: syncthing
        state: present
        update_cache: true

    - name: Create Syncthing group
      ansible.builtin.group:
        name: syncthing
        system: true
        state: present

    - name: Create Syncthing user
      ansible.builtin.user:
        name: syncthing
        group: syncthing
        system: true

    - name: Create Syncthing data directory
      ansible.builtin.file:
        path: "{{ (data_dir, 'syncthing') | path_join }}"
        state: directory
        owner: syncthing
        group: syncthing
        mode: "0755"

    - name: Enable and start Syncthing service
      ansible.builtin.systemd:
        name: syncthing@syncthing.service
        enabled: true
        state: started
        daemon_reload: true

    - name: Update configuration in config.xml
      community.general.xml:
        path: /home/syncthing/.local/state/syncthing/config.xml
        xpath: "/configuration/{{ item.key }}"
        value: "{{ item.value }}"
        attribute: "{{ item.attribute | default(omit) }}"
        backup: true
      notify: Restart syncthing
      loop:
        - { key: "gui/address", value: "0.0.0.0:8384" }
        - { key: "gui/user", value: "{{ syncthing.user }}" }
        - { key: "gui/password", value: '{{ syncthing.password | password_hash("bcrypt") }}' }
        - { key: "options/minHomeDiskFree", value: "0" }
        - { key: "defaults/folder/minDiskFree", value: "75" }
        - { key: "defaults/folder/minDiskFree", attribute: "unit", value: "%" }
      no_log: true

  handlers:
    - name: Restart syncthing
      ansible.builtin.systemd:
        name: syncthing@syncthing.service
        state: restarted

# vim: ft=yaml.ansible