Ejemplo n.º 1
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 {}, (), {}
Ejemplo n.º 2
0
import os.path
import stat
import tempfile
import traceback

from ansible import constants as C
from ansible.errors import AnsibleError, AnsibleFileNotFound
from ansible.module_utils.basic import FILE_COMMON_ARGUMENTS
from ansible.module_utils._text import to_bytes, to_native, to_text
from ansible.module_utils.parsing.convert_bool import boolean
from ansible.plugins.action import ActionBase
from ansible.utils.hashing import checksum

# Supplement the FILE_COMMON_ARGUMENTS with arguments that are specific to file
# FILE_COMMON_ARGUMENTS contains things that are not arguments of file so remove those as well
REAL_FILE_ARGS = frozenset(FILE_COMMON_ARGUMENTS.keys()).union(
    ('state', 'path', '_original_basename', 'recurse', 'force', '_diff_peek',
     'src')).difference(
         ('content', 'decrypt', 'backup', 'remote_src', 'regexp', 'delimiter',
          'directory_mode', 'unsafe_writes'))


def _create_remote_file_args(module_args):
    """remove keys that are not relevant to file"""
    return dict((k, v) for k, v in module_args.items() if k in REAL_FILE_ARGS)


def _create_remote_copy_args(module_args):
    """remove action plugin only keys"""
    return dict((k, v) for k, v in module_args.items()
                if k not in ('content', 'decrypt'))
Ejemplo n.º 3
0
import stat
import tempfile
import traceback

from ansible import constants as C
from ansible.errors import AnsibleError, AnsibleFileNotFound
from ansible.module_utils.basic import FILE_COMMON_ARGUMENTS
from ansible.module_utils._text import to_bytes, to_native, to_text
from ansible.module_utils.parsing.convert_bool import boolean
from ansible.plugins.action import ActionBase
from ansible.utils.hashing import checksum


# Supplement the FILE_COMMON_ARGUMENTS with arguments that are specific to file
# FILE_COMMON_ARGUMENTS contains things that are not arguments of file so remove those as well
REAL_FILE_ARGS = frozenset(FILE_COMMON_ARGUMENTS.keys()).union(
                          ('state', 'path', 'original_basename', 'recurse', 'force',
                           '_diff_peek', 'src')).difference(
                          ('content', 'decrypt', 'backup', 'remote_src', 'regexp', 'delimiter',
                           'directory_mode', 'unsafe_writes'))


def _create_remote_file_args(module_args):
    """remove keys that are not relevant to file"""
    return dict((k, v) for k, v in module_args.items() if k in REAL_FILE_ARGS)


def _create_remote_copy_args(module_args):
    """remove action plugin only keys"""
    return dict((k, v) for k, v in module_args.items() if k not in ('content', 'decrypt'))