Python ansible.module_utils._text.to_text() Examples

The following are 30 code examples of ansible.module_utils._text.to_text(). 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.module_utils._text , or try the search function .
Example #1
Source File: arubaoss.py    From aruba-ansible-modules with Apache License 2.0 6 votes vote down vote up
def _validate_request(self, method, payload, check):
        '''Compares value being applied to the configuration present on the device'''
        check_presence = self.get_config(check)
        if method == 'DELETE':
            if not check_presence:
                response = {'changed': False,
                            'failed': False,
                            'msg': 'Not present'}
                return response
        elif method != 'GET':
            if check_presence:
                diffkeys = False
                data = self._module.from_json(to_text(check_presence))
                for key in payload:
                    if key in data:
                        if payload[key] != data[key]:
                            diffkeys = True
                            break

                if not diffkeys:
                    data['changed'] = False
                    data['failed'] = False
                    return data
        return None 
Example #2
Source File: redhat_manifest.py    From foreman-ansible-modules with GNU General Public License v3.0 6 votes vote down vote up
def get_manifest(module):
    path = "/subscription/owners/%s/consumers?type=satellite" % (module.params['rhsm_owner'])
    resp, info = fetch_portal(module, path, 'GET')
    manifests = json.loads(to_text(resp.read()))
    if module.params['name']:
        attr = 'name'
    if module.params['uuid']:
        attr = 'uuid'
    manifest = [m for m in manifests if m[attr] == module.params[attr]]
    if manifest:
        if module.params['state'] == 'present':
            return manifest[0], False
        if module.params['state'] == 'absent':
            if not module.check_mode:
                return delete_manifest(module, manifest[0]['uuid']), True
            return None, True
    elif module.params['state'] == 'present':
        if not module.check_mode:
            return create_manifest(module), True
        return None, True
    return None, False 
Example #3
Source File: alicloud_ecs.py    From alibaba.alicloud with Apache License 2.0 6 votes vote down vote up
def _get_hostname(self, instance, hostnames):
        '''
            :param instance: an instance dict returned by describe_instances()
            :param hostnames: a list of hostname destination variables in order of preference
            :return the preferred identifer for the host
        '''
        if not hostnames:
            hostnames = ['instance_id', 'instance_name']

        hostname = None
        for preference in hostnames:
            if 'tag' in preference:
                hostname = self._get_tag_hostname(preference, instance)
            else:
                hostname = self._get_instance_attr(preference, instance)
            if hostname:
                break
        if hostname:
            if ':' in to_text(hostname):
                return self._sanitize_group_name((to_text(hostname)))
            else:
                return to_text(hostname) 
Example #4
Source File: load_lab_templates.py    From network-programmability-stream with MIT License 6 votes vote down vote up
def get_vars(self, loader, path, entities):
        super().get_vars(loader, path, entities)

        result = {}
        lab_templates_dir = os.path.join(path, 'user-data', 'topologies')
        lab_templates = {}
        if os.path.isdir(lab_templates_dir):
            for dir_path, subdirs, files in os.walk(lab_templates_dir):
                for filename in files:
                    base_filename, extension = os.path.splitext(filename)
                    if base_filename == 'topology' and to_text(extension) in constants.YAML_FILENAME_EXTENSIONS:
                        full_path = os.path.join(dir_path, filename)
                        dir_name = os.path.basename(os.path.dirname(full_path))
                        lab_templates[dir_name] = loader.load_from_file(
                            full_path,
                            cache=True,
                            unsafe=False
                        )
        result['topologies'] = lab_templates
        return result 
Example #5
Source File: alicloud_ecs.py    From alibaba.alicloud with Apache License 2.0 6 votes vote down vote up
def _get_tag_hostname(self, preference, instance):
        tag_hostnames = preference['tags']
        if ',' in tag_hostnames:
            tag_hostnames = tag_hostnames.split(',')
        else:
            tag_hostnames = [tag_hostnames]
        tags = instance.get('tags', {})
        for v in tag_hostnames:
            if '=' in v:
                tag_name, tag_value = v.split('=')
                if tags.get(tag_name) == tag_value:
                    return to_text(tag_name) + "_" + to_text(tag_value)
            else:
                tag_value = tags.get(v)
                if tag_value:
                    return to_text(tag_value)
        return None 
