Exemplo n.º 1
0
 def run(self, terms, inject=None, **kwargs):
     if isinstance(terms, basestring):
         terms = [terms]
     ret = []
     for term in terms:
         ret.append(utils.template_from_file(self.basedir, term, inject))
     return ret
Exemplo n.º 2
0
 def run(self, terms, inject=None, **kwargs):
     if isinstance(terms, basestring):
         terms = [ terms ]
     ret = []
     for term in terms:
         ret.append(utils.template_from_file(self.basedir, term, inject))
     return ret
Exemplo n.º 3
0
    def _include_handlers(self, play, handler, dirname, new_handlers):
        ''' load handlers from external files '''

        path = utils.path_dwim(dirname, handler['include'])
        inject_vars = self._get_vars(play, dirname)
        included = utils.template_from_file(path, inject_vars)
        included = utils.parse_yaml(included)
        for x in included:
            new_handlers.append(x)
Exemplo n.º 4
0
    def _include_handlers(self, play, handler, dirname, new_handlers):
        ''' load handlers from external files '''

        path = utils.path_dwim(dirname, handler['include'])
        inject_vars = self._get_vars(play, dirname)
        included = utils.template_from_file(path, inject_vars)
        included = utils.parse_yaml(included)
        for x in included:
            new_handlers.append(x)
Exemplo n.º 5
0
    def run(self, args, inject=None, **kwargs):

        if not inject:
            inject = {}
            
        args = args.split(' ',1)
        fname = args.pop(0)

        if args:
            args = json.loads(args[0])
            inject = dict(inject)
            inject.update(args)
        return [utils.template_from_file(self.basedir, fname, inject)]
Exemplo n.º 6
0
    def run(self, conn, tmp, module_name, module_args, inject):
        ''' handler for file transfer operations '''

        # load up options
        options = utils.parse_kv(module_args)
        src = options.get('src', None)

        # template the source data locally & transfer
        try:
            xmldata = utils.template_from_file(self.runner.basedir, src, inject)
        except Exception, e:
            result = dict(failed=True, msg='%s (%s)' % (e, src))
            return ReturnData(conn=conn, comm_ok=False, result=result)
Exemplo n.º 7
0
    def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
        ''' handler for file transfer operations '''

        # load up options
        options = utils.parse_kv(module_args)
        src = options.get('src', None)

        # template the source data locally & transfer
        try:
            xmldata = utils.template_from_file(self.runner.basedir, src, inject)
        except Exception, e:
            result = dict(failed=True, msg='%s (%s)' % (e, src))
            return ReturnData(conn=conn, comm_ok=False, result=result)
Exemplo n.º 8
0
    def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
        ''' handler for template operations '''

        # note: since this module just calls the copy module, the --check mode support
        # can be implemented entirely over there

        if not self.runner.is_playbook:
            raise errors.AnsibleError("in current versions of ansible, templates are only usable in playbooks")

        # load up options
        options  = {}
        if complex_args:
            options.update(complex_args)
        options.update(utils.parse_kv(module_args))

        source   = options.get('src', None)
        dest     = options.get('dest', None)

        if (source is None and 'first_available_file' not in inject) or dest is None:
            result = dict(failed=True, msg="src and dest are required")
            return ReturnData(conn=conn, comm_ok=False, result=result)

        # if we have first_available_file in our vars
        # look up the files and use the first one we find as src

        if 'first_available_file' in inject:
            found = False
            for fn in self.runner.module_vars.get('first_available_file'):
                fnt = utils.template(self.runner.basedir, fn, inject)
                fnd = utils.path_dwim(self.runner.basedir, fnt)
                if os.path.exists(fnd):
                    source = fnt
                    found = True
                    break
            if not found:
                result = dict(failed=True, msg="could not find src in first_available_file list")
                return ReturnData(conn=conn, comm_ok=False, result=result)
        else:
            source = utils.template(self.runner.basedir, source, inject)

        if dest.endswith("/"):
            base = os.path.basename(source)
            dest = os.path.join(dest, base)

        # template the source data locally & get ready to transfer
        try:
            resultant = utils.template_from_file(self.runner.basedir, source, inject)
        except Exception, e:
            result = dict(failed=True, msg=str(e))
            return ReturnData(conn=conn, comm_ok=False, result=result)
