Ejemplo n.º 1
0
    def load(data, play, current_role_path=None, parent_role=None, variable_manager=None, loader=None):

        assert isinstance(data, string_types) or isinstance(data, dict) or isinstance(data, AnsibleBaseYAMLObject)
        if isinstance(data, string_types) and ',' in data:
            data = RoleRequirement.role_spec_parse(data)

        ri = RoleInclude(play=play, role_basedir=current_role_path, variable_manager=variable_manager, loader=loader)
        return ri.load_data(data, variable_manager=variable_manager, loader=loader)
Ejemplo n.º 2
0
    def load(data, play, current_role_path=None, parent_role=None, variable_manager=None, loader=None):

        if not (isinstance(data, string_types) or isinstance(data, dict) or isinstance(data, AnsibleBaseYAMLObject)):
            raise AnsibleParserError("Invalid role definition: %s" % to_native(data))

        if isinstance(data, string_types) and ',' in data:
            data = RoleRequirement.role_spec_parse(data)

        ri = RoleInclude(play=play, role_basedir=current_role_path, variable_manager=variable_manager, loader=loader)
        return ri.load_data(data, variable_manager=variable_manager, loader=loader)
Ejemplo n.º 3
0
    def load(data,
             play,
             current_role_path=None,
             parent_role=None,
             variable_manager=None,
             loader=None):

        if not (isinstance(data, string_types) or isinstance(data, dict)
                or isinstance(data, AnsibleBaseYAMLObject)):
            raise AnsibleParserError("Invalid role definition: %s" %
                                     to_native(data))

        if isinstance(data, string_types) and ',' in data:
            data = RoleRequirement.role_spec_parse(data)

        ri = RoleInclude(play=play,
                         role_basedir=current_role_path,
                         variable_manager=variable_manager,
                         loader=loader)
        return ri.load_data(data,
                            variable_manager=variable_manager,
                            loader=loader)
Ejemplo n.º 4
0
    def execute_install(self):
        """
        Executes the installation action. The args list contains the
        roles to be installed, unless -f was specified. The list of roles
        can be a name (which will be downloaded via the galaxy API and github),
        or it can be a local .tar.gz file.
        """

        role_file  = self.get_opt("role_file", None)

        if len(self.args) == 0 and role_file is None:
            # the user needs to specify one of either --role-file
            # or specify a single user/role name
            raise AnsibleOptionsError("- you must specify a user/role name or a roles file")
        elif len(self.args) == 1 and not role_file is None:
            # using a role file is mutually exclusive of specifying
            # the role name on the command line
            raise AnsibleOptionsError("- please specify a user/role name, or a roles file, but not both")

        no_deps    = self.get_opt("no_deps", False)
        force      = self.get_opt('force', False)

        roles_left = []
        if role_file:
            try:
                f = open(role_file, 'r')
                if role_file.endswith('.yaml') or role_file.endswith('.yml'):
                    for role in yaml.safe_load(f.read()):
                        self.display.debug('found role %s in yaml file' % str(role))
                        if 'name' not in role:
                            if 'src' in role:
                                role['name'] = RoleRequirement.repo_url_to_role_name(role['src'])
                            else:
                                raise AnsibleError("Must specify name or src for role")
                        roles_left.append(GalaxyRole(self.galaxy, **role))
                else:
                    self.display.deprecated("going forward only the yaml format will be supported")
                    # roles listed in a file, one per line
                    for rline in f.readlines():
                        self.display.debug('found role %s in text file' % str(rline))
                        roles_left.append(GalaxyRole(self.galaxy, **RoleRequirement.role_spec_parse(rline)))
                f.close()
            except (IOError, OSError) as e:
                self.display.error('Unable to open %s: %s' % (role_file, str(e)))
        else:
            # roles were specified directly, so we'll just go out grab them
            # (and their dependencies, unless the user doesn't want us to).
            for rname in self.args:
                roles_left.append(GalaxyRole(self.galaxy, rname.strip()))

        for role in roles_left:
            self.display.debug('Installing role %s ' % role.name)
            # query the galaxy API for the role data
            role_data = None
            role = roles_left.pop(0)

            if role.install_info is not None and not force:
                self.display.display('- %s is already installed, skipping.' % role.name)
                continue

            try:
                installed = role.install()
            except AnsibleError as e:
                self.display.warning("- %s was NOT installed successfully: %s " % (role.name, str(e)))
                self.exit_without_ignore()
                continue

            # install dependencies, if we want them
            if not no_deps and installed:
                role_dependencies = role.metadata.get('dependencies', [])
                for dep in role_dependencies:
                    self.display.debug('Installing dep %s' % dep)
                    dep_req = RoleRequirement()
                    __, dep_name, __ = dep_req.parse(dep)
                    dep_role = GalaxyRole(self.galaxy, name=dep_name)
                    if dep_role.install_info is None or force:
                        if dep_role not in roles_left:
                            self.display.display('- adding dependency: %s' % dep_name)
                            roles_left.append(GalaxyRole(self.galaxy, name=dep_name))
                        else:
                            self.display.display('- dependency %s already pending installation.' % dep_name)
                    else:
                        self.display.display('- dependency %s is already installed, skipping.' % dep_name)

            if not installed:
                self.display.warning("- %s was NOT installed successfully." % role.name)
                self.exit_without_ignore()

        return 0