Exemplo n.º 1
0
 def _cast_value(self, value):
     if value in BOOLEANS_TRUE:
         return True
     elif value in BOOLEANS_FALSE:
         return False
     elif re.match(r"^\d+\.d+$", value):
         return float(value)
     elif re.match(r"^\d+$", value):
         return int(value)
     else:
         return text_type(value)
Exemplo n.º 2
0
def convert_yaml_objects_to_native(obj):
    """Older versions of the ``toml`` python library, don't have a pluggable
    way to tell the encoder about custom types, so we need to ensure objects
    that we pass are native types.

    Only used on ``toml<0.10.0`` where ``toml.TomlEncoder`` is missing.

    This function recurses an object and ensures we cast any of the types from
    ``assible.parsing.yaml.objects`` into their native types, effectively cleansing
    the data before we hand it over to ``toml``

    This function doesn't directly check for the types from ``assible.parsing.yaml.objects``
    but instead checks for the types those objects inherit from, to offer more flexibility.
    """
    if isinstance(obj, dict):
        return dict((k, convert_yaml_objects_to_native(v)) for k, v in obj.items())
    elif isinstance(obj, list):
        return [convert_yaml_objects_to_native(v) for v in obj]
    elif isinstance(obj, text_type):
        return text_type(obj)
    else:
        return obj
Exemplo n.º 3
0
    def _get_magic_variables(self,
                             play,
                             host,
                             task,
                             include_hostvars,
                             include_delegate_to,
                             _hosts=None,
                             _hosts_all=None):
        '''
        Returns a dictionary of so-called "magic" variables in Assible,
        which are special variables we set internally for use.
        '''

        variables = {}
        variables['playbook_dir'] = os.path.abspath(self._loader.get_basedir())
        variables['assible_playbook_python'] = sys.executable
        variables['assible_config_file'] = C.CONFIG_FILE

        if play:
            # This is a list of all role names of all dependencies for all roles for this play
            dependency_role_names = list(
                set([
                    d.get_name() for r in play.roles
                    for d in r.get_all_dependencies()
                ]))
            # This is a list of all role names of all roles for this play
            play_role_names = [r.get_name() for r in play.roles]

            # assible_role_names includes all role names, dependent or directly referenced by the play
            variables['assible_role_names'] = list(
                set(dependency_role_names + play_role_names))
            # assible_play_role_names includes the names of all roles directly referenced by this play
            # roles that are implicitly referenced via dependencies are not listed.
            variables['assible_play_role_names'] = play_role_names
            # assible_dependent_role_names includes the names of all roles that are referenced via dependencies
            # dependencies that are also explicitly named as roles are included in this list
            variables['assible_dependent_role_names'] = dependency_role_names

            # DEPRECATED: role_names should be deprecated in favor of assible_role_names or assible_play_role_names
            variables['role_names'] = variables['assible_play_role_names']

            variables['assible_play_name'] = play.get_name()

        if task:
            if task._role:
                variables['role_name'] = task._role.get_name(
                    include_role_fqcn=False)
                variables['role_path'] = task._role._role_path
                variables['role_uuid'] = text_type(task._role._uuid)
                variables[
                    'assible_collection_name'] = task._role._role_collection
                variables['assible_role_name'] = task._role.get_name()

        if self._inventory is not None:
            variables['groups'] = self._inventory.get_groups_dict()
            if play:
                templar = Templar(loader=self._loader)
                if templar.is_template(play.hosts):
                    pattern = 'all'
                else:
                    pattern = play.hosts or 'all'
                # add the list of hosts in the play, as adjusted for limit/filters
                if not _hosts_all:
                    _hosts_all = [
                        h.name for h in self._inventory.get_hosts(
                            pattern=pattern, ignore_restrictions=True)
                    ]
                if not _hosts:
                    _hosts = [h.name for h in self._inventory.get_hosts()]

                variables['assible_play_hosts_all'] = _hosts_all[:]
                variables['assible_play_hosts'] = [
                    x for x in variables['assible_play_hosts_all']
                    if x not in play._removed_hosts
                ]
                variables['assible_play_batch'] = [
                    x for x in _hosts if x not in play._removed_hosts
                ]

                # DEPRECATED: play_hosts should be deprecated in favor of assible_play_batch,
                # however this would take work in the templating engine, so for now we'll add both
                variables['play_hosts'] = variables['assible_play_batch']

        # the 'omit' value allows params to be left out if the variable they are based on is undefined
        variables['omit'] = self._omit_token
        # Set options vars
        for option, option_value in iteritems(self._options_vars):
            variables[option] = option_value

        if self._hostvars is not None and include_hostvars:
            variables['hostvars'] = self._hostvars

        return variables
