#!/usr/bin/python from __future__ import (absolute_import, division, print_function) __metaclass__ = type DOCUMENTATION = r''' --- module: nextcloud_facts short_description: Gather ansible_facts from Nextcloud. # If this is part of a collection, you need to use semantic versioning, # i.e. the version is of the form "2.5.0" and not "2.4". version_added: "0.0.1" description: Gather ansible_facts from Nextcloud via occ status and occ config:list. options: webroot: description: Path to the Nextcloud webroot. required: false default: /var/www/html type: str php: description: Path to the php-cli binary. required: false default: /usr/bin/php type: str # Specify this value according to your collection # in format of namespace.collection.doc_fragment_name #extends_documentation_fragment: # - s3lph.nextcloud.nextcloud_facts author: - s3lph ''' EXAMPLES = r''' ''' RETURN = r''' ansible_facts.nextcloud_status: type: dict description: Parsed output from occ status. returned: success sample: installed: true version: "25.0.3.2" versionstring: "25.0.3 edition: "" maintenance: false needsDbUpgrade: false productname: Nextcloud extendedSupport: false ansible_facts.nextcloud_config: type: dict description: Parsed output from occ status:list. Only present if installation was completed. returned: success sample: system: dbtype: sqlite3 version: "25.0.3.2" apps: activity: installed_version: "2.17.0" types: "filesystem" enabled: "yes" ''' from ansible.module_utils.basic import AnsibleModule import json import subprocess def run_module(): # define available arguments/parameters a user can pass to the module module_args = dict( webroot=dict(required=False, default='/var/www/html', type='str'), php=dict(required=False, default='/usr/bin/php', type='str'), ) # 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, ansible_facts={} ) # 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, ) # Gather Nextcloud installation status sc = subprocess.run([module.params['php'], 'occ', 'status', '--output=json'], cwd=module.params['webroot'], capture_output=True, encoding='utf-8') if sc.returncode != 0: result['stdout'] = sc.stdout result['stderr'] = sc.stderr module.fail_json(msg='occ status returned non-zero exit code. Run with -vvv to view the output', **result) status = json.loads(sc.stdout) result['ansible_facts']['nextcloud_status'] = status if not status['installed']: module.exit_json(**result) # Gather Nextcloud configuration cc = subprocess.run([module.params['php'], 'occ', 'config:list', '--output=json', '--private'], cwd=module.params['webroot'], capture_output=True, encoding='utf-8') if cc.returncode != 0: result['stdout'] = cc.stdout result['stderr'] = cc.stderr module.fail_json(msg='occ config:list returned non-zero exit code. Run with -vvv to view the output', **result) config = json.loads(cc.stdout) result['ansible_facts']['nextcloud_config'] = config module.exit_json(**result) def main(): run_module() if __name__ == '__main__': main()