예제 #1
0
    def run(self, terms, inject=None, **kwargs):
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        terms[0] = utils.listify_lookup_plugin_terms(terms[0], self.basedir,
                                                     inject)
        terms[2:] = self.__lookup_injects(terms[2:], inject)

        if not isinstance(terms, list) or len(terms) < 3:
            raise errors.AnsibleError(
                "subelements_nested lookup expects a list of at least three items, first a dict or a list, second a string, and one or more lists"
            )
        terms[0] = utils.listify_lookup_plugin_terms(terms[0], self.basedir,
                                                     inject)
        if not isinstance(terms[0], (list, dict)) or not isinstance(
                terms[1], basestring):
            raise errors.AnsibleError(
                "subelements_nested lookup expects a list of at least three items, first a dict or a list, second a string, and one or more lists"
            )

        if isinstance(terms[0], dict):  # convert to list:
            if terms[0].get('skipped', False) != False:
                # the registered result was completely skipped
                return []
            elementlist = []
            for key in terms[0].iterkeys():
                elementlist.append(terms[0][key])
        else:
            elementlist = terms[0]
        subelement = terms[1]

        nest_terms = terms[2:]

        ret = []
        for item0 in elementlist:
            if not isinstance(item0, dict):
                raise errors.AnsibleError(
                    "subelements lookup expects a dictionary, got '%s'" %
                    item0)
            if item0.get('skipped', False) != False:
                # this particular item is to be skipped
                continue
            if not subelement in item0:
                raise errors.AnsibleError(
                    "could not find '%s' key in iterated item '%s'" %
                    (subelement, item0))
            if not isinstance(item0[subelement], list):
                raise errors.AnsibleError(
                    "the key %s should point to a list, got '%s'" %
                    (subelement, item0[subelement]))
            sublist = item0.pop(subelement, [])
            for item1 in sublist:
                ret.extend(do_nest((item0, item1), nest_terms))

        return ret
예제 #2
0
파일: url.py 프로젝트: dataxu/ansible
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if isinstance(terms, basestring):
            terms = [ terms ]

        validate_certs = kwargs.get('validate_certs', True)

        ret = []
        for term in terms:
            try:
                response = open_url(term, validate_certs=validate_certs)
            except urllib2.URLError as e:
                utils.warning("Failed lookup url for %s : %s" % (term, str(e)))
                continue
            except urllib2.HTTPError as e:
                utils.warning("Received HTTP error for %s : %s" % (term, str(e)))
                continue
            except SSLValidationError as e:
                utils.warning("Error validating the server's certificate for %s: %s" % (term, str(e)))
                continue
            except ConnectionError as e:
                utils.warning("Error connecting to %s: %s" % (term, str(e)))
                continue

            for line in response.read().splitlines():
                ret.append(to_unicode(line))
        return ret
예제 #3
0
파일: file.py 프로젝트: terretta/ansible-1
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        ret = []

        # this can happen if the variable contains a string, strictly not desired for lookup
        # plugins, but users may try it, so make it work.
        if not isinstance(terms, list):
            terms = [ terms ]

        for term in terms:
            basedir_path  = utils.path_dwim(self.basedir, term)
            relative_path = None
            playbook_path = None

            # Special handling of the file lookup, used primarily when the
            # lookup is done from a role. If the file isn't found in the
            # basedir of the current file, use dwim_relative to look in the
            # role/files/ directory, and finally the playbook directory
            # itself (which will be relative to the current working dir)
            if '_original_file' in inject:
                relative_path = utils.path_dwim_relative(inject['_original_file'], 'files', term, self.basedir, check=False)
            if 'playbook_dir' in inject:
                playbook_path = os.path.join(inject['playbook_dir'], term)

            for path in (basedir_path, relative_path, playbook_path):
                if path and os.path.exists(path):
                    ret.append(codecs.open(path, encoding="utf8").read().rstrip())
                    break
            else:
                raise errors.AnsibleError("could not locate file in lookup: %s" % term)

        return ret
예제 #4
0
    def run(self, terms, inject=None, **kwargs):
#        with open('/tmp/terms_before.out', 'wa') as file:
#            file.write(str(terms) + "\n")
#            file.write(str(inject)+ "\n")
#            file.write(str(**kwargs)+ "\n")
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
#        with open('/tmp/terms_after.out', 'wa') as file:
#            file.write(str(terms) + "\n")
#            file.write(str(inject)+ "\n")
#            file.write(str(**kwargs)+ "\n")

        if not isinstance(terms, dict):
            raise errors.AnsibleError("with_cmd expects a dict")

        cmds = [] # commands to return
        regex = re.compile('^cmd_(.*)$')
        
        for t in terms:
            m = regex.match(t) 
            if m:
                # The matched string is the key, so with cmd_build,
                # build would be the key
                cmds.append({'key': m.group(1), 'value': terms[t]})

        #return flatten_hash_to_list(cmds)
        return cmds
예제 #5
0
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) 

        ret = []
        for term in terms:
            (url,key) = term.split(',')
            if url == "":
                url = 'redis://localhost:6379'

            # urlsplit on Python 2.6.1 is broken. Hmm. Probably also the reason
            # Redis' from_url() doesn't work here.

            p = '(?P<scheme>[^:]+)://?(?P<host>[^:/ ]+).?(?P<port>[0-9]*).*'

            try:
                m = re.search(p, url)
                host = m.group('host')
                port = int(m.group('port'))
            except AttributeError:
                raise errors.AnsibleError("Bad URI in redis lookup")

            try:
                conn = redis.Redis(host=host, port=port)
                res = conn.get(key)
                if res is None:
                    res = ""
                ret.append(res)
            except:
                ret.append("")  # connection failed or key not found
        return ret
