Python ansible.errors.AnsibleFilterError() Examples

The following are 30 code examples of ansible.errors.AnsibleFilterError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ansible.errors , or try the search function .
Example #1
Source File: openshift_master.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def validate_idp_list(idp_list, openshift_version, deployment_type):
        ''' validates a list of idps '''
        login_providers = [x.name for x in idp_list if x.login]

        multiple_logins_unsupported = False
        if len(login_providers) > 1:
            if deployment_type in ['enterprise', 'online', 'atomic-enterprise', 'openshift-enterprise']:
                if LooseVersion(openshift_version) < LooseVersion('3.2'):
                    multiple_logins_unsupported = True
            if deployment_type in ['origin']:
                if LooseVersion(openshift_version) < LooseVersion('1.2'):
                    multiple_logins_unsupported = True
        if multiple_logins_unsupported:
            raise errors.AnsibleFilterError("|failed multiple providers are "
                                            "not allowed for login. login "
                                            "providers: {0}".format(', '.join(login_providers)))

        names = [x.name for x in idp_list]
        if len(set(names)) != len(names):
            raise errors.AnsibleFilterError("|failed more than one provider configured with the same name")

        for idp in idp_list:
            idp.validate() 
Example #2
Source File: aws.py    From -Deploying-Jenkins-to-the-Cloud-with-DevOps-Tools with MIT License 6 votes vote down vote up
def get_instance_by_tags(region, tags, return_key="PublicIpAddress",
                         state=None, profile=None):

    instances = get_instances_by_tags(region, tags, return_key, state, profile)
    if len(instances) > 1:
        raise errors.AnsibleFilterError(
            "More than 1 {0} instance was found with the following tags {1} in region {2}"
            .format(instances, tags, region)
        )
    elif len(instances) == 1:
        return instances[0]
    else:
        raise errors.AnsibleFilterError(
            "No instances was found with the following tags {0} in region {1}"
            .format(tags, region)
        ) 
Example #3
Source File: aws.py    From -Deploying-Jenkins-to-the-Cloud-with-DevOps-Tools with MIT License 6 votes vote down vote up
def get_acm_arn(domain_name, region, profile=None):
    """Retrieve the attributes of a certificate if it exists or all certs.
    Args:
        domain_name (str): The domain name of the certificate.
        region (str): The AWS region.

    Basic Usage:
        >>> arn = get_acm_arn('test', 'us-west-2')
        "arn:aws:acm:us-west-2:123456789:certificate/25b4ad8a-1e24-4001-bcd0-e82fb3554cd7",
    """
    arn = None
    client = aws_client(region, 'acm', profile)
    try:
        acm_certs = client.list_certificates()['CertificateSummaryList']
        for cert in acm_certs:
            if domain_name == cert['DomainName']:
                arn = cert['CertificateArn']
                return arn
        if not arn:
            raise errors.AnsibleFilterError(
                'Certificate {0} does not exist'.format(domain_name)
            )
    except Exception as e:
        raise e 
Example #4
Source File: aws.py    From -Deploying-Jenkins-to-the-Cloud-with-DevOps-Tools with MIT License 6 votes vote down vote up
def get_redshift_endpoint(region, name, profile=None):
    """Retrieve the endpoint name of the redshift cluster.
    Args:
        region (str): The AWS region.
        name (str): The name of the redshift cluster.

    Basic Usage:
        >>> import boto3
        >>> redshift = boto3.client('redshift', 'us-west-2')
        >>> endpoint = get_redshift_endpoint(region, 'test')
        dns_name
    """
    client = aws_client(region, 'redshift', profile)
    try:
        return client.describe_clusters(ClusterIdentifier=name)['Clusters'][0]['Endpoint']['Address']
    except Exception as e:
        if isinstance(e, botocore.exceptions.ClientError):
            raise e
        else:
            raise errors.AnsibleFilterError(
                'Could not retreive ip for {0}: {1}'.format(name, str(e))
            ) 
Example #5
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def get_attr(data, attribute=None):
        """ This looks up dictionary attributes of the form a.b.c and returns
            the value.

            If the key isn't present, None is returned.
            Ex: data = {'a': {'b': {'c': 5}}}
                attribute = "a.b.c"
                returns 5
        """
        if not attribute:
            raise errors.AnsibleFilterError("|failed expects attribute to be set")

        ptr = data
        for attr in attribute.split('.'):
            if attr in ptr:
                ptr = ptr[attr]
            else:
                ptr = None
                break

        return ptr 
