Ejemplo n.º 1
0
def equal_values(v1, v2):
    """
    Checks whether types and content of two values are the same. In case of complex objects, the method might be
    called recursively.

    :param v1: first value
    :param v2: second value
    :return: True if types and content of passed values are equal. Otherwise, returns False.
    :rtype: bool
    """

    # string-like values might have same text but different types, so checking them separately
    if is_string(v1) and is_string(v2):
        return to_text(v1) == to_text(v2)

    if type(v1) != type(v2):
        return False
    value_type = type(v1)

    if value_type == list:
        return equal_lists(v1, v2)
    elif value_type == dict:
        return equal_dicts(v1, v2)
    else:
        return v1 == v2
Ejemplo n.º 2
0
    def get_shutdown_command(self, task_vars, distribution):
        shutdown_bin = self._get_value_from_facts('SHUTDOWN_COMMANDS',
                                                  distribution,
                                                  'DEFAULT_SHUTDOWN_COMMAND')
        default_search_paths = ['/sbin', '/usr/sbin', '/Users/Nick/brew/sbin']
        search_paths = self._task.args.get('search_paths',
                                           default_search_paths)

        # FIXME: switch all this to user arg spec validation methods when they are available
        # Convert bare strings to a list
        if is_string(search_paths):
            search_paths = [search_paths]

        # Error if we didn't get a list
        err_msg = "'search_paths' must be a string or flat list of strings, got {0}"
        try:
            incorrect_type = any(not is_string(x) for x in search_paths)
            if not isinstance(search_paths, list) or incorrect_type:
                raise TypeError
        except TypeError:
            raise AnsibleError(err_msg.format(search_paths))

        display.debug(
            '{action}: running find module looking in {paths} to get path for "{command}"'
            .format(action=self._task.action,
                    command=shutdown_bin,
                    paths=search_paths))
        find_result = self._execute_module(
            task_vars=task_vars,
            # prevent collection search by calling with ansible.legacy (still allows library/ override of find)
            module_name='ansible.legacy.find',
            module_args={
                'paths': search_paths,
                'patterns': [shutdown_bin],
                'file_type': 'any'
            })

        full_path = [x['path'] for x in find_result['files']]
        if not full_path:
            raise AnsibleError(
                'Unable to find command "{0}" in search paths: {1}'.format(
                    shutdown_bin, search_paths))
        self._shutdown_command = full_path[0]
        return self._shutdown_command
Ejemplo n.º 3
0
    def _filter(self, facts_dict, filter_spec):
        # assume filter_spec='' or filter_spec=[] is equivalent to filter_spec='*'
        if not filter_spec or filter_spec == '*':
            return facts_dict

        if is_string(filter_spec):
            filter_spec = [filter_spec]

        return [(x, y) for x, y in facts_dict.items() for f in filter_spec
                if not f or fnmatch.fnmatch(x, f)]
Ejemplo n.º 4
0
    def _filter(self, facts_dict, filter_spec):
        # assume filter_spec='' or filter_spec=[] is equivalent to filter_spec='*'
        if not filter_spec or filter_spec == '*':
            return facts_dict

        if is_string(filter_spec):
            filter_spec = [filter_spec]

        found = []
        for f in filter_spec:
            for x, y in facts_dict.items():
                if not f or fnmatch.fnmatch(x, f):
                    found.append((x, y))
                elif not f.startswith(('ansible_', 'facter', 'ohai')):
                    # try to match with ansible_ prefix added when non empty
                    g = 'ansible_%s' % f
                    if fnmatch.fnmatch(x, g):
                        found.append((x, y))
        return found