예제 #6
0
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if isinstance(terms, basestring):
            terms = [terms]

        ret = []
        for term in terms:
            domain = term.split()[0]
            string = []
            try:
                answers = dns.resolver.query(domain, 'TXT')
                for rdata in answers:
                    s = rdata.to_text()
                    string.append(s[1:-1])  # Strip outside quotes on TXT rdata

            except dns.resolver.NXDOMAIN:
                string = 'NXDOMAIN'
            except dns.resolver.Timeout:
                string = ''
            except dns.exception.DNSException, e:
                raise errors.AnsibleError("dns.resolver unhandled exception",
                                          e)

            ret.append(''.join(string))
예제 #7
0
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) 

        ret = []

        for term in terms:
            # you can't have escaped spaces in yor pathname
            params = term.split()
            relpath = params[0]

            paramvals = {
                'length': LookupModule.LENGTH,
                'encrypt': None,
                'chars': ['ascii_letters','digits',".,:-_"],
            }

            # get non-default parameters if specified
            try:
                for param in params[1:]:
                    name, value = param.split('=')
                    assert(name in paramvals)
                    if name == 'length':
                        paramvals[name] = int(value)
                    elif name == 'chars':
                        use_chars=[]
                        if ",," in value: 
                            use_chars.append(',')
                        use_chars.extend(value.replace(',,',',').split(','))
                        paramvals['chars'] = use_chars
                    else:
                        paramvals[name] = value
            except (ValueError, AssertionError), e:
                raise errors.AnsibleError(e)

            length  = paramvals['length']
            encrypt = paramvals['encrypt']
            use_chars = paramvals['chars']

            # get password or create it if file doesn't exist
            path = utils.path_dwim(self.basedir, relpath)
            if not os.path.exists(path):
                pathdir = os.path.dirname(path)
                if not os.path.isdir(pathdir):
                    try:
                        os.makedirs(pathdir, mode=0700)
                    except OSError, e:
                        raise errors.AnsibleError("cannot create the path for the password lookup: %s (error was %s)" % (pathdir, str(e)))

                chars = "".join([getattr(string,c,c) for c in use_chars]).replace('"','').replace("'",'')
                password = ''.join(random.choice(chars) for _ in range(length))

                if encrypt is not None:
                    salt = self.random_salt()
                    content = '%s salt=%s' % (password, salt)
                else:
                    content = password
                with open(path, 'w') as f:
                    os.chmod(path, 0600)
                    f.write(content + '\n')
예제 #8
0
                def run(self, terms, inject=None, **kwargs):

                    terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
                    ret = []

                    # this can happen if the variable contains a string, strictly not desired for lookup
                    # plugins, but users may try it, so make it work.
                    if not isinstance(terms, list):
                        terms = [ terms ]

                    places = []

                    for term in terms:
                        if '_original_file' in inject:
                            relative_path = utils.path_dwim_relative(inject['_original_file'], 'tasks', '', self.basedir, check=False)
                            places.append(relative_path)
                        for path in places:
                            template = os.path.join(path, term)
                            if template and os.path.exists(template):
                                ret.append(template)
                                break
                        else:
                            raise errors.AnsibleError("could not locate file in lookup: %s" % term)

                    return ret
예제 #9
0
    def run(self, terms, inject=None, **kwargs):
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if not isinstance(terms, dict):
            raise errors.AnsibleError("with_nested_dict expects a dict")

        return flatten_nested_hash_to_list(terms)
예제 #10
0
파일: url.py 프로젝트: bopopescu/ansible-1
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if isinstance(terms, basestring):
            terms = [terms]

        validate_certs = kwargs.get('validate_certs', True)

        ret = []
        for term in terms:
            try:
                response = open_url(term, validate_certs=validate_certs)
            except urllib2.URLError as e:
                utils.warning("Failed lookup url for %s : %s" % (term, str(e)))
                continue
            except urllib2.HTTPError as e:
                utils.warning("Received HTTP error for %s : %s" %
                              (term, str(e)))
                continue
            except SSLValidationError as e:
                utils.warning(
                    "Error validating the server's certificate for %s: %s" %
                    (term, str(e)))
                continue
            except ConnectionError as e:
                utils.warning("Error connecting to %s: %s" % (term, str(e)))
                continue

            for line in response.read().splitlines():
                ret.append(to_unicode(line))
        return ret
예제 #11
0
    def run(self, terms, inject=None, **kwargs):
        results = []

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if isinstance(terms, basestring):
            terms = [ terms ]

        for term in terms:
            try:
                self.reset()  # clear out things for this iteration

                try:
                    if not self.parse_simple_args(term):
                        self.parse_kv_args(utils.parse_kv(term))
                except Exception:
                    raise AnsibleError(
                        "unknown error parsing with_sequence arguments: %r"
                        % term
                    )

                self.sanity_check()
                if self.stride != 0:
                    results.extend(self.generate_sequence())
            except AnsibleError:
                raise
            except Exception, e:
                raise AnsibleError(
                    "unknown error generating sequence: %s" % str(e)
                )