Example #6
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def oo_select_keys(data, keys):
        """ This returns a list, which contains the value portions for the keys
            Ex: data = { 'a':1, 'b':2, 'c':3 }
                keys = ['a', 'c']
                returns [1, 3]
        """

        if not isinstance(data, Mapping):
            raise errors.AnsibleFilterError("|failed expects to filter on a dict or object")

        if not isinstance(keys, list):
            raise errors.AnsibleFilterError("|failed expects first param is a list")

        # Gather up the values for the list of keys passed in
        retval = [data[key] for key in keys if key in data]

        return retval 
Example #7
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def oo_ami_selector(data, image_name):
        """ This takes a list of amis and an image name and attempts to return
            the latest ami.
        """
        if not isinstance(data, list):
            raise errors.AnsibleFilterError("|failed expects first param is a list")

        if not data:
            return None
        else:
            if image_name is None or not image_name.endswith('_*'):
                ami = sorted(data, key=itemgetter('name'), reverse=True)[0]
                return ami['ami_id']
            else:
                ami_info = [(ami, ami['name'].split('_')[-1]) for ami in data]
                ami = sorted(ami_info, key=itemgetter(1), reverse=True)[0][0]
                return ami['ami_id'] 
Example #8
Source File: aws.py    From -Deploying-Jenkins-to-the-Cloud-with-DevOps-Tools with MIT License 6 votes vote down vote up
def get_account_id(region, profile=None):
    """ Retrieve the AWS account id.
    Args:
        region (str): The AWS region.

    Basic Usage:
        >>> region = 'us-west-2'
        >>> account_id = get_account_id(region)
        12345667899

    Returns:
        String
    """
    client = aws_client(region, 'iam', profile)
    try:
        account_id = client.list_users()['Users'][0]['Arn'].split(':')[4]
        return account_id
    except Exception as e:
        if isinstance(e, botocore.exceptions.ClientError):
            raise e
        else:
            raise errors.AnsibleFilterError(
                "Failed to retrieve account id"
            ) 
Example #9
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def oo_openshift_env(hostvars):
        ''' Return facts which begin with "openshift_" and translate
            legacy facts to their openshift_env counterparts.

            Ex: hostvars = {'openshift_fact': 42,
                            'theyre_taking_the_hobbits_to': 'isengard'}
                returns  = {'openshift_fact': 42}
        '''
        if not issubclass(type(hostvars), dict):
            raise errors.AnsibleFilterError("|failed expects hostvars is a dict")

        facts = {}
        regex = re.compile('^openshift_.*')
        for key in hostvars:
            if regex.match(key):
                facts[key] = hostvars[key]

        migrations = {'openshift_router_selector': 'openshift_hosted_router_selector',
                      'openshift_registry_selector': 'openshift_hosted_registry_selector'}
        for old_fact, new_fact in migrations.iteritems():
            if old_fact in facts and new_fact not in facts:
                facts[new_fact] = facts[old_fact]
        return facts 
Example #10
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def oo_31_rpm_rename_conversion(rpms, openshift_version=None):
        """ Filters a list of 3.0 rpms and return the corresponding 3.1 rpms
            names with proper version (if provided)

            If 3.1 rpms are passed in they will only be augmented with the
            correct version.  This is important for hosts that are running both
            Masters and Nodes.
        """
        if not isinstance(rpms, list):
            raise errors.AnsibleFilterError("failed expects to filter on a list")
        if openshift_version is not None and not isinstance(openshift_version, basestring):
            raise errors.AnsibleFilterError("failed expects openshift_version to be a string")

        rpms_31 = []
        for rpm in rpms:
            if not 'atomic' in rpm:
                rpm = rpm.replace("openshift", "atomic-openshift")
            if openshift_version:
                rpm = rpm + openshift_version
            rpms_31.append(rpm)

        return rpms_31 