Exemplo n.º 4
0
 def env_prefix(**args):
     return ' '.join([
         '%s=%s' % (k, shlex_quote(text_type(v)))
         for k, v in args.items()
     ])
Exemplo n.º 5
0
def get_aws_connection_info(module, boto3=False):

    # Check module args for credentials, then check environment vars
    # access_key

    ec2_url = module.params.get('ec2_url')
    access_key = module.params.get('aws_access_key')
    secret_key = module.params.get('aws_secret_key')
    security_token = module.params.get('security_token')
    region = get_aws_region(module, boto3)
    profile_name = module.params.get('profile')
    validate_certs = module.params.get('validate_certs')
    config = module.params.get('aws_config')

    if not ec2_url:
        if 'AWS_URL' in os.environ:
            ec2_url = os.environ['AWS_URL']
        elif 'EC2_URL' in os.environ:
            ec2_url = os.environ['EC2_URL']

    if not access_key:
        if os.environ.get('AWS_ACCESS_KEY_ID'):
            access_key = os.environ['AWS_ACCESS_KEY_ID']
        elif os.environ.get('AWS_ACCESS_KEY'):
            access_key = os.environ['AWS_ACCESS_KEY']
        elif os.environ.get('EC2_ACCESS_KEY'):
            access_key = os.environ['EC2_ACCESS_KEY']
        elif HAS_BOTO and boto.config.get('Credentials', 'aws_access_key_id'):
            access_key = boto.config.get('Credentials', 'aws_access_key_id')
        elif HAS_BOTO and boto.config.get('default', 'aws_access_key_id'):
            access_key = boto.config.get('default', 'aws_access_key_id')
        else:
            # in case access_key came in as empty string
            access_key = None

    if not secret_key:
        if os.environ.get('AWS_SECRET_ACCESS_KEY'):
            secret_key = os.environ['AWS_SECRET_ACCESS_KEY']
        elif os.environ.get('AWS_SECRET_KEY'):
            secret_key = os.environ['AWS_SECRET_KEY']
        elif os.environ.get('EC2_SECRET_KEY'):
            secret_key = os.environ['EC2_SECRET_KEY']
        elif HAS_BOTO and boto.config.get('Credentials', 'aws_secret_access_key'):
            secret_key = boto.config.get('Credentials', 'aws_secret_access_key')
        elif HAS_BOTO and boto.config.get('default', 'aws_secret_access_key'):
            secret_key = boto.config.get('default', 'aws_secret_access_key')
        else:
            # in case secret_key came in as empty string
            secret_key = None

    if not security_token:
        if os.environ.get('AWS_SECURITY_TOKEN'):
            security_token = os.environ['AWS_SECURITY_TOKEN']
        elif os.environ.get('AWS_SESSION_TOKEN'):
            security_token = os.environ['AWS_SESSION_TOKEN']
        elif os.environ.get('EC2_SECURITY_TOKEN'):
            security_token = os.environ['EC2_SECURITY_TOKEN']
        elif HAS_BOTO and boto.config.get('Credentials', 'aws_security_token'):
            security_token = boto.config.get('Credentials', 'aws_security_token')
        elif HAS_BOTO and boto.config.get('default', 'aws_security_token'):
            security_token = boto.config.get('default', 'aws_security_token')
        else:
            # in case secret_token came in as empty string
            security_token = None

    if HAS_BOTO3 and boto3:
        boto_params = dict(aws_access_key_id=access_key,
                           aws_secret_access_key=secret_key,
                           aws_session_token=security_token)
        boto_params['verify'] = validate_certs

        if profile_name:
            boto_params = dict(aws_access_key_id=None, aws_secret_access_key=None, aws_session_token=None)
            boto_params['profile_name'] = profile_name

    else:
        boto_params = dict(aws_access_key_id=access_key,
                           aws_secret_access_key=secret_key,
                           security_token=security_token)

        # only set profile_name if passed as an argument
        if profile_name:
            boto_params['profile_name'] = profile_name

        boto_params['validate_certs'] = validate_certs

    if config is not None:
        if HAS_BOTO3 and boto3:
            boto_params['aws_config'] = botocore.config.Config(**config)
        elif HAS_BOTO and not boto3:
            if 'user_agent' in config:
                sys.modules["boto.connection"].UserAgent = config['user_agent']

    for param, value in boto_params.items():
        if isinstance(value, binary_type):
            boto_params[param] = text_type(value, 'utf-8', 'strict')

    return region, ec2_url, boto_params