예제 #12
0
  def run(self, terms, inject=None, **kwargs):

    if CHECK_FOR_DNS_RESOLVER == False:
      raise errors.AnsibleError("Cannot resolve IP Addresss: module dns.resolver is not installed")
          
    terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
    ret = []

    if isinstance(terms, basestring):
      terms = [ terms ]

    for term in terms:
      hostname = term.split()[0]
      try:
        answers = dns.resolver.query(hostname, 'A')
        for rdata in answers:
          ret.append(''.join(rdata.to_text()))

      except dns.resolver.NXDOMAIN:
        string = 'NXDOMAIN'
      except dns.resolver.Timeout:
        string = ''
      except dns.exception.DNSException as e:
        raise errors.AnsibleError("dns.resolver unhandled exception", e)

    return ret
예제 #13
0
    def flatten(self, terms, inject):

        ret = []
        for term in terms:
            term = check_list_of_one_list(term)

            if term == 'None' or term == 'null':
                # ignore undefined items
                break

            if isinstance(term, basestring):
                # convert a variable to a list
                term2 = utils.listify_lookup_plugin_terms(
                    term, self.basedir, inject)
                # but avoid converting a plain string to a list of one string
                if term2 != [term]:
                    term = term2

            if isinstance(term, list):
                # if it's a list, check recursively for items that are a list
                term = self.flatten(term, inject)
                ret.extend(term)
            else:
                ret.append(term)

        return ret
예제 #14
0
    def run(self, terms, inject=None, **kwargs):
        """
        Implements LookupModule run method.
        """
        # flatten the terms if it's passed from with_yaml_file syntax
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        ret = []
        # Check if it's basestring as ansible 1.9 supports badly on unicode
        if isinstance(terms, basestring):
            terms = [terms]

        for term in terms:
            params = term.split()
            yaml_file = params[0]
            paramvals = self._build_params(params[1:])

            # make relative paths to absoluate path
            path = utils.path_dwim(self.basedir, yaml_file)
            data = self.read_yaml(path, **paramvals)

            if data is not None:
                if isinstance(data, list):
                    ret.extend(data)
                else:
                    ret.append(data)

        return ret
예제 #15
0
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        ret = []

        # this can happen if the variable contains a string, strictly not desired for lookup
        # plugins, but users may try it, so make it work.
        if not isinstance(terms, list):
            terms = [ terms ]

        for term in terms:
            basedir_path  = utils.path_dwim(self.basedir, term)
            relative_path = None
            playbook_path = None

            # Special handling of the file lookup, used primarily when the
            # lookup is done from a role. If the file isn't found in the
            # basedir of the current file, use dwim_relative to look in the
            # role/files/ directory, and finally the playbook directory
            # itself (which will be relative to the current working dir)
            if '_original_file' in inject:
                relative_path = utils.path_dwim_relative(inject['_original_file'], 'files', term, self.basedir, check=False)
            if 'playbook_dir' in inject:
                playbook_path = os.path.join(inject['playbook_dir'], term)

            for path in (basedir_path, relative_path, playbook_path):
                if path and os.path.exists(path):
                    ret.append(path)
                    break
            else:
                raise errors.AnsibleError("could not locate file in lookup: %s" % term)

        return ret
예제 #16
0
    def run(self, terms, inject=None, **kwargs):
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if isinstance(terms, basestring):
            terms = [terms]

        ret = []

        for term in terms:
            found_iface = "eth0"

            net = ipaddr.IPv4Network(term)
            for iface in kwargs['vars'].get('ansible_interfaces', []):
                addrs = kwargs['vars'].get('ansible_%s' % iface,
                                           {}).get('ipv4', [])
                if isinstance(addrs, dict):
                    addrs = [addrs]

                for addr in addrs:
                    if 'address' in addr and ipaddr.IPv4Address(
                            addr['address']) in net:
                        found_iface = iface

            ret.append(found_iface)
        return ret
예제 #17
0
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        ret = []

        # this can happen if the variable contains a string, strictly not desired for lookup
        # plugins, but users may try it, so make it work.
        if not isinstance(terms, list):
            terms = [ terms ]

        project_root = find_debops_project(required=False)
        config = read_config(project_root)
        places = []

        if 'paths' in config and conf_template_paths in config['paths']:
            custom_places = config['paths'][conf_template_paths].split(':')
            for custom_path in custom_places:
                if os.path.isabs(custom_path):
                    places.append(custom_path)
                else:
                    places.append(os.path.join(project_root, custom_path))

        for term in terms:
            if '_original_file' in inject:
                relative_path = utils.path_dwim_relative(inject['_original_file'], 'tasks', '', self.basedir, check=False)
                places.append(relative_path)
            for path in places:
                template = os.path.join(path, term)
                if template and os.path.exists(template):
                    ret.append(template)
                    break
            else:
                raise errors.AnsibleError("could not locate file in lookup: %s" % term)

        return ret
예제 #18
0
파일: password.py 프로젝트: jtsoi/ansible
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        ret = []

        for term in terms:
            # you can't have escaped spaces in yor pathname
            params = term.split()
            relpath = params[0]
            length = LookupModule.LENGTH

            # get non-default length parameter if specified
            if len(params) > 1:
                try:
                    name, length = params[1].split('=')
                    assert (name.startswith("length"))
                    length = int(length)
                except (ValueError, AssertionError) as e:
                    raise errors.AnsibleError(e)

            # get password or create it if file doesn't exist
            path = utils.path_dwim(self.basedir, relpath)
            if not os.path.exists(path):
                pathdir = os.path.dirname(path)
                if not os.path.isdir(pathdir):
                    os.makedirs(pathdir)
                chars = ascii_uppercase + ascii_lowercase + digits + ".,:-_"
                password = ''.join(random.choice(chars) for _ in range(length))
                with open(path, 'w') as f:
                    f.write(password)
            ret.append(open(path).read().rstrip())

        return ret