Example #11
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def oo_pods_match_component(pods, deployment_type, component):
        """ Filters a list of Pods and returns the ones matching the deployment_type and component
        """
        if not isinstance(pods, list):
            raise errors.AnsibleFilterError("failed expects to filter on a list")
        if not isinstance(deployment_type, basestring):
            raise errors.AnsibleFilterError("failed expects deployment_type to be a string")
        if not isinstance(component, basestring):
            raise errors.AnsibleFilterError("failed expects component to be a string")

        image_prefix = 'openshift/origin-'
        if deployment_type in ['enterprise', 'online', 'openshift-enterprise']:
            image_prefix = 'openshift3/ose-'
        elif deployment_type == 'atomic-enterprise':
            image_prefix = 'aep3_beta/aep-'

        matching_pods = []
        image_regex = image_prefix + component + r'.*'
        for pod in pods:
            for container in pod['spec']['containers']:
                if re.search(image_regex, container['image']):
                    matching_pods.append(pod)
                    break # stop here, don't add a pod more than once

        return matching_pods 
Example #12
Source File: openshift_master.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def validate(self):
        ''' validate this idp instance '''
        IdentityProviderBase.validate(self)
        if not isinstance(self.provider['attributes'], dict):
            raise errors.AnsibleFilterError("|failed attributes for provider "
                                            "{0} must be a dictionary".format(self.__class__.__name__))

        attrs = ['id', 'email', 'name', 'preferredUsername']
        for attr in attrs:
            if attr in self.provider['attributes'] and not isinstance(self.provider['attributes'][attr], list):
                raise errors.AnsibleFilterError("|failed {0} attribute for "
                                                "provider {1} must be a list".format(attr, self.__class__.__name__))

        unknown_attrs = set(self.provider['attributes'].keys()) - set(attrs)
        if len(unknown_attrs) > 0:
            raise errors.AnsibleFilterError("|failed provider {0} has unknown "
                                            "attributes: {1}".format(self.__class__.__name__, ', '.join(unknown_attrs))) 
Example #13
Source File: openshift_master.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def translate_idps(idps, api_version, openshift_version, deployment_type):
        ''' Translates a list of dictionaries into a valid identityProviders config '''
        idp_list = []

        if not isinstance(idps, list):
            raise errors.AnsibleFilterError("|failed expects to filter on a list of identity providers")
        for idp in idps:
            if not isinstance(idp, dict):
                raise errors.AnsibleFilterError("|failed identity providers must be a list of dictionaries")

            cur_module = sys.modules[__name__]
            idp_class = getattr(cur_module, idp['kind'], None)
            idp_inst = idp_class(api_version, idp) if idp_class is not None else IdentityProviderBase(api_version, idp)
            idp_inst.set_provider_items()
            idp_list.append(idp_inst)


        IdentityProviderBase.validate_idp_list(idp_list, openshift_version, deployment_type)
        return yaml.safe_dump([idp.to_dict() for idp in idp_list], default_flow_style=False) 
Example #14
Source File: openshift_master.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def validate_pcs_cluster(data, masters=None):
        ''' Validates output from "pcs status", ensuring that each master
            provided is online.
            Ex: data = ('...',
                        'PCSD Status:',
                        'master1.example.com: Online',
                        'master2.example.com: Online',
                        'master3.example.com: Online',
                        '...')
                masters = ['master1.example.com',
                           'master2.example.com',
                           'master3.example.com']
               returns True
        '''
        if not issubclass(type(data), basestring):
            raise errors.AnsibleFilterError("|failed expects data is a string or unicode")
        if not issubclass(type(masters), list):
            raise errors.AnsibleFilterError("|failed expects masters is a list")
        valid = True
        for master in masters:
            if "{0}: Online".format(master) not in data:
                valid = False
        return valid 
Example #15
Source File: openshift_master.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def oo_htpasswd_users_from_file(file_contents):
        ''' return a dictionary of htpasswd users from htpasswd file contents '''
        htpasswd_entries = {}
        if not isinstance(file_contents, basestring):
            raise errors.AnsibleFilterError("failed, expects to filter on a string")
        for line in file_contents.splitlines():
            user = None
            passwd = None
            if len(line) == 0:
                continue
            if ':' in line:
                user, passwd = line.split(':', 1)

            if user is None or len(user) == 0 or passwd is None or len(passwd) == 0:
                error_msg = "failed, expects each line to be a colon separated string representing the user and passwd"
                raise errors.AnsibleFilterError(error_msg)
            htpasswd_entries[user] = passwd
        return htpasswd_entries 
