#!/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 # Returns an object of a new device def create_device(params): device = { 'addresses': [ 'dynamic' ], 'allowedNetworks': [], 'autoAcceptFolders': False, 'certName': '', 'compression': 'metadata', 'deviceID': params['id'], 'ignoredFolders': [], 'introducedBy': '', 'introducer': False, 'maxRecvKbps': 0, 'maxSendKbps': 0, 'name': params['name'], 'paused': True if params['state'] == 'paused' else False, 'pendingFolders': [], 'skipIntroductionRemovals': False } return device def run_module(): # module arguments module_args = {} module_args.update(dict( id=dict(type='str', required=True), name=dict(type='str', required=False), state=dict(type='str', default='present', choices=['absent', 'present', 'paused']), )) # the AnsibleModule object will be our abstraction working with Ansible module = SyncthingModule( api_url='/rest/config/devices', argument_spec=module_args ) if module.params['state'] != 'absent' and not module.params['name']: module.fail_json(msg='You must provide a name when creating') want_pause = module.params['state'] == 'paused' device_exists = False device = module.get_call(target=module.params['id']) if 'deviceID' in device and device['deviceID'] == module.params['id']: device_exists = True if module.params['state'] == 'absent': if device_exists: if module.check_mode: module.result['changed'] = True else: module.delete_call(target=device['deviceID']) elif device_exists: # exists but maybe needs changing if device['paused'] != want_pause: device['paused'] = want_pause if module.check_mode: module.result['changed'] = True else: module.patch_call(data=device, target=device['deviceID']) else: # Doesn't exist but needs to be added if module.check_mode: module.result['changed'] = True else: device = create_device(module.params) module.post_call(data=device) module.exit_json() def main(): run_module() if __name__ == '__main__': main()