예제 #19
0
    def run(self, terms, inject=None, **kwargs):
        results = []

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if isinstance(terms, basestring):
            terms = [terms]

        for term in terms:
            try:
                self.reset()  # clear out things for this iteration

                try:
                    if not self.parse_simple_args(term):
                        self.parse_kv_args(utils.parse_kv(term))
                except Exception:
                    raise AnsibleError(
                        "unknown error parsing with_sequence arguments: %r" %
                        term)

                self.sanity_check()
                if self.stride != 0:
                    results.extend(self.generate_sequence())
            except AnsibleError:
                raise
            except Exception, e:
                raise AnsibleError("unknown error generating sequence: %s" %
                                   str(e))
예제 #20
0
    def run(self, terms, inject=None, **kwargs):
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if not isinstance(terms, list):
            raise errors.AnsibleError(
                "with_inventory_hostnames expects a list")
        return flatten(inventory.Inventory(self.host_list).list_hosts(terms))
예제 #21
0
    def flatten(self, terms, inject):

        ret = []
        for term in terms:
            term = check_list_of_one_list(term)

            if term == 'None' or term == 'null':
                # ignore undefined items
                break

            if isinstance(term, basestring):
                # convert a variable to a list
                term2 = utils.listify_lookup_plugin_terms(term, self.basedir, inject)
                # but avoid converting a plain string to a list of one string
                if term2 != [ term ]:
                    term = term2

            if isinstance(term, list):
                # if it's a list, check recursively for items that are a list
                term = self.flatten(term, inject)
                ret.extend(term)
            else:   
                ret.append(term)

        return ret
예제 #22
0
                def run(self, terms, inject=None, **kwargs):

                    terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
                    ret = []

                    # this can happen if the variable contains a string, strictly not desired for lookup
                    # plugins, but users may try it, so make it work.
                    if not isinstance(terms, list):
                        terms = [ terms ]

                    project_root = find_debops_project(required=False)
                    config = read_config(project_root)
                    places = []

                    if 'paths' in config and conf_template_paths in config['paths']:
                        custom_places = config['paths'][conf_template_paths].split(':')
                        for custom_path in custom_places:
                            if os.path.isabs(custom_path):
                                places.append(custom_path)
                            else:
                                places.append(os.path.join(project_root, custom_path))

                    for term in terms:
                        if '_original_file' in inject:
                            relative_path = utils.path_dwim_relative(inject['_original_file'], 'templates', '', self.basedir, check=False)
                            places.append(relative_path)
                        for path in places:
                            template = os.path.join(path, term)
                            if template and os.path.exists(template):
                                ret.append(template)
                                break
                        else:
                            raise errors.AnsibleError("could not locate file in lookup: %s" % term)

                    return ret
예제 #23
0
 def run(self, terms, inject=None, **kwargs):
     terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
     ret = []
     if not isinstance(terms, list):
         terms = [terms]
     for term in terms:
         basedir_path = utils.path_dwim(self.basedir, term)
         relative_path = None
         playbook_path = None
         if '_original_file' in inject:
             relative_path = utils.path_dwim_relative(inject['_original_file'], 'files', term, self.basedir, check=False)
         if 'playbook_dir' in inject:
             playbook_path = os.path.join(inject['playbook_dir'], term)
         for path in (basedir_path, relative_path, playbook_path):
             if path and os.path.exists(path):
                 vaultlib = inject['_ploy_instance'].get_vault_lib()
                 with open(path) as f:
                     data = f.read()
                 if vaultlib.is_encrypted(data):
                     data = vaultlib.decrypt(data)
                 try:
                     data = data.decode('utf8')
                 except UnicodeDecodeError as e:
                     raise errors.AnsibleError("UnicodeDecodeError encrypted file lookup, only ascii and utf8 supported: %s\n%s" % (term, e))
                 ret.append(data)
                 break
         else:
             raise errors.AnsibleError("could not locate encrypted file in lookup: %s" % term)
     return ret
예제 #24
0
 def __lookup_injects(self, terms, inject):
     results = []
     for x in terms:
         intermediate = utils.listify_lookup_plugin_terms(
             x, self.basedir, inject)
         results.append(intermediate)
     return results
예제 #25
0
파일: dict.py 프로젝트: basecrm/ansible
    def run(self, terms, inject=None, **kwargs):
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if not isinstance(terms, dict):
            raise errors.AnsibleError("with_dict expects a dict")

        return flatten_hash_to_list(terms)
예제 #26
0
    def run_v1(self, terms, inject=None, **kwargs):
        """
        Implements LookupModule run method for ansible v1.9.
        """
        # flatten the terms if it's passed from with_yaml_file syntax
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        ret = []
        # Check if it's basestring as ansible 1.9 supports badly on unicode
        if isinstance(terms, basestring):
            terms = [terms]

        for term in terms:
            params = term.split()
            yaml_file = params[0]
            paramvals = self._build_params(params[1:])

            # make relative paths to absoluate path
            path = utils.path_dwim(self.basedir, yaml_file)
            data = self.read_yaml(path, **paramvals)

            if data is not None:
                if isinstance(data, list):
                    ret.extend(data)
                else:
                    ret.append(data)

        return ret