Example #6
Source File: utils.py    From pipelinewise with Apache License 2.0 6 votes vote down vote up
def vault_format_ciphertext_yaml(b_ciphertext, indent=None, name=None):
    """
    Format a ciphertext to YAML compatible string
    """
    indent = indent or 10

    block_format_var_name = ''
    if name:
        block_format_var_name = '%s: ' % name

    block_format_header = '%s!vault |' % block_format_var_name
    lines = []
    vault_ciphertext = to_text(b_ciphertext)

    lines.append(block_format_header)
    for line in vault_ciphertext.splitlines():
        lines.append('%s%s' % (' ' * indent, line))

    yaml_ciphertext = '\n'.join(lines)
    return yaml_ciphertext 
Example #7
Source File: plugin_formatter.py    From citrix-adc-ansible-modules with GNU General Public License v3.0 6 votes vote down vote up
def html_ify(text):
    ''' convert symbols like I(this is in italics) to valid HTML '''

    if not isinstance(text, string_types):
        text = to_text(text)

    t = html_escape(text)
    t = _ITALIC.sub(r"<em>\1</em>", t)
    t = _BOLD.sub(r"<b>\1</b>", t)
    t = _MODULE.sub(r"<span class='module'>\1</span>", t)
    t = _URL.sub(r"<a href='\1'>\1</a>", t)
    t = _LINK.sub(r"<a href='\2'>\1</a>", t)
    t = _CONST.sub(r"<code>\1</code>", t)
    t = _RULER.sub(r"<hr/>", t)

    return t.strip() 
Example #8
Source File: sshjail.py    From ansible-sshjail with MIT License 6 votes vote down vote up
def match_jail(self):
        if self.jid is None:
            code, stdout, stderr = self._jailhost_command("jls -q jid name host.hostname path")
            if code != 0:
                display.vvv("JLS stdout: %s" % stdout)
                raise AnsibleError("jls returned non-zero!")

            lines = stdout.strip().split(b'\n')
            found = False
            for line in lines:
                if line.strip() == '':
                    break

                jid, name, hostname, path = to_text(line).strip().split()
                if name == self.jailspec or hostname == self.jailspec:
                    self.jid = jid
                    self.jname = name
                    self.jpath = path
                    found = True
                    break

            if not found:
                raise AnsibleError("failed to find a jail with name or hostname of '%s'" % self.jailspec) 
Example #9
Source File: gcp_compute.py    From google.cloud with GNU General Public License v3.0 6 votes vote down vote up
def _populate_host(self, item):
        """
            :param item: A GCP instance
        """
        hostname = item.hostname()
        self.inventory.add_host(hostname)
        for key in item.to_json():
            try:
                self.inventory.set_variable(
                    hostname, self.get_option("vars_prefix") + key, item.to_json()[key]
                )
            except (ValueError, TypeError) as e:
                self.display.warning(
                    "Could not set host info hostvar for %s, skipping %s: %s"
                    % (hostname, key, to_text(e))
                )
        self.inventory.add_child("all", hostname) 
Example #10
Source File: foreman.py    From foreman-ansible-modules with GNU General Public License v3.0 6 votes vote down vote up
def send_facts(self, host, data):
        """
        Sends facts to Foreman, to be parsed by foreman_ansible fact
        parser.  The default fact importer should import these facts
        properly.
        """
        data["_type"] = "ansible"
        data["_timestamp"] = datetime.now().strftime(self.TIME_FORMAT)
        facts = {"name": host,
                 "facts": data,
                 }
        try:
            r = requests.post(url=self.FOREMAN_URL + '/api/v2/hosts/facts',
                              data=json.dumps(facts),
                              headers=self.FOREMAN_HEADERS,
                              cert=self.FOREMAN_SSL_CERT,
                              verify=self.ssl_verify)
            r.raise_for_status()
        except requests.exceptions.RequestException as err:
            print(to_text(err)) 
Example #11
Source File: aos_switch_filters.py    From aruba-switch-ansible with Apache License 2.0 6 votes vote down vote up
def find_version(serach_string, version):
    """
    Return the current SWI Version of the selected Image
    :param serach_string: string that shall be looked through
    :param version: string for one of the following (primary,secondary,primary_boot,secondary_boot)
    :return: SWI Version as string
    """
    regex = u"(?:WC|YA|YC|KB|WB|K|KB)\.[0-9]{2}\.[0-9]{2}\.[0-9]{4}"
    matches = re.findall(regex, serach_string)
    if version == "primary":
        return matches[0]
    elif version == "secondary":
        return matches[1]
    elif version == "primary_boot":
        return matches[2]
    elif version == "secondary_boot":
        return matches[3]
    else:
        raise AnsibleParserError(
            'No correct version selector entered. Choose one of the following:'
            ' primary,secondary,primary_boot,secondary_boot. You entered: %s .' % to_text(version)) 
