From e61ad55945c3e04ab3c5c1eded19e3ec5bde350d Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Tue, 23 Apr 2024 21:33:09 +0800 Subject: [PATCH] Add 'devices' - will return a dict of devices in format id: name --- collection/plugins/modules/devices.py | 103 ++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 collection/plugins/modules/devices.py diff --git a/collection/plugins/modules/devices.py b/collection/plugins/modules/devices.py new file mode 100644 index 0000000..2686a7e --- /dev/null +++ b/collection/plugins/modules/devices.py @@ -0,0 +1,103 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright: (c) 2018, Rafael Bodill +# Copyright: (c) 2020, Borjan Tchakaloff +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +ANSIBLE_METADATA = { + 'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community' +} + +DOCUMENTATION = ''' +--- +module: syncthing_device + +short_description: Manage Syncthing devices + +version_added: "2.7" + +description: + - "This is my longer description explaining my sample module" + +options: + id: + description: + - This is the unique id of this new device + required: true + name: + description: + - The name for this new device + required: false + host: + description: + - Host to connect to, including port + default: http://127.0.0.1:8384 + unix_socket: + description: + - Use this unix socket instead of TCP + required: false + api_key: + description: + - API key to use for authentication with host. + If not provided, will try to auto-configure from filesystem. + required: false + config_file: + description: + - Path to the Syncthing configuration file for automatic + discovery (`api_key`). Note that the running user needs read + access to the file. + required: false + timeout: + description: + - The socket level timeout in seconds + default: 30 + state: + description: + - Use present/absent to ensure device is added, or not. + default: present + choices: ['absent', 'present', 'paused'] + +author: + - Rafael Bodill (@rafi) +''' + +EXAMPLES = ''' +# Add a device to share with +- name: Add syncthing device + syncthing_device: + id: 1234-1234-1234-1234 + name: my-server-name +''' + +RETURN = ''' +response: + description: The API response, in-case of an error. + type: dict +''' + +from ansible_collections.community.syncthing.plugins.module_utils.syncthing_api import SyncthingModule + +def run_module(): + module = SyncthingModule( + api_url='/rest/config/devices', + ) + + devices = {} + devices_dict = module.get_call() + own_id = module.result['response']['x-syncthing-id'] + for device in devices_dict: + if device['deviceID'] == own_id: + continue + devices[device['deviceID']] = device['name'] + module.result['devices'] = devices + + module.exit_json() + +def main(): + run_module() + +if __name__ == '__main__': + main()