예제 #27
0
파일: dnstxt.py 프로젝트: basecrm/ansible
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) 

        if isinstance(terms, basestring):
            terms = [ terms ]

        ret = []
        for term in terms:
            domain = term.split()[0]
            string = []
            try:
                answers = dns.resolver.query(domain, 'TXT')
                for rdata in answers:
                    s = rdata.to_text()
                    string.append(s[1:-1])  # Strip outside quotes on TXT rdata

            except dns.resolver.NXDOMAIN:
                string = 'NXDOMAIN'
            except dns.resolver.Timeout:
                string = ''
            except dns.exception.DNSException, e:
                raise errors.AnsibleError("dns.resolver unhandled exception", e)

            ret.append(''.join(string))
예제 #28
0
파일: items.py 프로젝트: terretta/ansible-1
    def run(self, terms, inject=None, **kwargs):
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if not isinstance(terms, list) and not isinstance(terms, set):
            raise errors.AnsibleError("with_items expects a list or a set")

        return flatten(terms)
예제 #29
0
    def run(self, terms, inject=None, **kwargs):
        """Run the main application.

        :type terms: ``str``
        :type inject: ``str``
        :type kwargs: ``dict``
        :returns: ``list``
        """
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        if isinstance(terms, basestring):
            terms = [terms]

        return_data = {
            'packages': list(),
            'remote_packages': list()
        }
        return_list = list()
        for term in terms:
            try:
                dfp = DependencyFileProcessor(
                    local_path=_abs_path(str(term))
                )
                return_list.extend(dfp.pip['py_package'])
                return_list.extend(dfp.pip['git_package'])
            except Exception as exp:
                raise errors.AnsibleError(
                    'lookup_plugin.py_pkgs(%s) returned "%s" error "%s"' % (
                        term,
                        str(exp),
                        traceback.format_exc()
                    )
                )

            for item in sorted(set(return_list)):
                if item.startswith(('http:', 'https:', 'git+')):
                    if '@' not in item:
                        return_data['packages'].append(item)
                    else:
                        return_data['remote_packages'].append(item)
                else:
                    return_data['packages'].append(item)
            else:
                return_data['packages'] = list(
                    set([i.lower() for i in return_data['packages']])
                )
                return_data['remote_packages'] = list(
                    set(return_data['remote_packages'])
                )
                keys = ['name', 'version', 'fragment', 'url', 'original']
                remote_package_parts = [
                    dict(
                        zip(
                            keys, git_pip_link_parse(i)
                        )
                    ) for i in return_data['remote_packages']
                ]
                return_data['remote_package_parts'] = remote_package_parts
                return_data['role_packages'] = dfp.pip['role_packages']

                return [return_data]
예제 #30
0
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if isinstance(terms, basestring):
            terms = [terms]

        ret = []
        for term in terms:
            """
            http://docs.python.org/2/library/subprocess.html#popen-constructor

            The shell argument (which defaults to False) specifies whether to use the 
            shell as the program to execute. If shell is True, it is recommended to pass 
            args as a string rather than as a sequence

            https://github.com/ansible/ansible/issues/6550
            """
            term = str(term)

            p = subprocess.Popen(term, cwd=self.basedir, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
            (stdout, stderr) = p.communicate()
            if p.returncode == 0:
                ret.append(stdout.decode("utf-8").rstrip())
            else:
                raise errors.AnsibleError("lookup_plugin.pipe(%s) returned %d" % (term, p.returncode))
        return ret
예제 #31
0
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        ret = []
        for term in terms:
            (url, key) = term.split(',')
            if url == "":
                url = 'redis://localhost:6379'

            # urlsplit on Python 2.6.1 is broken. Hmm. Probably also the reason
            # Redis' from_url() doesn't work here.

            p = '(?P<scheme>[^:]+)://?(?P<host>[^:/ ]+).?(?P<port>[0-9]*).*'

            try:
                m = re.search(p, url)
                host = m.group('host')
                port = int(m.group('port'))
            except AttributeError:
                raise errors.AnsibleError("Bad URI in redis lookup")

            try:
                conn = redis.Redis(host=host, port=port)
                res = conn.get(key)
                if res is None:
                    res = ""
                ret.append(res)
            except:
                ret.append("")  # connection failed or key not found
        return ret
예제 #32
0
                def run(self, terms, inject=None, **kwargs):

                    terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
                    ret = []

                    # this can happen if the variable contains a string, strictly not desired for lookup
                    # plugins, but users may try it, so make it work.
                    if not isinstance(terms, list):
                        terms = [ terms ]

                    # We need to search for the term in the relative path, and return
                    # the file path, instead of the contents due to how this lookup
                    # is used in a playbook

                    for term in terms:
                        path  = None

                        if '_original_file' in inject:
                            path = utils.path_dwim_relative(inject['_original_file'], 'templates', '', self.basedir, check=False)
                            path = os.path.join(path, term)

                        if path and os.path.exists(path):
                            ret.append(path)
                            break
                        else:
                            raise errors.AnsibleError("could not locate template in lookup: %s" % term)

                    return ret
예제 #33
0
파일: items.py 프로젝트: basecrm/ansible
    def run(self, terms, inject=None, **kwargs):
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) 

        if not isinstance(terms, list) and not isinstance(terms,set):
            raise errors.AnsibleError("with_items expects a list or a set")

        return flatten(terms)
