Exemple #1
0
    def munge(self, ds):
        # create the new ds as an AnsibleMapping, so we can preserve any line/column
        # data from the parser, and copy that info from the old ds (if applicable)
        new_ds = AnsibleMapping()
        if isinstance(ds, AnsibleBaseYAMLObject):
            new_ds.copy_position_info(ds)

        # Role definitions can be strings or dicts, so we fix things up here.
        # Anything that is not a role name, tag, or conditional will also be
        # added to the params sub-dictionary for loading later
        if isinstance(ds, string_types):
            new_ds['role_name'] = ds
        else:
            # munge the role ds here to correctly fill in the various fields which
            # may be used to define the role, like: role, src, scm, etc.
            ds = self._munge_role(ds)

            # now we split any random role params off from the role spec and store
            # them in a dictionary of params for parsing later
            params = dict()
            attr_names = [
                attr_name
                for (attr_name,
                     attr_value) in self._get_base_attributes().iteritems()
            ]
            for (key, value) in iteritems(ds):
                if key not in attr_names and key != 'role':
                    # this key does not match a field attribute, so it must be a role param
                    params[key] = value
                else:
                    # this is a field attribute, so copy it over directly
                    new_ds[key] = value
            new_ds['params'] = params

        # Set the role name and path, based on the role definition
        (role_name, role_path) = self._get_role_path(new_ds.get('role_name'))
        new_ds['role_name'] = role_name
        new_ds['role_path'] = role_path

        # load the role's files, if they exist
        new_ds['task_blocks'] = self._load_role_yaml(role_path, 'tasks')
        new_ds['handler_blocks'] = self._load_role_yaml(role_path, 'handlers')
        new_ds['default_vars'] = self._load_role_yaml(role_path, 'defaults')
        new_ds['role_vars'] = self._load_role_yaml(role_path, 'vars')

        # we treat metadata slightly differently: we instead pull out the
        # valid metadata keys and munge them directly into new_ds
        metadata_ds = self._munge_metadata(role_name, role_path)
        new_ds.update(metadata_ds)

        # and return the newly munged ds
        return new_ds