#!/usr/bin/python3 from time import sleep from os import fdopen, walk, readlink from os.path import join, islink from dbus import SystemBus, Array from dbus.exceptions import DBusException from ansible.module_utils.basic import AnsibleModule def run_module(): # define available arguments/parameters a user can pass to the module module_args = dict( root=dict(type='str', required=True), paths=dict(type='list', required=True), ) # seed the result dict in the object # we primarily care about changed and state # changed is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict( changed=False, compare={}, symlink={}, old_files=[], ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=module_args, supports_check_mode=True ) root = module.params['root'] if root != '/': paths = {join(root, path['name'][1:]): path['name'] for path in module.params['paths']} search_paths = [join(directory, f) for directory, subdirectories, files in walk(root) for f in files] else: paths = {path['name']: path['name'] for path in module.params['paths']} search_paths = paths for path in search_paths: if path in paths: if not islink(path): result['compare'][paths[path]] = {'type': 'file', 'shasum': module.digest_from_file(path, 'sha256'), } else: result['compare'][paths[path]] = {'type': 'symlink', 'name': readlink(path), } else: result['old_files'].append(path) module.exit_json(**result) def main(): run_module() if __name__ == '__main__': main()