Simplify fact gathering by using the cli instead of a webapi request
This commit is contained in:
parent
23e2941b3a
commit
b6ee8f76c2
6 changed files with 84 additions and 60 deletions
13
README.md
13
README.md
|
@ -10,14 +10,23 @@ I wanted a role to install and configure syncthing for me and did not find an ex
|
||||||
- the validation of host_vars which virtually no role in the wild ever does
|
- the validation of host_vars which virtually no role in the wild ever does
|
||||||
- the ability to manage an additional inventory file for devices which ansible cannot manage (like my phone)
|
- the ability to manage an additional inventory file for devices which ansible cannot manage (like my phone)
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
|
||||||
|
This role relies on `doas` being installed and configured so that your ansible user can run the syncthing cli as the syncthing user.
|
||||||
|
|
||||||
|
Here is an example of a `doas.conf` that works for the ansible user:
|
||||||
|
```yaml
|
||||||
|
permit nopass ansible as syncthing
|
||||||
|
```
|
||||||
|
|
||||||
## Role variables
|
## Role variables
|
||||||
|
|
||||||
There is a single variable to specify in the `host_vars` of your hosts: `syncthing`. This is a dict that can contain the following keys:
|
There is a single variable to specify in the `host_vars` of your hosts: `syncthing`. This is a dict that can contain the following keys:
|
||||||
- address: optional string to specify how to connect to the server, must match the format `tcp://<hostname>` or `tcp://<ip>`. Default value is *dynamic* which means a passive host.
|
- address: optional string to specify how to connect to the server, must match the format `tcp://<hostname>` or `tcp://<ip>`. Default value is *dynamic* which means a passive host.
|
||||||
- shared: a mandatory dict describing the directories this host shares, which can contain the following keys:
|
- shared: a mandatory dict describing the directories this host shares, which can contain the following keys:
|
||||||
- name: a mandatory string to name the share in the configuration. It must match on all devices that share this folder.
|
- name: a mandatory string to name the share in the configuration. It must match on all devices that share this folder.
|
||||||
- path: the path of the folder on the device. This can difer on each device sharing this data.
|
- path: the path of the folder on the device. This can differ on each device sharing this data.
|
||||||
- peers: a list a strings. Each item should be either the ansible_hostname of another device, or a hostname from the `syncthing_data.yaml` file
|
- peers: a list a strings. Each item should be either the `ansible_hostname` of another device, or a hostname from the `syncthing_data.yaml` file
|
||||||
|
|
||||||
Configuring a host through its `host_vars` looks like this:
|
Configuring a host through its `host_vars` looks like this:
|
||||||
```yaml
|
```yaml
|
||||||
|
|
|
@ -28,14 +28,14 @@ class ActionModule(ActionBase):
|
||||||
syncthing = hostvars['syncthing']
|
syncthing = hostvars['syncthing']
|
||||||
peer = {
|
peer = {
|
||||||
'address': 'dynamic',
|
'address': 'dynamic',
|
||||||
'id': '0000000-0000000-0000000-0000000-0000000-0000000-0000000-0000000',
|
'id': '',
|
||||||
'shared': [],
|
'shared': [],
|
||||||
}
|
}
|
||||||
if 'address' in syncthing.keys():
|
if 'address' in syncthing.keys():
|
||||||
peer['address'] = syncthing['address']
|
peer['address'] = syncthing['address']
|
||||||
for shared in syncthing['shared']:
|
for shared in syncthing['shared']:
|
||||||
peer['shared'].append({ 'name': shared['name'], 'path': shared['path'], 'peers': shared['peers']})
|
peer['shared'].append({ 'name': shared['name'], 'path': shared['path'], 'peers': shared['peers']})
|
||||||
if 'syncthing' in hostvars['ansible_local']:
|
if 'ansible_local' in hostvars and 'syncthing' in hostvars['ansible_local']:
|
||||||
peer['id'] = hostvars['ansible_local']['syncthing']['id']
|
peer['id'] = hostvars['ansible_local']['syncthing']['id']
|
||||||
peers[hostname] = peer
|
peers[hostname] = peer
|
||||||
|
|
||||||
|
@ -44,24 +44,9 @@ class ActionModule(ActionBase):
|
||||||
if task_vars['ansible_host'] in peers.keys():
|
if task_vars['ansible_host'] in peers.keys():
|
||||||
myself = peers[task_vars['ansible_host']]
|
myself = peers[task_vars['ansible_host']]
|
||||||
config = {
|
config = {
|
||||||
'config_path': "",
|
|
||||||
'folders_to_create': [],
|
|
||||||
'packages': [],
|
|
||||||
'peers': {},
|
'peers': {},
|
||||||
'service': "syncthing",
|
|
||||||
'shared': myself['shared'],
|
'shared': myself['shared'],
|
||||||
'user_group': "syncthing",
|
|
||||||
}
|
}
|
||||||
if task_vars['ansible_distribution'] == 'FreeBSD':
|
|
||||||
config['config_path'] = "/usr/local/etc/syncthing/config.xml"
|
|
||||||
config['folders_to_create'] = ["/usr/local/etc/syncthing/", "/var/syncthing"]
|
|
||||||
config['packages'] = ["p5-libwww", "syncthing"]
|
|
||||||
elif task_vars['ansible_distribution'] == 'Gentoo':
|
|
||||||
config['config_path'] = "/var/lib/syncthing/.config/syncthing/config.xml"
|
|
||||||
config['folders_to_create'] = ["/var/lib/syncthing/.config/syncthing"]
|
|
||||||
config['packages'] = ["net-p2p/syncthing"]
|
|
||||||
else:
|
|
||||||
error_msgs.append(f"syncthing role does not support {task_vars['ansible_distribution']} hosts yet")
|
|
||||||
for shared in myself['shared']:
|
for shared in myself['shared']:
|
||||||
for peer in shared['peers']:
|
for peer in shared['peers']:
|
||||||
if not peer in config['peers'].keys():
|
if not peer in config['peers'].keys():
|
||||||
|
|
46
action_plugins/syncthing_pre.py
Normal file
46
action_plugins/syncthing_pre.py
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
from ansible.plugins.action import ActionBase
|
||||||
|
import re
|
||||||
|
import yaml
|
||||||
|
from yaml.loader import SafeLoader
|
||||||
|
|
||||||
|
class ActionModule(ActionBase):
|
||||||
|
def run(self, tmp=None, task_vars=None):
|
||||||
|
if task_vars is None:
|
||||||
|
task_vars = dict()
|
||||||
|
result = super(ActionModule, self).run(tmp, task_vars)
|
||||||
|
result['changed'] = False
|
||||||
|
result['failed'] = False
|
||||||
|
|
||||||
|
error_msgs = []
|
||||||
|
|
||||||
|
### Compiling host configuration ######################################
|
||||||
|
config = {}
|
||||||
|
if 'syncthing' in task_vars:
|
||||||
|
config = {
|
||||||
|
'config_path': "",
|
||||||
|
'folders_to_create': [],
|
||||||
|
'packages': [],
|
||||||
|
'service': "syncthing",
|
||||||
|
'user_group': "syncthing",
|
||||||
|
}
|
||||||
|
if task_vars['ansible_distribution'] == 'FreeBSD':
|
||||||
|
config['config_dir'] = "/usr/local/etc/syncthing/"
|
||||||
|
config['data_dir'] = "/var/syncthing"
|
||||||
|
config['packages'] = ["syncthing"]
|
||||||
|
elif task_vars['ansible_distribution'] == 'Gentoo':
|
||||||
|
config['config_dir'] = "/var/lib/syncthing/.config/syncthing/"
|
||||||
|
config['data_dir'] = "/var/lib/syncthing"
|
||||||
|
config['packages'] = ["net-p2p/syncthing"]
|
||||||
|
else:
|
||||||
|
error_msgs.append(f"syncthing role does not support {task_vars['ansible_distribution']} hosts yet")
|
||||||
|
|
||||||
|
### Results compilation ##############################################
|
||||||
|
if error_msgs != []:
|
||||||
|
result['msg'] = ' ; '.join(error_msgs)
|
||||||
|
result['failed'] = True
|
||||||
|
|
||||||
|
result['ansible_facts'] = {
|
||||||
|
'syncthing_pre': config,
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
|
@ -1,4 +1,4 @@
|
||||||
- name: restart syncthing
|
- name: restart syncthing
|
||||||
service:
|
service:
|
||||||
name: syncthing
|
name: "{{ syncthing_pre.service }}"
|
||||||
state: restarted
|
state: restarted
|
||||||
|
|
|
@ -1,5 +1,21 @@
|
||||||
|
- action: syncthing_pre
|
||||||
|
|
||||||
|
- name: enforces service directories
|
||||||
|
file:
|
||||||
|
state: directory
|
||||||
|
path: "{{ item }}"
|
||||||
|
owner: "{{ syncthing_pre.user_group }}"
|
||||||
|
mode: 0700
|
||||||
|
loop:
|
||||||
|
- "{{ syncthing_pre.config_dir }}"
|
||||||
|
- "{{ syncthing_pre.data_dir }}"
|
||||||
|
|
||||||
|
- name: Install syncthing dependencies
|
||||||
|
package:
|
||||||
|
name: "{{ syncthing_pre.packages }}"
|
||||||
|
|
||||||
- name: Push syncthing gathering fact on clients
|
- name: Push syncthing gathering fact on clients
|
||||||
copy:
|
template:
|
||||||
src: syncthing.fact
|
src: syncthing.fact
|
||||||
dest: /etc/ansible/facts.d/
|
dest: /etc/ansible/facts.d/
|
||||||
mode: 0500
|
mode: 0500
|
||||||
|
@ -10,49 +26,20 @@
|
||||||
setup: filter=ansible_local
|
setup: filter=ansible_local
|
||||||
when: syncthing_gathering_fact.changed
|
when: syncthing_gathering_fact.changed
|
||||||
|
|
||||||
#- debug:
|
|
||||||
# msg: "{{ syncthing_config }}"
|
|
||||||
# changed_when: true
|
|
||||||
|
|
||||||
- action: syncthing_validate
|
- action: syncthing_validate
|
||||||
|
|
||||||
- action: syncthing_init
|
- action: syncthing_init
|
||||||
|
|
||||||
- name: Install syncthing dependencies
|
|
||||||
package:
|
|
||||||
name: "{{ syncthing_config.packages }}"
|
|
||||||
|
|
||||||
- name: enforces service directories
|
|
||||||
file:
|
|
||||||
state: directory
|
|
||||||
path: "{{ item }}"
|
|
||||||
owner: syncthing
|
|
||||||
mode: 0700
|
|
||||||
loop: "{{ syncthing_config.folders_to_create }}"
|
|
||||||
|
|
||||||
- name: enforces config file
|
- name: enforces config file
|
||||||
template:
|
template:
|
||||||
src: config.xml
|
src: config.xml
|
||||||
dest: "{{ syncthing_config.config_path }}"
|
dest: "{{ syncthing_pre.config_dir }}"
|
||||||
owner: "{{ syncthing_config.user_group }}"
|
owner: "{{ syncthing_pre.user_group }}"
|
||||||
mode: 0640
|
mode: 0400
|
||||||
notify: restart syncthing
|
notify: restart syncthing
|
||||||
|
|
||||||
- name: start and enables service
|
- name: start and enables service
|
||||||
service:
|
service:
|
||||||
name: syncthing
|
name: "{{ syncthing_pre.service }}"
|
||||||
enabled: yes
|
enabled: yes
|
||||||
state: started
|
state: started
|
||||||
|
|
||||||
- name: reload ansible_local
|
|
||||||
setup: filter=ansible_local
|
|
||||||
when: syncthing_gathering_fact.changed
|
|
||||||
|
|
||||||
- name: enforces config file
|
|
||||||
template:
|
|
||||||
src: config.xml
|
|
||||||
dest: "{{ syncthing_config.config_path }}"
|
|
||||||
owner: "{{ syncthing_config.user_group }}"
|
|
||||||
mode: 0640
|
|
||||||
when: syncthing_gathering_fact.changed
|
|
||||||
notify: restart syncthing
|
|
||||||
|
|
|
@ -8,14 +8,11 @@ use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
use JSON::PP;
|
use JSON::PP;
|
||||||
use LWP::UserAgent;
|
|
||||||
|
|
||||||
my $id = '0000000-0000000-0000000-0000000-0000000-0000000-0000000-0000000';
|
`doas -u {{ syncthing_pre.user_group }} syncthing generate --config {{ syncthing_pre.config_dir }} 2>&1` unless -e "{{ syncthing_pre.config_dir }}/config.xml";
|
||||||
|
|
||||||
my $resp = LWP::UserAgent->new()->head('http://localhost:8384/');
|
my $id = `doas -u {{ syncthing_pre.user_group }} syncthing -device-id --config {{ syncthing_pre.config_dir }} --data {{ syncthing_pre.data_dir }} 2>&1`;
|
||||||
if ($resp->code == 200) {
|
chomp $id;
|
||||||
$id = $resp->header('X-Syncthing-Id');
|
|
||||||
}
|
|
||||||
|
|
||||||
my %output = (
|
my %output = (
|
||||||
'id' => $id,
|
'id' => $id,
|
Reference in a new issue