Exemplo n.º 9
0
    def _include_tasks(self, play, task, dirname, new_tasks):
        ''' load tasks included from external files. '''

        # include: some.yml a=2 b=3 c=4
        include_tokens = task['include'].split()
        path = utils.path_dwim(dirname, include_tokens[0])
        inject_vars = self._get_vars(play, dirname)
        for i,x in enumerate(include_tokens):
            if x.find("=") != -1:
                (k,v) = x.split("=")
                inject_vars[k] = v
        included = utils.template_from_file(path, inject_vars)
        included = utils.parse_yaml(included)
        for x in included:
            new_tasks.append(x)
Exemplo n.º 10
0
    def run(self, conn, tmp, module_name, module_args, inject):
        ''' handler for template operations '''

        if not self.runner.is_playbook:
            raise errors.AnsibleError(
                "in current versions of ansible, templates are only usable in playbooks"
            )

        # load up options
        options = utils.parse_kv(module_args)
        source = options.get('src', None)
        dest = options.get('dest', None)

        if dest.endswith("/"):
            base = os.path.basename(source)
            dest = os.path.join(dest, base)

        if (source is None
                and 'first_available_file' not in inject) or dest is None:
            result = dict(failed=True, msg="src and dest are required")
            return ReturnData(conn=conn, comm_ok=False, result=result)

        # if we have first_available_file in our vars
        # look up the files and use the first one we find as src
        if 'first_available_file' in inject:
            found = False
            for fn in self.runner.module_vars.get('first_available_file'):
                fn = utils.template(self.runner.basedir, fn, inject)
                if os.path.exists(fn):
                    source = fn
                    found = True
                    break
            if not found:
                result = dict(
                    failed=True,
                    msg="could not find src in first_available_file list")
                return ReturnData(conn=conn, comm_ok=False, result=result)

        source = utils.template(self.runner.basedir, source, inject)

        # template the source data locally & transfer
        try:
            resultant = utils.template_from_file(self.runner.basedir, source,
                                                 inject)
        except Exception, e:
            result = dict(failed=True, msg=str(e))
            return ReturnData(conn=conn, comm_ok=False, result=result)
Exemplo n.º 11
0
    def run(self, conn, tmp, module_name, module_args, inject):
        ''' handler for template operations '''

        if not self.runner.is_playbook:
            raise errors.AnsibleError("in current versions of ansible, templates are only usable in playbooks")

        # load up options
        options  = utils.parse_kv(module_args)
        source   = options.get('src', None)
        dest     = options.get('dest', None)

        if (source is None and 'first_available_file' not in inject) or dest is None:
            result = dict(failed=True, msg="src and dest are required")
            return ReturnData(conn=conn, comm_ok=False, result=result)

        # if we have first_available_file in our vars
        # look up the files and use the first one we find as src
        if 'first_available_file' in inject:
            found = False
            for fn in self.runner.module_vars.get('first_available_file'):
                fnt = utils.template(self.runner.basedir, fn, inject)
                fnd = utils.path_dwim(self.runner.basedir, fnt)
                if os.path.exists(fnd):
                    source = fnt
                    found = True
                    break
            if not found:
                result = dict(failed=True, msg="could not find src in first_available_file list")
                return ReturnData(conn=conn, comm_ok=False, result=result)
        else:
            source = utils.template(self.runner.basedir, source, inject)

        if dest.endswith("/"):
            base = os.path.basename(source)
            dest = os.path.join(dest, base)

        # template the source data locally & transfer
        try:
            resultant = utils.template_from_file(self.runner.basedir, source, inject)
        except Exception, e:
            result = dict(failed=True, msg=str(e))
            return ReturnData(conn=conn, comm_ok=False, result=result)