Example #12
Source File: common_filters.py    From aruba-switch-ansible with Apache License 2.0 6 votes vote down vote up
def json_type_converter(current_dict, typelist):
    """
    This filter fill allow you to build JSON Bodies with Data types of booleans and integers.
    If you enter values which are not in the dict, nothing will happen. This allows you to use this function even for dynamic bodies.
    :param current_dict: the current dict in which strings are that shall be booleans or integers
    :param typelist: a list of list where by each list has a dict key at index 0 and either "int" or "boolean" at index 1.
    :return: current_dict with correct types, best directly transfered into module
    """
    for tuple in typelist:
        if tuple[0] in current_dict:
            type = tuple[1]
            if type == "boolean":
                current_dict[tuple[0]] = bool(current_dict[tuple[0]])
            elif type == "int":
                current_dict[tuple[0]] = int(current_dict[tuple[0]])
            else:
                raise AnsibleParserError(
                    'You entered the not valid type %s for the key %s . Only "int" or "boolean" is allowed.' % (to_text(type),to_text(type[0])))

    return current_dict 
Example #13
Source File: csv.py    From commcare-cloud with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def parse(self, inventory, loader, path, cache=False):
        super(InventoryModule, self).parse(inventory, loader, path)

        try:
            if self.loader:
                (b_data, private) = self.loader._get_file_contents(path)
            else:
                b_path = to_bytes(path, errors='surrogate_or_strict')
                with open(b_path, 'rb') as fh:
                    b_data = fh.read()

            # Faster to do to_text once on a long string than many
            # times on smaller strings
            data = to_text(b_data, errors='surrogate_or_strict').splitlines()

            self._parse(data)
        except Exception as e:
            raise AnsibleParserError(e) 
Example #14
Source File: vagrant.py    From learning-tools with MIT License 6 votes vote down vote up
def get_a_ssh_config(box_name):
    """Gives back a map of all the machine's ssh configurations"""

    output = to_text(subprocess.check_output(["vagrant", "ssh-config", box_name]), errors='surrogate_or_strict')
    config = SSHConfig()
    config.parse(StringIO(output))
    host_config = config.lookup(box_name)

    # man 5 ssh_config:
    # > It is possible to have multiple identity files ...
    # > all these identities will be tried in sequence.
    for id in host_config['identityfile']:
        if os.path.isfile(id):
            host_config['identityfile'] = id

    return dict((v, host_config[k]) for k, v in _ssh_to_ansible)