Example #16
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def oo_select_keys_from_list(data, keys):
        """ This returns a list, which contains the value portions for the keys
            Ex: data = { 'a':1, 'b':2, 'c':3 }
                keys = ['a', 'c']
                returns [1, 3]
        """

        if not isinstance(data, list):
            raise errors.AnsibleFilterError("|failed expects to filter on a list")

        if not isinstance(keys, list):
            raise errors.AnsibleFilterError("|failed expects first param is a list")

        # Gather up the values for the list of keys passed in
        retval = [FilterModule.oo_select_keys(item, keys) for item in data]

        return FilterModule.oo_flatten(retval) 
Example #17
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def oo_ami_selector(data, image_name):
        """ This takes a list of amis and an image name and attempts to return
            the latest ami.
        """
        if not isinstance(data, list):
            raise errors.AnsibleFilterError("|failed expects first param is a list")

        if not data:
            return None
        else:
            if image_name is None or not image_name.endswith('_*'):
                ami = sorted(data, key=itemgetter('name'), reverse=True)[0]
                return ami['ami_id']
            else:
                ami_info = [(ami, ami['name'].split('_')[-1]) for ami in data]
                ami = sorted(ami_info, key=itemgetter(1), reverse=True)[0][0]
                return ami['ami_id'] 
Example #18
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def oo_openshift_env(hostvars):
        ''' Return facts which begin with "openshift_" and translate
            legacy facts to their openshift_env counterparts.

            Ex: hostvars = {'openshift_fact': 42,
                            'theyre_taking_the_hobbits_to': 'isengard'}
                returns  = {'openshift_fact': 42}
        '''
        if not issubclass(type(hostvars), dict):
            raise errors.AnsibleFilterError("|failed expects hostvars is a dict")

        facts = {}
        regex = re.compile('^openshift_.*')
        for key in hostvars:
            if regex.match(key):
                facts[key] = hostvars[key]

        migrations = {'openshift_router_selector': 'openshift_hosted_router_selector',
                      'openshift_registry_selector': 'openshift_hosted_registry_selector'}
        for old_fact, new_fact in migrations.iteritems():
            if old_fact in facts and new_fact not in facts:
                facts[new_fact] = facts[old_fact]
        return facts 
Example #19
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def oo_pods_match_component(pods, deployment_type, component):
        """ Filters a list of Pods and returns the ones matching the deployment_type and component
        """
        if not isinstance(pods, list):
            raise errors.AnsibleFilterError("failed expects to filter on a list")
        if not isinstance(deployment_type, basestring):
            raise errors.AnsibleFilterError("failed expects deployment_type to be a string")
        if not isinstance(component, basestring):
            raise errors.AnsibleFilterError("failed expects component to be a string")

        image_prefix = 'openshift/origin-'
        if deployment_type in ['enterprise', 'online', 'openshift-enterprise']:
            image_prefix = 'openshift3/ose-'
        elif deployment_type == 'atomic-enterprise':
            image_prefix = 'aep3_beta/aep-'

        matching_pods = []
        image_regex = image_prefix + component + r'.*'
        for pod in pods:
            for container in pod['spec']['containers']:
                if re.search(image_regex, container['image']):
                    matching_pods.append(pod)
                    break # stop here, don't add a pod more than once

        return matching_pods 
Example #20
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def oo_image_tag_to_rpm_version(version, include_dash=False):
        """ Convert an image tag string to an RPM version if necessary
            Empty strings and strings that are already in rpm version format
            are ignored. Also remove non semantic version components.

            Ex. v3.2.0.10 -> -3.2.0.10
                v1.2.0-rc1 -> -1.2.0
        """
        if not isinstance(version, basestring):
            raise errors.AnsibleFilterError("|failed expects a string or unicode")
        if version.startswith("v"):
            version = version[1:]
            # Strip release from requested version, we no longer support this.
            version = version.split('-')[0]

        if include_dash and version and not version.startswith("-"):
            version = "-" + version

        return version 
Example #21
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def oo_image_tag_to_rpm_version(version, include_dash=False):
        """ Convert an image tag string to an RPM version if necessary
            Empty strings and strings that are already in rpm version format
            are ignored. Also remove non semantic version components.

            Ex. v3.2.0.10 -> -3.2.0.10
                v1.2.0-rc1 -> -1.2.0
        """
        if not isinstance(version, basestring):
            raise errors.AnsibleFilterError("|failed expects a string or unicode")
        if version.startswith("v"):
            version = version[1:]
            # Strip release from requested version, we no longer support this.
            version = version.split('-')[0]

        if include_dash and version and not version.startswith("-"):
            version = "-" + version

        return version 
