コード例 #1
0
ファイル: module_args.py プロジェクト: alexdoka/ansibletrain
def get_py_argument_spec(filename, collection):
    name = get_module_name_from_filename(filename, collection)

    with setup_env(filename) as fake:
        try:
            with CaptureStd():
                runpy.run_module(name, run_name='__main__', alter_sys=True)
        except AnsibleModuleCallError:
            pass
        except BaseException as e:
            # we want to catch all exceptions here, including sys.exit
            reraise(AnsibleModuleImportError,
                    AnsibleModuleImportError('%s' % e),
                    sys.exc_info()[2])

        if not fake.called:
            raise AnsibleModuleNotInitialized()

    try:
        try:
            # for ping kwargs == {'argument_spec':{'data':{'type':'str','default':'pong'}}, 'supports_check_mode':True}
            return fake.kwargs['argument_spec'], fake.args, fake.kwargs
        except KeyError:
            return fake.args[0], fake.args, fake.kwargs
    except (TypeError, IndexError):
        return {}, (), {}
コード例 #2
0
ファイル: core.py プロジェクト: DinStamou/ansible
def get_encrypted_password(password,
                           hashtype='sha512',
                           salt=None,
                           salt_size=None,
                           rounds=None,
                           ident=None):
    passlib_mapping = {
        'md5': 'md5_crypt',
        'blowfish': 'bcrypt',
        'sha256': 'sha256_crypt',
        'sha512': 'sha512_crypt',
    }

    hashtype = passlib_mapping.get(hashtype, hashtype)
    try:
        return passlib_or_crypt(password,
                                hashtype,
                                salt=salt,
                                salt_size=salt_size,
                                rounds=rounds,
                                ident=ident)
    except AnsibleError as e:
        reraise(AnsibleFilterError, AnsibleFilterError(to_native(e),
                                                       orig_exc=e),
                sys.exc_info()[2])
コード例 #3
0
def get_py_argument_spec(filename, collection):
    name = get_module_name_from_filename(filename, collection)

    with setup_env(filename) as fake:
        try:
            with CaptureStd():
                runpy.run_module(name, run_name='__main__', alter_sys=True)
        except AnsibleModuleCallError:
            pass
        except BaseException as e:
            # we want to catch all exceptions here, including sys.exit
            reraise(AnsibleModuleImportError,
                    AnsibleModuleImportError('%s' % e),
                    sys.exc_info()[2])

        if not fake.called:
            raise AnsibleModuleNotInitialized()

    try:
        try:
            # for ping kwargs == {'argument_spec':{'data':{'type':'str','default':'pong'}}, 'supports_check_mode':True}
            argument_spec = fake.kwargs['argument_spec']
            # If add_file_common_args is truish, add options from FILE_COMMON_ARGUMENTS when not present.
            # This is the only modification to argument_spec done by AnsibleModule itself, and which is
            # not caught by setup_env's AnsibleModule replacement
            if fake.kwargs.get('add_file_common_args'):
                for k, v in FILE_COMMON_ARGUMENTS.items():
                    if k not in argument_spec:
                        argument_spec[k] = v
            return argument_spec, fake.args, fake.kwargs
        except KeyError:
            return fake.args[0], fake.args, fake.kwargs
    except (TypeError, IndexError):
        return {}, (), {}
コード例 #4
0
def get_py_argument_spec(filename):
    with setup_env(filename) as fake:
        try:
            # We use ``module`` here instead of ``__main__``
            # which helps with some import issues in this tool
            # where modules may import things that conflict
            with CaptureStd():
                mod = imp.load_source('module', filename)
                if not fake.called:
                    mod.main()
        except AnsibleModuleCallError:
            pass
        except Exception as e:
            reraise(AnsibleModuleImportError,
                    AnsibleModuleImportError('%s' % e),
                    sys.exc_info()[2])

    try:
        try:
            # for ping kwargs == {'argument_spec':{'data':{'type':'str','default':'pong'}}, 'supports_check_mode':True}
            return fake.kwargs['argument_spec'], fake.args, fake.kwargs
        except KeyError:
            return fake.args[0], fake.args, fake.kwargs
    except (TypeError, IndexError):
        return {}, (), {}
コード例 #5
0
def get_argument_spec(filename):
    with add_mocks(filename) as module_mock:
        try:
            mod = imp.load_source('module', filename)
            if not module_mock.call_args:
                mod.main()
        except AnsibleModuleCallError:
            pass
        except Exception as e:
            reraise(AnsibleModuleImportError, AnsibleModuleImportError('%s' % e), sys.exc_info()[2])

    try:
        args, kwargs = module_mock.call_args
        try:
            return kwargs['argument_spec'], args, kwargs
        except KeyError:
            return args[0], args, kwargs
    except TypeError:
        return {}, (), {}
コード例 #6
0
ファイル: module_args.py プロジェクト: awiddersheim/ansible
def get_argument_spec(filename):
    with add_mocks(filename) as module_mock:
        try:
            mod = imp.load_source('module', filename)
            if not module_mock.call_args:
                mod.main()
        except AnsibleModuleCallError:
            pass
        except Exception as e:
            reraise(AnsibleModuleImportError, AnsibleModuleImportError('%s' % e), sys.exc_info()[2])

    try:
        args, kwargs = module_mock.call_args
        try:
            return kwargs['argument_spec'], args, kwargs
        except KeyError:
            return args[0], args, kwargs
    except TypeError:
        return {}, (), {}
コード例 #7
0
ファイル: module_args.py プロジェクト: Jagan1100/ansible-1
def get_argument_spec(filename):
    with setup_env(filename) as fake:
        try:
            # We use ``module`` here instead of ``__main__``
            # which helps with some import issues in this tool
            # where modules may import things that conflict
            mod = imp.load_source('module', filename)
            if not fake.called:
                mod.main()
        except AnsibleModuleCallError:
            pass
        except Exception as e:
            reraise(AnsibleModuleImportError, AnsibleModuleImportError('%s' % e), sys.exc_info()[2])

    try:
        try:
            return fake.kwargs['argument_spec'], fake.args, fake.kwargs
        except KeyError:
            return fake.args[0], fake.args, fake.kwargs
    except TypeError:
        return {}, (), {}
コード例 #8
0
ファイル: module_args.py プロジェクト: Vemulashiva/Ansible
def get_py_argument_spec(filename):
    # Calculate the module's name so that relative imports work correctly
    name = None
    try:
        idx = filename.index('ansible/modules')
    except ValueError:
        try:
            idx = filename.index('ansible_collections/')
        except ValueError:
            # We default to ``module`` here instead of ``__main__``
            # which helps with some import issues in this tool
            # where modules may import things that conflict
            name = 'module'
    if name is None:
        name = filename[idx:-len('.py')].replace('/', '.')

    with setup_env(filename) as fake:
        try:
            with CaptureStd():
                mod = imp.load_source(name, filename)
                if not fake.called:
                    mod.main()
        except AnsibleModuleCallError:
            pass
        except Exception as e:
            reraise(AnsibleModuleImportError,
                    AnsibleModuleImportError('%s' % e),
                    sys.exc_info()[2])

    try:
        try:
            # for ping kwargs == {'argument_spec':{'data':{'type':'str','default':'pong'}}, 'supports_check_mode':True}
            return fake.kwargs['argument_spec'], fake.args, fake.kwargs
        except KeyError:
            return fake.args[0], fake.args, fake.kwargs
    except (TypeError, IndexError):
        return {}, (), {}