Exemplo n.º 12
0
    def _include_tasks(self, play, task, dirname, new_tasks):
        ''' load tasks included from external files. '''

        # include: some.yml a=2 b=3 c=4
        include_tokens = task['include'].split()
        path = utils.path_dwim(dirname, include_tokens[0])
        play_vars = self._get_vars(play, dirname)
        include_vars = {}
        for i, x in enumerate(include_tokens):
            if x.find("=") != -1:
                (k, v) = x.split("=")
                include_vars[k] = v
        inject_vars = play_vars.copy()
        inject_vars.update(include_vars)
        included = utils.template_from_file(path, inject_vars)
        included = utils.parse_yaml(included)
        for x in included:
            if len(include_vars):
                x["vars"] = include_vars
            new_tasks.append(x)
Exemplo n.º 13
0
    def _execute_template(self, conn, tmp):
        ''' handler for template operations '''

        # load up options
        options = utils.parse_kv(self.module_args)
        source = options.get('src', None)
        dest = options.get('dest', None)
        metadata = options.get('metadata', None)
        if (source is None and 'first_available_file'
                not in self.module_vars) or dest is None:
            result = dict(failed=True, msg="src and dest are required")
            return ReturnData(host=conn.host, comm_ok=False, result=result)

        # apply templating to source argument so vars can be used in the path
        inject = self.setup_cache.get(conn.host, {})

        # if we have first_available_file in our vars
        # look up the files and use the first one we find as src
        if 'first_available_file' in self.module_vars:
            found = False
            for fn in self.module_vars.get('first_available_file'):
                fn = utils.template(fn, inject, self.setup_cache)
                if os.path.exists(fn):
                    source = fn
                    found = True
                    break
            if not found:
                result = dict(
                    failed=True,
                    msg="could not find src in first_available_file list")
                return ReturnData(host=conn.host, comm_ok=False, result=result)

        if self.module_vars is not None:
            inject.update(self.module_vars)

        source = utils.template(source, inject, self.setup_cache)

        #(host, ok, data, err) = (None, None, None, None)

        if not self.is_playbook:

            # not running from a playbook so we have to fetch the remote
            # setup file contents before proceeding...
            if metadata is None:
                if self.remote_user == 'root':
                    metadata = '/etc/ansible/setup'
                else:
                    # path is expanded on remote side
                    metadata = "~/.ansible/setup"

            # install the template module
            slurp_module = self._transfer_module(conn, tmp, 'slurp')

            # run the slurp module to get the metadata file
            args = "src=%s" % metadata
            result1 = self._execute_module(conn, tmp, slurp_module, args)
            if not 'content' in result1.result or result1.result.get(
                    'encoding', 'base64') != 'base64':
                result1.result['failed'] = True
                return result1
            content = base64.b64decode(result1.result['content'])
            inject = utils.json_loads(content)

        # install the template module
        copy_module = self._transfer_module(conn, tmp, 'copy')

        # template the source data locally
        try:
            resultant = utils.template_from_file(utils.path_dwim(
                self.basedir, source),
                                                 inject,
                                                 self.setup_cache,
                                                 no_engine=False)
        except Exception, e:
            result = dict(failed=True, msg=str(e))
            return ReturnData(host=conn.host, comm_ok=False, result=result)