Exemplo n.º 6
0
    def template(self,
                 variable,
                 convert_bare=False,
                 preserve_trailing_newlines=True,
                 escape_backslashes=True,
                 fail_on_undefined=None,
                 overrides=None,
                 convert_data=True,
                 static_vars=None,
                 cache=True,
                 disable_lookups=False):
        '''
        Templates (possibly recursively) any given data as input. If convert_bare is
        set to True, the given data will be wrapped as a jinja2 variable ('{{foo}}')
        before being sent through the template engine.
        '''
        static_vars = [''] if static_vars is None else static_vars

        # Don't template unsafe variables, just return them.
        if hasattr(variable, '__UNSAFE__'):
            return variable

        if fail_on_undefined is None:
            fail_on_undefined = self._fail_on_undefined_errors

        try:
            if convert_bare:
                variable = self._convert_bare_variable(variable)

            if isinstance(variable, string_types):
                result = variable

                if self.is_possibly_template(variable):
                    # Check to see if the string we are trying to render is just referencing a single
                    # var.  In this case we don't want to accidentally change the type of the variable
                    # to a string by using the jinja template renderer. We just want to pass it.
                    only_one = self.SINGLE_VAR.match(variable)
                    if only_one:
                        var_name = only_one.group(1)
                        if var_name in self._available_variables:
                            resolved_val = self._available_variables[var_name]
                            if isinstance(resolved_val, NON_TEMPLATED_TYPES):
                                return resolved_val
                            elif resolved_val is None:
                                return C.DEFAULT_NULL_REPRESENTATION

                    # Using a cache in order to prevent template calls with already templated variables
                    sha1_hash = None
                    if cache:
                        variable_hash = sha1(
                            text_type(variable).encode('utf-8'))
                        options_hash = sha1(
                            (text_type(preserve_trailing_newlines) +
                             text_type(escape_backslashes) +
                             text_type(fail_on_undefined) +
                             text_type(overrides)).encode('utf-8'))
                        sha1_hash = variable_hash.hexdigest(
                        ) + options_hash.hexdigest()
                    if cache and sha1_hash in self._cached_result:
                        result = self._cached_result[sha1_hash]
                    else:
                        result = self.do_template(
                            variable,
                            preserve_trailing_newlines=
                            preserve_trailing_newlines,
                            escape_backslashes=escape_backslashes,
                            fail_on_undefined=fail_on_undefined,
                            overrides=overrides,
                            disable_lookups=disable_lookups,
                        )

                        if not self.jinja2_native:
                            unsafe = hasattr(result, '__UNSAFE__')
                            if convert_data and not self._no_type_regex.match(
                                    variable):
                                # if this looks like a dictionary or list, convert it to such using the safe_eval method
                                if (result.startswith("{") and not result.startswith(self.environment.variable_start_string)) or \
                                        result.startswith("[") or result in ("True", "False"):
                                    eval_results = safe_eval(
                                        result, include_exceptions=True)
                                    if eval_results[1] is None:
                                        result = eval_results[0]
                                        if unsafe:
                                            result = wrap_var(result)
                                    else:
                                        # FIXME: if the safe_eval raised an error, should we do something with it?
                                        pass

                        # we only cache in the case where we have a single variable
                        # name, to make sure we're not putting things which may otherwise
                        # be dynamic in the cache (filters, lookups, etc.)
                        if cache and only_one:
                            self._cached_result[sha1_hash] = result

                return result

            elif is_sequence(variable):
                return [
                    self.template(
                        v,
                        preserve_trailing_newlines=preserve_trailing_newlines,
                        fail_on_undefined=fail_on_undefined,
                        overrides=overrides,
                        disable_lookups=disable_lookups,
                    ) for v in variable
                ]
            elif isinstance(variable, Mapping):
                d = {}
                # we don't use iteritems() here to avoid problems if the underlying dict
                # changes sizes due to the templating, which can happen with hostvars
                for k in variable.keys():
                    if k not in static_vars:
                        d[k] = self.template(
                            variable[k],
                            preserve_trailing_newlines=
                            preserve_trailing_newlines,
                            fail_on_undefined=fail_on_undefined,
                            overrides=overrides,
                            disable_lookups=disable_lookups,
                        )
                    else:
                        d[k] = variable[k]
                return d
            else:
                return variable

        except AssibleFilterError:
            if self._fail_on_filter_errors:
                raise
            else:
                return variable