예제 #34
0
    def run(self, terms, inject=None, **kwargs):

        if CHECK_FOR_DNS_RESOLVER == False:
            raise errors.AnsibleError(
                "Cannot resolve IP Addresss: module dns.resolver is not installed"
            )

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        ret = []

        if isinstance(terms, basestring):
            terms = [terms]

        for term in terms:
            hostname = term.split()[0]
            try:
                answers = dns.resolver.query(hostname, 'A')
                for rdata in answers:
                    ret.append(''.join(rdata.to_text()))

            except dns.resolver.NXDOMAIN:
                string = 'NXDOMAIN'
            except dns.resolver.Timeout:
                string = ''
            except dns.exception.DNSException as e:
                raise errors.AnsibleError("dns.resolver unhandled exception",
                                          e)

        return ret
예제 #35
0
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) 

        result = None
        anydict = False
        skip = False

        for term in terms:
            if isinstance(term, dict):
                anydict = True
        
        if anydict:
            for term in terms:
                if isinstance(term, dict):
                    files = term.get('files', [])
                    paths = term.get('paths', [])
                    skip  = utils.boolean(term.get('skip', False))
                
                    filelist = files
                    if isinstance(files, basestring):
                        files = files.replace(',', ' ')
                        files = files.replace(';', ' ')
                        filelist = files.split(' ')

                    pathlist = paths
                    if paths:
                        if isinstance(paths, basestring):
                            paths = paths.replace(',', ' ')
                            paths = paths.replace(':', ' ')
                            paths = paths.replace(';', ' ')
                            pathlist = paths.split(' ')
                        
                    total_search = []
                

                    if not pathlist:
                        total_search = filelist
                    else:
                        for path in pathlist:
                            for fn in filelist:
                                f = os.path.join(path, fn)
                                total_search.append(f)
                else:
                    total_search = [term]
        else:
            total_search = terms

        result = None
        for fn in total_search:
            path = utils.path_dwim(self.basedir, fn)
            if os.path.exists(path):
                return [path]


        if not result:
            if skip:
                return []
            else:
                return [None]
예제 #36
0
파일: pipe.py 프로젝트: terretta/ansible-1
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if isinstance(terms, basestring):
            terms = [terms]

        ret = []
        for term in terms:
            '''
            http://docs.python.org/2/library/subprocess.html#popen-constructor

            The shell argument (which defaults to False) specifies whether to use the 
            shell as the program to execute. If shell is True, it is recommended to pass 
            args as a string rather than as a sequence

            https://github.com/ansible/ansible/issues/6550
            '''
            term = str(term)

            p = subprocess.Popen(term,
                                 cwd=self.basedir,
                                 shell=True,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE)
            (stdout, stderr) = p.communicate()
            if p.returncode == 0:
                ret.append(stdout.decode("utf-8").rstrip())
            else:
                raise errors.AnsibleError(
                    "lookup_plugin.pipe(%s) returned %d" %
                    (term, p.returncode))
        return ret
예제 #37
0
    def run(self, terms, inject=None, **kwargs):
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if not isinstance(terms, list):
            raise errors.AnsibleError("with_indexed_items expects a list")

        items = flatten(terms)
        return zip(range(len(items)), items)
예제 #38
0
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        ret = []
        for term in terms:
            ret.append(template.template_from_file(self.basedir, term, inject))
        return ret
예제 #39
0
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) 

        ret = []
        for term in terms:
            ret.append(template.template_from_file(self.basedir, term, inject))
        return terms
예제 #40
0
    def run(self, terms, inject=None, **kwargs):
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if not isinstance(terms, list):
            raise errors.AnsibleError("with_indexed_items expects a list")

        items = flatten(terms)
        return zip(range(len(items)), items)
예제 #41
0
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        result = None
        anydict = False
        skip = False

        for term in terms:
            if isinstance(term, dict):
                anydict = True

        total_search = []
        if anydict:
            for term in terms:
                if isinstance(term, dict):
                    files = term.get('files', [])
                    paths = term.get('paths', [])
                    skip  = utils.boolean(term.get('skip', False))

                    filelist = files
                    if isinstance(files, basestring):
                        files = files.replace(',', ' ')
                        files = files.replace(';', ' ')
                        filelist = files.split(' ')

                    pathlist = paths
                    if paths:
                        if isinstance(paths, basestring):
                            paths = paths.replace(',', ' ')
                            paths = paths.replace(':', ' ')
                            paths = paths.replace(';', ' ')
                            pathlist = paths.split(' ')

                    if not pathlist:
                        total_search = filelist
                    else:
                        for path in pathlist:
                            for fn in filelist:
                                f = os.path.join(path, fn)
                                total_search.append(f)
                else:
                    total_search.append(term)
        else:
            total_search = terms

        result = None
        for fn in total_search:
            path = utils.path_dwim(self.basedir, fn)
            if os.path.exists(path):
                return [path]


        if not result:
            if skip:
                return []
            else:
                return [None]
예제 #42
0
    def run(self, terms, inject=None, variables=None, **kwargs):

        candidates = []
        basedir = self.get_basedir(variables)
        basepath = self._loader.path_dwim_relative(basedir, "", self.CREDENIAL_DIR)
        if basepath:
            candidates.append(basepath)

        if inject is not None and "playbook_dir" in inject:
            candidates.append(os.path.join(inject['playbook_dir'], self.CREDENIAL_DIR))

        keydir = None
        for candidate in candidates:
            if os.path.exists(candidate):
                keydir = candidate
                break
        if 'listify_lookup_plugin_terms' in globals():
            terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject) 

        if isinstance(terms, six.string_types):
            terms = [ terms ] 

        ret = []
        for term in terms:
            '''
            http://docs.python.org/2/library/subprocess.html#popen-constructor

            The shell argument (which defaults to False) specifies whether to use the 
            shell as the program to execute. If shell is True, it is recommended to pass 
            args as a string rather than as a sequence

            https://github.com/ansible/ansible/issues/6550
            '''
            term = str(term)

            if keydir is None:
                raise AnsibleError("lookup_plugin.pass(%s) No 'credentials' dir found in playbook dir. candidates: %s" % (term, candidates))

            env = dict(os.environ)
            env["PASSWORD_STORE_DIR"] = keydir

            command = "%s %s" % (self.COMMAND, term)

            p = subprocess.Popen(command,
                    cwd=basedir,
                    shell=True,
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    env=env)

            (stdout, stderr) = p.communicate()
            if p.returncode == 0:
                ret.append(stdout.decode("utf-8").splitlines()[0].rstrip())
            else:
                err = stderr.decode("utf-8").rstrip()
                raise AnsibleError("lookup_plugin.pass(%s) returned %d: %s" % (term, p.returncode, err))
        return ret