Example #22
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 6 votes vote down vote up
def oo_filter_list(data, filter_attr=None):
        """ This returns a list, which contains all items where filter_attr
            evaluates to true
            Ex: data = [ { a: 1, b: True },
                         { a: 3, b: False },
                         { a: 5, b: True } ]
                filter_attr = 'b'
                returns [ { a: 1, b: True },
                          { a: 5, b: True } ]
        """
        if not isinstance(data, list):
            raise errors.AnsibleFilterError("|failed expects to filter on a list")

        if not isinstance(filter_attr, basestring):
            raise errors.AnsibleFilterError("|failed expects filter_attr is a str or unicode")

        # Gather up the values for the list of keys passed in
        return [x for x in data if filter_attr in x and x[filter_attr]] 
Example #23
Source File: aws.py    From -Deploying-Jenkins-to-the-Cloud-with-DevOps-Tools with MIT License 5 votes vote down vote up
def get_all_route_table_ids_except(vpc_id, region=None, profile=None):
    """
    Args:
        vpc_id (str): The vpc you want to exclude routes from.

    Basic Usage:
        >>> vpc_id = 'vpc-98c797fd'
        >>> get_all_route_table_ids_except(vpc_id)
        ['rtb-5f78343a']

    Returns:
        List of route table ids
    """
    route_ids = list()
    client = aws_client(region, 'ec2', profile)
    params = {
        'Filters': [
            {
                'Name': 'association.main',
                'Values': ['false']
            }
        ]
    }
    routes = client.describe_route_tables(**params)
    if routes:
        for route in routes['RouteTables']:
            if route['VpcId'] != vpc_id:
                route_ids.append(route['RouteTableId'])
        if len(route_ids) > 0:
            return route_ids
        else:
            raise errors.AnsibleFilterError("No routes were found")
    else:
        raise errors.AnsibleFilterError("No routes were found") 
Example #24
Source File: openshift_master.py    From origin-ci-tool with Apache License 2.0 5 votes vote down vote up
def set_provider_items(self):
        ''' set the provider items for this idp '''
        for items in self._required:
            self.set_provider_item(items, True)
        for items in self._optional:
            self.set_provider_item(items)
        if self._allow_additional:
            for key in self._idp.keys():
                self.set_provider_item([key])
        else:
            if len(self._idp) > 0:
                raise errors.AnsibleFilterError("|failed provider {0} "
                                                "contains unknown keys "
                                                "{1}".format(self.__class__.__name__, ', '.join(self._idp.keys()))) 
Example #25
Source File: openshift_master.py    From origin-ci-tool with Apache License 2.0 5 votes vote down vote up
def certificates_to_synchronize(hostvars, include_keys=True):
        ''' Return certificates to synchronize based on facts. '''
        if not issubclass(type(hostvars), dict):
            raise errors.AnsibleFilterError("|failed expects hostvars is a dict")
        certs = ['admin.crt',
                 'admin.key',
                 'admin.kubeconfig',
                 'master.kubelet-client.crt',
                 'master.kubelet-client.key',
                 'openshift-registry.crt',
                 'openshift-registry.key',
                 'openshift-registry.kubeconfig',
                 'openshift-router.crt',
                 'openshift-router.key',
                 'openshift-router.kubeconfig']
        if bool(include_keys):
            certs += ['serviceaccounts.private.key',
                      'serviceaccounts.public.key']
        if bool(hostvars['openshift']['common']['version_gte_3_1_or_1_1']):
            certs += ['master.proxy-client.crt',
                      'master.proxy-client.key']
        if not bool(hostvars['openshift']['common']['version_gte_3_2_or_1_2']):
            certs += ['openshift-master.crt',
                      'openshift-master.key',
                      'openshift-master.kubeconfig']
        if bool(hostvars['openshift']['common']['version_gte_3_3_or_1_3']):
            certs += ['service-signer.crt',
                      'service-signer.key']
        return certs 