# List out servers that vagrant has running
# ------------------------------ 
Example #15
Source File: display.py    From litmus with Apache License 2.0 6 votes vote down vote up
def banner_cowsay(self, msg, color=None):
        if u": [" in msg:
            msg = msg.replace(u"[", u"")
            if msg.endswith(u"]"):
                msg = msg[:-1]
        runcmd = [self.b_cowsay, b"-W", b"60"]
        if self.noncow:
            thecow = self.noncow
            if thecow == 'random':
                thecow = random.choice(list(self.cows_available))
            runcmd.append(b'-f')
            runcmd.append(to_bytes(thecow))
        runcmd.append(to_bytes(msg))
        cmd = subprocess.Popen(runcmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (out, err) = cmd.communicate()
        self.display(u"%s\n" % to_text(out), color=color) 
Example #16
Source File: build.py    From FTDAnsible with GNU General Public License v3.0 6 votes vote down vote up
def _authorize(self, username, password):
        def request_token(url):
            resp = open_url(
                url,
                method=HTTPMethod.POST,
                data=json.dumps({'grant_type': 'password', 'username': username, 'password': password}),
                headers=BASE_HEADERS,
                validate_certs=False
            ).read()
            return json.loads(to_text(resp))

        api_versions = sorted(self._fetch_api_versions(), reverse=True)

        for version in api_versions:
            token_url = self._hostname + self.TOKEN_PATH_TEMPLATE.format(version)
            try:
                token = request_token(token_url)
            except urllib_error.HTTPError as e:
                logger.debug("Can't get token for API version: %s", version, exc_info=True)
                if e.code != HTTPStatus.UNAUTHORIZED:
                    raise
            else:
                return token

        raise Exception("Can't fetch token via API") 
Example #17
Source File: common.py    From FTDAnsible with GNU General Public License v3.0 6 votes vote down vote up
def equal_values(v1, v2):
    """
    Checks whether types and content of two values are the same. In case of complex objects, the method might be
    called recursively.

    :param v1: first value
    :param v2: second value
    :return: True if types and content of passed values are equal. Otherwise, returns False.
    :rtype: bool
    """

    # string-like values might have same text but different types, so checking them separately
    if is_string(v1) and is_string(v2):
        return to_text(v1) == to_text(v2)

    if type(v1) != type(v2):
        return False
    value_type = type(v1)

    if value_type == list:
        return equal_lists(v1, v2)
    elif value_type == dict:
        return equal_dicts(v1, v2)
    else:
        return v1 == v2 
Example #18
Source File: constants.py    From CloudEngine-Ansible with GNU General Public License v3.0 6 votes vote down vote up
def _get_config(p, section, key, env_var, default):
    ''' helper function for get_config '''
    value = default

    if p is not None:
        try:
            value = p.get(section, key, raw=True)
        except:
            pass

    if env_var is not None:
        env_value = os.environ.get(env_var, None)
        if env_value is not None:
            value = env_value

    return to_text(value, errors='surrogate_or_strict', nonstring='passthru') 
Example #19
Source File: arubaoss.py    From aruba-ansible-modules with Apache License 2.0 6 votes vote down vote up
def run_commands(self, commands=None, check_rc=False):
        '''
        Run commands on the switch
        '''
        if commands is None:
            raise ValueError("'commands' value is required")
        responses = list()
        for cmd in to_list(commands):

            if not isinstance(cmd, Mapping):
                cmd = {'command': cmd}

            try:
                out = self.send_command(**cmd)
            except AnsibleConnectionFailure as exception:

                if check_rc:
                    raise
                out = getattr(exception, 'err', exception)

            out = to_text(out, errors='surrogate_or_strict')

            responses.append(out)

        return responses 
Example #20
Source File: arubaoss.py    From aruba-ansible-modules with Apache License 2.0 6 votes vote down vote up
def load_config(module, commands):
    '''
    Loads the configuration onto the switch
    '''
    rc, out, err = exec_command(module, 'configure terminal')
    if rc != 0:
        module.fail_json(msg='unable to enter configuration mode', err=to_text(out, errors='surrogate_then_replace'))

    for command in to_list(commands):
        if command == 'end':
            continue
        rc, out, err = exec_command(module, command)
        if rc != 0:
            module.fail_json(msg=to_text(err, errors='surrogate_then_replace'), command=command, rc=rc)

    exec_command(module, 'end') 
Example #21
Source File: tripleo_base.py    From tripleo-ansible with Apache License 2.0 6 votes vote down vote up
def _send_task_callback(self, task, templar):
        """Send a task callback for task start"""
        self._debug('_send_task_callback...')
        if self._callback_sent:
            return
        name = task.name
        try:
            task.name = to_text(templar.template(task.name,
                                                 fail_on_undefined=False),
                                nonstring='empty')
        except Exception:
            self._debug('templating failed')
        self._tqm.send_callback('v2_playbook_on_task_start',
                                task,
                                is_conditional=False)
        task.name = name
        self._callback_sent = True 
Example #22
Source File: arubaoss.py    From aruba-ansible-modules with Apache License 2.0 6 votes vote down vote up
def get_cli_config(module, flags=None):
    '''
    Obtains the switch configuration
    '''
    flags = [] if flags is None else flags

    cmd = 'show running-config '
    cmd += ' '.join(flags)
    cmd = cmd.strip()

    try:
        return _DEVICE_CONFIGS[cmd]
    except KeyError:
        rc, out, err = exec_command(module, cmd)
        if rc != 0:
            module.fail_json(msg='unable to retrieve current config', stderr=to_text(err, errors='surrogate_then_replace'))
        cfg = to_text(out, errors='surrogate_then_replace').strip()
        _DEVICE_CONFIGS[cmd] = cfg
        return cfg 
Example #23
Source File: ztp_vars.py    From aruba-switch-ansible with Apache License 2.0 6 votes vote down vote up
def login(self, username, password):
        """
        Function handles login for REST API of the Switch
        :param username: The switch account username for authentication
        :param password: The switch account password authentication
        :return: Session object
        """
        # Create Session Object
        session = requests.Session()
        # Authenticate Session Object
        response = session.post(self.base_url + "login",
                                params={"username": username, "password": password}, verify=False,
                                timeout=2)
        if response.status_code != 200:
            raise AnsibleParserError('Login Request Failed with Status Code %s .' % to_text(str(response.status_code)))
        else:
            return session 
Example #24
Source File: gcp_utils.py    From google.cloud with GNU General Public License v3.0 5 votes vote down vote up
def _convert_value(self, value):
        if isinstance(value, list):
            new_list = []
            for item in value:
                new_list.append(self._convert_value(item))
            return new_list
        elif isinstance(value, dict):
            new_dict = {}
            for key in value:
                new_dict[key] = self._convert_value(value[key])
            return new_dict
        else:
            return to_text(value) 
Example #25
Source File: arubaoss.py    From aruba-ansible-modules with Apache License 2.0 5 votes vote down vote up
def run_cli_commands(module, commands, check_rc=False):
    conn = get_connection(module, True)
    try:
        return conn.run_commands(commands=commands, check_rc=check_rc)
    except ConnectionError as exc:
        module.fail_json(msg=to_text(exc)) 
Example #26
Source File: arubaoss.py    From aruba-ansible-modules with Apache License 2.0 5 votes vote down vote up
def get_device_info(self):
        '''
        Get device info
        '''
        device_info = {}

        device_info['network_os'] = 'aruba'
        reply = self.get('show version')
        data = to_text(reply, errors='surrogate_or_strict').strip()

        match = re.search(r'Version (\S+)', data)
        if match:
            device_info['network_os_version'] = match.group(1)

        match = re.search(r'^MODEL: (\S+)\),', data, re.M)
        if match:
            device_info['network_os_model'] = match.group(1)

        reply = self.get('show hostname')
        data = to_text(reply, errors='surrogate_or_strict').strip()

        match = re.search(r'^Hostname is (.+)', data, re.M)
        if match:
            device_info['network_os_hostname'] = match.group(1)

        return device_info 
Example #27
Source File: display.py    From litmus with Apache License 2.0 5 votes vote down vote up
def __init__(self, verbosity=0):

        self.columns = None
        self.verbosity = verbosity

        # list of all deprecation messages to prevent duplicate display
        self._deprecations = {}
        self._warns = {}
        self._errors = {}

        self.b_cowsay = None
        self.noncow = C.ANSIBLE_COW_SELECTION

        self.set_cowsay_info()

        if self.b_cowsay:
            try:
                cmd = subprocess.Popen([self.b_cowsay, "-l"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                (out, err) = cmd.communicate()
                self.cows_available = set([to_text(c) for c in out.split()])
                if C.ANSIBLE_COW_WHITELIST:
                    self.cows_available = set(C.ANSIBLE_COW_WHITELIST).intersection(self.cows_available)
            except:
                # could not execute cowsay for some reason
                self.b_cowsay = False

        self._set_column_width() 
Example #28
Source File: grafana_dashboard.py    From community.grafana with GNU General Public License v3.0 5 votes vote down vote up
def get_grafana_version(module, grafana_url, headers):
    grafana_version = None
    r, info = fetch_url(module, '%s/api/frontend/settings' % grafana_url, headers=headers, method='GET')
    if info['status'] == 200:
        try:
            settings = json.loads(to_text(r.read()))
            grafana_version = settings['buildInfo']['version'].split('.')[0]
        except UnicodeError as e:
            raise GrafanaAPIException('Unable to decode version string to Unicode')
        except Exception as e:
            raise GrafanaAPIException(e)
    else:
        raise GrafanaAPIException('Unable to get grafana version : %s' % info)

    return int(grafana_version) 
Example #29
Source File: grafana_annotations.py    From community.grafana with GNU General Public License v3.0 5 votes vote down vote up
def _send_annotation(self, annotation):
        try:
            open_url(self.grafana_url, data=json.dumps(annotation), headers=self.headers,
                     method="POST",
                     validate_certs=self.validate_grafana_certs,
                     url_username=self.grafana_user, url_password=self.grafana_password,
                     http_agent=self.http_agent, force_basic_auth=self.force_basic_auth)
        except Exception as e:
            self._display.error(u'Could not submit message to Grafana: %s' % to_text(e)) 
Example #30
Source File: grafana_folder.py    From community.grafana with GNU General Public License v3.0 5 votes vote down vote up
def get_folder(self, title):
        url = "/api/search?type=dash-folder&query={title}".format(title=quote(title))
        response = self._send_request(url, headers=self.headers, method="GET")
        for item in response:
            if item.get("title") == to_text(title):
                return item
        return None