예제 #43
0
        def run(self, terms, inject=None, **kwargs):

            terms = utils.listify_lookup_plugin_terms(
                    terms, self.basedir, inject)
            ret = []
            config = {}
            places = []

            # this can happen if the variable contains a string,
            # strictly not desired for lookup plugins, but users may
            # try it, so make it work.
            if not isinstance(terms, list):
                terms = [terms]

            try:
                project_config = debops.config.Configuration()
                project_dir = debops.projectdir.ProjectDir(
                        config=project_config)
                project_root = project_dir.path
                config = project_dir.config.get(['views', 'system'])
            except NameError:
                try:
                    project_root = find_debops_project(required=False)
                    config = read_config(project_root)
                except NameError:
                    pass
            except NotADirectoryError:
                # This is not a DebOps project directory, so continue as normal
                pass

            if conf_section in config and conf_key in config[conf_section]:
                custom_places = (
                        config[conf_section][conf_key].split(':'))
                for custom_path in custom_places:
                    if os.path.isabs(custom_path):
                        places.append(custom_path)
                    else:
                        places.append(os.path.join(
                            project_root, custom_path))

            for term in terms:
                if '_original_file' in inject:
                    relative_path = (
                            utils.path_dwim_relative(
                                inject['_original_file'], 'templates',
                                '', self.basedir, check=False))
                    places.append(relative_path)
                for path in places:
                    template = os.path.join(path, term)
                    if template and os.path.exists(template):
                        ret.append(template)
                        break
                else:
                    raise errors.AnsibleError(
                            "could not locate file in lookup: %s"
                            % term)

            return ret
예제 #44
0
파일: cartesian.py 프로젝트: dataxu/ansible
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        terms = self.__lookup_injects(terms, inject)

        my_list = terms[:]
        if len(my_list) == 0:
            raise errors.AnsibleError("with_cartesian requires at least one element in each list")
        return [flatten(x) for x in product(*my_list)]
    def run(self, terms, inject=None, **kwargs):
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        terms = self.__lookup_injects(terms, inject)[:]
        if len(terms) == 0:
            raise errors.AnsibleError("with_dependent requires at least one element in the nested list")

        result = []
        self.process(result, terms, 0, [None] * len(terms), inject)
        return result
예제 #46
0
    def run(self, terms, inject=None, **kwargs):

        slots = utils.listify_lookup_plugin_terms(terms.get('slots'),
                                                  self.basedir, inject)

        items = utils.listify_lookup_plugin_terms(terms.get('items'),
                                                  self.basedir, inject)

        if not items:
            return []

        if not slots:
            raise errors.AnsibleError(
                'Missing "slots" parameter: '
                'with_{} requires "slots" to be a list (over which the list '
                'of "items" are allocated)'.format(self.plugin_name))

        idict = lambda item: {'current': item[1], 'slot': item[0]}
        return [idict(x) for x in izip(*[cycle(slots), items])]
예제 #47
0
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        terms = self.__lookup_injects(terms, inject)

        my_list = terms[:]
        if len(my_list) == 0:
            raise errors.AnsibleError(
                "with_cartesian requires at least one element in each list")
        return [flatten(x) for x in product(*my_list)]
예제 #48
0
    def run(self, terms, inject=None, **kwargs):

        # see if the string represents a list and convert to list if so
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if not isinstance(terms, list):
            raise errors.AnsibleError("with_flattened expects a list")

        ret = self.flatten(terms, inject)
        return ret
예제 #49
0
    def run(self, terms, inject=None, **kwargs):

        # see if the string represents a list and convert to list if so
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if not isinstance(terms, list):
            raise errors.AnsibleError("with_flattened expects a list")

        ret = self.flatten(terms, inject)
        return ret