Example #26
Source File: openshift_node.py    From origin-ci-tool with Apache License 2.0 5 votes vote down vote up
def get_dns_ip(openshift_dns_ip, hostvars):
        ''' Navigates the complicated logic of when to set dnsIP

            In all situations if they've set openshift_dns_ip use that
            For 1.0/3.0 installs we use the openshift_master_cluster_vip, openshift_node_first_master_ip, else None
            For 1.1/3.1 installs we use openshift_master_cluster_vip, else None (product will use kube svc ip)
            For 1.2/3.2+ installs we set to the node's default interface ip
        '''

        if not issubclass(type(hostvars), dict):
            raise errors.AnsibleFilterError("|failed expects hostvars is a dict")

        # We always use what they've specified if they've specified a value
        if openshift_dns_ip != None:
            return openshift_dns_ip

        if bool(hostvars['openshift']['common']['use_dnsmasq']):
            return hostvars['ansible_default_ipv4']['address']
        elif bool(hostvars['openshift']['common']['version_gte_3_1_or_1_1']):
            if 'openshift_master_cluster_vip' in hostvars:
                return hostvars['openshift_master_cluster_vip']
        else:
            if 'openshift_master_cluster_vip' in hostvars:
                return hostvars['openshift_master_cluster_vip']
            elif 'openshift_node_first_master_ip' in hostvars:
                return hostvars['openshift_node_first_master_ip']
        return None 
Example #27
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 5 votes vote down vote up
def oo_flatten(data):
        """ This filter plugin will flatten a list of lists
        """
        if not isinstance(data, list):
            raise errors.AnsibleFilterError("|failed expects to flatten a List")

        return [item for sublist in data for item in sublist] 
Example #28
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 5 votes vote down vote up
def oo_merge_dicts(first_dict, second_dict):
        """ Merge two dictionaries where second_dict values take precedence.
            Ex: first_dict={'a': 1, 'b': 2}
                second_dict={'b': 3, 'c': 4}
                returns {'a': 1, 'b': 3, 'c': 4}
        """
        if not isinstance(first_dict, dict) or not isinstance(second_dict, dict):
            raise errors.AnsibleFilterError("|failed expects to merge two dicts")
        merged = first_dict.copy()
        merged.update(second_dict)
        return merged 
Example #29
Source File: openshift_master.py    From origin-ci-tool with Apache License 2.0 5 votes vote down vote up
def set_provider_item(self, items, required=False):
        ''' set a provider item based on the list of item names provided. '''
        for item in items:
            provider_key = items[0]
            if item in self._idp:
                self.provider[provider_key] = self._idp.pop(item)
                break
        else:
            default = self.get_default(provider_key)
            if default is not None:
                self.provider[provider_key] = default
            elif required:
                raise errors.AnsibleFilterError("|failed provider {0} missing "
                                                "required key {1}".format(self.__class__.__name__, provider_key)) 
Example #30
Source File: oo_filters.py    From origin-ci-tool with Apache License 2.0 5 votes vote down vote up
def oo_merge_hostvars(hostvars, variables, inventory_hostname):
        """ Merge host and play variables.

            When ansible version is greater than or equal to 2.0.0,
            merge hostvars[inventory_hostname] with variables (ansible vars)
            otherwise merge hostvars with hostvars['inventory_hostname'].

            Ex: hostvars={'master1.example.com': {'openshift_variable': '3'},
                          'openshift_other_variable': '7'}
                variables={'openshift_other_variable': '6'}
                inventory_hostname='master1.example.com'
                returns {'openshift_variable': '3', 'openshift_other_variable': '7'}

                hostvars=<ansible.vars.hostvars.HostVars object> (Mapping)
                variables={'openshift_other_variable': '6'}
                inventory_hostname='master1.example.com'
                returns {'openshift_variable': '3', 'openshift_other_variable': '6'}
        """
        if not isinstance(hostvars, Mapping):
            raise errors.AnsibleFilterError("|failed expects hostvars is dictionary or object")
        if not isinstance(variables, dict):
            raise errors.AnsibleFilterError("|failed expects variables is a dictionary")
        if not isinstance(inventory_hostname, basestring):
            raise errors.AnsibleFilterError("|failed expects inventory_hostname is a string")
        # pylint: disable=no-member
        ansible_version = pkg_resources.get_distribution("ansible").version
        merged_hostvars = {}
        if LooseVersion(ansible_version) >= LooseVersion('2.0.0'):
            merged_hostvars = FilterModule.oo_merge_dicts(hostvars[inventory_hostname],
                                                          variables)
        else:
            merged_hostvars = FilterModule.oo_merge_dicts(hostvars[inventory_hostname],
                                                          hostvars)
        return merged_hostvars