Exemplo n.º 14
0
    def _execute_template(self, conn, host, tmp):
        """ handler for template operations """

        # load up options
        options = utils.parse_kv(self.module_args)
        source = options.get("src", None)
        dest = options.get("dest", None)
        metadata = options.get("metadata", None)
        if (source is None and "first_available_file" not in self.module_vars) or dest is None:
            return (host, True, dict(failed=True, msg="src and dest are required"), "")

        # apply templating to source argument so vars can be used in the path
        inject = self.setup_cache.get(conn.host, {})
        inject.update(self.module_vars)

        # if we have first_available_file in our vars
        # look up the files and use the first one we find as src
        if "first_available_file" in self.module_vars:
            found = False
            for fn in self.module_vars.get("first_available_file"):
                fn = utils.template(fn, inject, self.setup_cache)
                if os.path.exists(fn):
                    source = fn
                    found = True
                    break
            if not found:
                return (host, True, dict(failed=True, msg="could not find src in first_available_file list"), "")

        source = utils.template(source, inject, self.setup_cache)

        (host, ok, data, err) = (None, None, None, None)

        if not self.is_playbook:

            # not running from a playbook so we have to fetch the remote
            # setup file contents before proceeding...
            if metadata is None:
                if self.remote_user == "root":
                    metadata = "/etc/ansible/setup"
                else:
                    # path is expanded on remote side
                    metadata = "~/.ansible/setup"

            # install the template module
            slurp_module = self._transfer_module(conn, tmp, "slurp")

            # run the slurp module to get the metadata file
            args = "src=%s" % metadata
            (result1, err, executed) = self._execute_module(conn, tmp, slurp_module, args)
            result1 = utils.json_loads(result1)
            if not "content" in result1 or result1.get("encoding", "base64") != "base64":
                result1["failed"] = True
                return self._return_from_module(conn, host, result1, err, executed)
            content = base64.b64decode(result1["content"])
            inject = utils.json_loads(content)

        # install the template module
        copy_module = self._transfer_module(conn, tmp, "copy")

        # template the source data locally
        try:
            resultant = utils.template_from_file(
                utils.path_dwim(self.basedir, source), inject, self.setup_cache, no_engine=False
            )
        except Exception, e:
            return (host, False, dict(failed=True, msg=str(e)), "")
Exemplo n.º 15
0
    def _execute_template(self, conn, tmp):
        ''' handler for template operations '''

        # load up options
        options  = utils.parse_kv(self.module_args)
        source   = options.get('src', None)
        dest     = options.get('dest', None)
        metadata = options.get('metadata', None)
        if (source is None and 'first_available_file' not in self.module_vars) or dest is None:
            result = dict(failed=True, msg="src and dest are required")
            return ReturnData(host=conn.host, comm_ok=False, result=result)

        # apply templating to source argument so vars can be used in the path
        inject = self.setup_cache.get(conn.host,{})

        # if we have first_available_file in our vars
        # look up the files and use the first one we find as src
        if 'first_available_file' in self.module_vars:
            found = False
            for fn in self.module_vars.get('first_available_file'):
                fn = utils.template(fn, inject, self.setup_cache)
                if os.path.exists(fn):
                    source = fn
                    found = True
                    break
            if not found:
                result = dict(failed=True, msg="could not find src in first_available_file list")
                return ReturnData(host=conn.host, comm_ok=False, result=result)


        if self.module_vars is not None:
            inject.update(self.module_vars)

        source = utils.template(source, inject, self.setup_cache)

        #(host, ok, data, err) = (None, None, None, None)

        if not self.is_playbook:

            # not running from a playbook so we have to fetch the remote
            # setup file contents before proceeding...
            if metadata is None:
                if self.remote_user == 'root':
                    metadata = '/etc/ansible/setup'
                else:
                    # path is expanded on remote side
                    metadata = "~/.ansible/tmp/setup" 
            
            # install the template module
            slurp_module = self._transfer_module(conn, tmp, 'slurp')

            # run the slurp module to get the metadata file
            args = "src=%s" % metadata
            result1  = self._execute_module(conn, tmp, slurp_module, args)
            if not 'content' in result1.result or result1.result.get('encoding','base64') != 'base64':
                result1.result['failed'] = True
                return result1
            content = base64.b64decode(result1.result['content'])
            inject = utils.json_loads(content)


        # install the template module
        copy_module = self._transfer_module(conn, tmp, 'copy')

        # template the source data locally
        try:
            resultant = utils.template_from_file(self.basedir, source, inject, self.setup_cache)
        except Exception, e:
            result = dict(failed=True, msg=str(e))
            return ReturnData(host=conn.host, comm_ok=False, result=result)
Exemplo n.º 16
0
 def run(self, terms, inject=None, **kwargs):
     return utils.template_from_file(self.basedir, terms, inject)