예제 #50
0
    def run(self, terms, inject=None, **kwargs):
        # turns inbound "thing" into a list (not specifically needed if we're passing in strings)
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if isinstance(terms, basestring):
            terms = [terms]

        resource = AWSResource(terms=terms)

        return resource.response()
    def run(self, terms, inject=None, **kwargs):
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        terms[0] = utils.listify_lookup_plugin_terms(terms[0], self.basedir, inject)
        terms[2:] = self.__lookup_injects(terms[2:], inject)

        if not isinstance(terms, list) or len(terms) < 3:
            raise errors.AnsibleError(
                "subelements_nested lookup expects a list of at least three items, first a dict or a list, second a string, and one or more lists")
        terms[0] = utils.listify_lookup_plugin_terms(terms[0], self.basedir, inject)
        if not isinstance(terms[0], (list, dict)) or not isinstance(terms[1], basestring):
            raise errors.AnsibleError(
                "subelements_nested lookup expects a list of at least three items, first a dict or a list, second a string, and one or more lists")

        if isinstance(terms[0], dict): # convert to list:
            if terms[0].get('skipped',False) != False:
                # the registered result was completely skipped
                return []
            elementlist = []
            for key in terms[0].iterkeys():
                elementlist.append(terms[0][key])
        else: 
            elementlist = terms[0]
        subelement = terms[1]

        nest_terms = terms[2:]

        ret = []
        for item0 in elementlist:
            if not isinstance(item0, dict):
                raise errors.AnsibleError("subelements lookup expects a dictionary, got '%s'" %item0)
            if item0.get('skipped',False) != False:
                # this particular item is to be skipped
                continue 
            if not subelement in item0:
                raise errors.AnsibleError("could not find '%s' key in iterated item '%s'" % (subelement, item0))
            if not isinstance(item0[subelement], list):
                raise errors.AnsibleError("the key %s should point to a list, got '%s'" % (subelement, item0[subelement]))
            sublist = item0.pop(subelement, [])
            for item1 in sublist:
                ret.extend(do_nest((item0, item1), nest_terms))

        return ret
예제 #52
0
    def run(self, terms, inject=None, **kwargs):
        values = []
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        for term in terms:
            params = self.parse_params(term)
            self.sg = AWSAutoScalingGroup(params['region'])
            result = self.sg.get_instances(params['id'], params['filter'])
            values.append(result)

        return values
예제 #53
0
 def run(self, terms, inject=None, **kwargs):
     terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
     if isinstance(terms, basestring):
         terms = [terms]
     ret = []
     for term in terms:
         try:
             ret.append(socket.gethostbyname(term))
         except socket.error, ex:
             raise errors.AnsibleError("exception resolving %r" % (term, ),
                                       ex)
예제 #54
0
 def run(self, terms, inject=None, **kwargs):
     terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
     if isinstance(terms, basestring):
         terms = [terms]
     ret = []
     for term in terms:
         try:
             ret.append(socket.gethostbyname(term))
         except socket.error, ex:
             raise errors.AnsibleError("exception resolving %r" % (term,),
                                       ex)
예제 #55
0
    def run(self, terms, inject=None, **kwargs):

        # this code is common with 'items.py' consider moving to utils if we need it again

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        terms = self.__lookup_injects(terms, inject)

        my_list = terms[:]
        if len(my_list) == 0:
            raise errors.AnsibleError("with_transpose requires at least one element in each list")
        return [flatten(x) for x in izip_longest(*my_list, fillvalue=None)]
예제 #56
0
    def run(self, terms, inject=None, **kwargs):

        # this code is common with 'items.py' consider moving to utils if we need it again

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        terms = self.__lookup_injects(terms, inject)

        my_list = terms[:]
        if len(my_list) == 0:
            raise errors.AnsibleError("with_together requires at least one element in each list")
        return [flatten(x) for x in izip_longest(*my_list, fillvalue=None)]
예제 #57
0
    def run(self, terms, inject=None, **kwargs):
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if not isinstance(terms, (list, set)):
            raise errors.AnsibleError("with_list expects a list or a set")

        for i, elem in enumerate(terms):
            if not isinstance(elem, (list, tuple)):
                raise errors.AnsibleError("with_list expects a list (or a set) of lists or tuples, but elem %i is not")

        return terms
예제 #58
0
    def run(self, terms, inject=None, **kwargs):

        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if isinstance(terms, basestring):
            terms = [terms]

        ret = []
        for term in terms:
            var = term.split()[0]
            ret.append(os.getenv(var, ''))
        return ret
    def run_v1(self, terms, inject=None, **kwargs):
        """Run the main application.

        :type terms: ``str``
        :type inject: ``str``
        :type kwargs: ``dict``
        :returns: ``list``
        """
        terms = utils.listify_lookup_plugin_terms(
            terms,
            self.ansible_v1_basedir,
            inject
        )
        if isinstance(terms, basestring):
            terms = [terms]

        return_data = PACKAGE_MAPPING

        for term in terms:
            return_list = list()
            try:
                dfp = DependencyFileProcessor(
                    local_path=_abs_path(str(term))
                )
                return_list.extend(dfp.pip['py_package'])
                return_list.extend(dfp.pip['git_package'])
            except Exception as exp:
                raise errors.AnsibleError(
                    'lookup_plugin.py_pkgs(%s) returned "%s" error "%s"' % (
                        term,
                        str(exp),
                        traceback.format_exc()
                    )
                )

            for item in return_list:
                map_base_and_remote_packages(item, return_data)
            else:
                parse_remote_package_parts(return_data)
        else:
            map_role_packages(return_data)
            map_base_package_details(return_data)
            # Sort everything within the returned data
            for key, value in return_data.items():
                if isinstance(value, (list, set)):
                    return_data[key] = sorted(value)
            return_data['role_requirement_files'] = ROLE_REQUIREMENTS
            return_data['role_requirements'] = ROLE_BREAKOUT_REQUIREMENTS
            _dp = return_data['role_distro_packages'] = ROLE_DISTRO_BREAKOUT_PACKAGES
            for k, v in PACKAGE_MAPPING['role_project_groups'].items():
                if k in _dp:
                    _dp[k]['project_group'] = v
            return [return_data]
    def run(self, terms, inject=None, **kwargs):
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
        ret = []

        h = httplib2.Http()

        for term in terms:
            url = 'https://github.com/' + term + '.keys'
            response, content = h.request(url, method='GET')
            ret.append(content)

        return ret