Python ansible.module_utils._text.to_bytes() Examples
The following are 30
code examples of ansible.module_utils._text.to_bytes().
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: ansible_launcher.py From im with GNU General Public License v3.0 | 8 votes |
def get_play_prereqs_2_4(self, options): loader = DataLoader() if self.vault_pass: loader.set_vault_secrets([('default', VaultSecret(_bytes=to_bytes(self.vault_pass)))]) # create the inventory, and filter it based on the subset specified (if any) inventory = InventoryManager(loader=loader, sources=options.inventory) # create the variable manager, which will be shared throughout # the code, ensuring a consistent view of global variables try: # Ansible 2.8 variable_manager = VariableManager(loader=loader, inventory=inventory, version_info=self.version_info(ansible_version)) variable_manager._extra_vars = self.extra_vars except TypeError: variable_manager = VariableManager(loader=loader, inventory=inventory) variable_manager.extra_vars = self.extra_vars variable_manager.options_vars = {'ansible_version': self.version_info(ansible_version)} return loader, inventory, variable_manager
Example #2
Source File: display.py From litmus with Apache License 2.0 | 6 votes |
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 #3
Source File: json_patch.py From ansible-jsonpatch with MIT License | 6 votes |
def write(self): result = {'dest': self.outfile} if self.module.check_mode: # stop here before doing anything permanent return result dump_kwargs = {} if self.pretty_print: dump_kwargs.update({'indent': 4, 'separators': (',', ': ')}) if self.do_backup: # backup first if needed result.update(self.backup()) tmpfd, tmpfile = tempfile.mkstemp() with open(tmpfile, "w") as f: f.write(json.dumps(self.patcher.obj, **dump_kwargs)) self.module.atomic_move(tmpfile, to_native(os.path.realpath(to_bytes(self.outfile, errors='surrogate_or_strict')), errors='surrogate_or_strict'), unsafe_writes=self.module.params['unsafe_writes']) return result
Example #4
Source File: junit_report.py From infrared with Apache License 2.0 | 6 votes |
def _generate_report(self): """Generate a TestSuite report. Generate a TestSuite report from the collected TaskData and HostData. """ test_cases = [] for task_uuid, task_data in self._task_data.items(): if task_data.action == 'setup' and \ self._include_setup_tasks_in_report == 'false': continue for host_uuid, host_data in task_data.host_data.items(): test_cases.append(self._build_test_case(task_data, host_data)) test_suite = TestSuite(self._playbook_name, test_cases) report = TestSuite.to_xml_string([test_suite]) output_file = os.path.join(self._output_dir, '%s-%s.xml' % ( self._playbook_name, time.time())) with open(output_file, 'wb') as xml: xml.write(to_bytes(report, errors='surrogate_or_strict'))
Example #5
Source File: mixins.py From mitogen with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _transfer_data(self, remote_path, data): """ Used by the base _execute_module(), and in <2.4 also by the template action module, and probably others. """ if isinstance(data, dict): data = jsonify(data) if not isinstance(data, bytes): data = to_bytes(data, errors='surrogate_or_strict') LOG.debug('_transfer_data(%r, %s ..%d bytes)', remote_path, type(data), len(data)) self._connection.put_data(remote_path, data) return remote_path #: Actions listed here cause :func:`_fixup_perms2` to avoid a needless #: roundtrip, as they modify file modes separately afterwards. This is due #: to the method prototype having a default of `execute=True`.
Example #6
Source File: cloudformation_diff.py From ansible-role-cloudformation with MIT License | 6 votes |
def cfndiff_module_validation(module): ''' Validate for correct module call/usage in ansible. ''' # Boto3 is required! if not HAS_BOTO3: module.fail_json(msg='boto3 is required. Try pip install boto3') # cfn_flip is required! if not HAS_CFN_FLIP: module.fail_json(msg='cfn_flip is required. Try pip install cfn_flip') template = module.params['template'] b_template = to_bytes(template, errors='surrogate_or_strict') # Validate path of template if not os.path.exists(b_template): module.fail_json(msg="template %s not found" % (template)) if not os.access(b_template, os.R_OK): module.fail_json(msg="template %s not readable" % (template)) if os.path.isdir(b_template): module.fail_json(msg="diff does not support recursive diff of directory: %s" % (template)) return module
Example #7
Source File: podman_container.py From tripleo-ansible with Apache License 2.0 | 6 votes |
def construct_command_from_params(self): """Create a podman command from given module parameters. Returns: list -- list of byte strings for Popen command """ if self.action in ['start', 'stop', 'delete']: return self.start_stop_delete() if self.action in ['create', 'run']: cmd = [self.action, '--name', self.params['name']] all_param_methods = [func for func in dir(self) if callable(getattr(self, func)) and func.startswith("addparam")] params_set = (i for i in self.params if self.params[i] is not None) for param in params_set: func_name = "_".join(["addparam", param]) if func_name in all_param_methods: cmd = getattr(self, func_name)(cmd) cmd.append(self.params['image']) if self.params['command']: if isinstance(self.params['command'], list): cmd += self.params['command'] else: cmd += self.params['command'].split() return [to_bytes(i, errors='surrogate_or_strict') for i in cmd]
Example #8
Source File: lchroot.py From luna with GNU General Public License v3.0 | 6 votes |
def fetch_file(self, in_path, out_path): ''' fetch a file from chroot to local ''' super(Connection, self).fetch_file(in_path, out_path) display.vvv("FETCH %s TO %s" % (in_path, out_path), host=self.path) in_path = pipes.quote(self._prefix_login_path(in_path)) try: p = self._buffered_exec_command('dd if=%s bs=%s' % (in_path, BUFSIZE)) except OSError: raise AnsibleError("chroot connection requires dd command in the chroot") with open(to_bytes(out_path, errors='surrogate_or_strict'), 'wb+') as out_file: try: chunk = p.stdout.read(BUFSIZE) while chunk: out_file.write(chunk) chunk = p.stdout.read(BUFSIZE) except: traceback.print_exc() raise AnsibleError("failed to transfer file %s to %s" % (in_path, out_path)) stdout, stderr = p.communicate() if p.returncode != 0: raise AnsibleError("failed to transfer file %s to %s:\n%s\n%s" % (in_path, out_path, stdout, stderr))
Example #9
Source File: lchroot.py From luna with GNU General Public License v3.0 | 6 votes |
def put_file(self, in_path, out_path): ''' transfer a file from local to lchroot ''' super(Connection, self).put_file(in_path, out_path) display.vvv("PUT %s TO %s" % (in_path, out_path), host=self.path) out_path = pipes.quote(self._prefix_login_path(out_path)) try: with open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb') as in_file: try: p = self._buffered_exec_command('dd of=%s bs=%s' % (out_path, BUFSIZE), stdin=in_file) except OSError: raise AnsibleError("lchroot connection requires dd command in the lchroot") try: stdout, stderr = p.communicate() except: traceback.print_exc() raise AnsibleError("failed to transfer file %s to %s" % (in_path, out_path)) if p.returncode != 0: raise AnsibleError("failed to transfer file %s to %s:\n%s\n%s" % (in_path, out_path, stdout, stderr)) except IOError: raise AnsibleError("file or module does not exist at: %s" % in_path)
Example #10
Source File: lchroot.py From luna with GNU General Public License v3.0 | 6 votes |
def _buffered_exec_command(self, cmd, stdin=subprocess.PIPE): ''' run a command on the chroot. This is only needed for implementing put_file() get_file() so that we don't have to read the whole file into memory. compared to exec_command() it looses some niceties like being able to return the process's exit code immediately. ''' executable = C.DEFAULT_EXECUTABLE.split()[0] if C.DEFAULT_EXECUTABLE else '/bin/sh' local_cmd = [self.chroot_cmd, self.path, executable, '-c', cmd] display.vvv( "EXEC %s cmd is %s" % (local_cmd,cmd), host=self.path) local_cmd = [to_bytes(i, errors='surrogate_or_strict') for i in local_cmd] p = subprocess.Popen(local_cmd, shell=False, stdin=stdin, env={ 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin', 'FAKE_KERN': self.osimage.get('kernver'), 'LD_PRELOAD': 'libluna-fakeuname.so' }, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return p
Example #11
Source File: arubaoscx_rest.py From aruba-ansible-modules with Apache License 2.0 | 6 votes |
def update_play_context(self, pc_data): """Updates the play context information for the connection""" pc_data = to_bytes(pc_data) if PY3: pc_data = cPickle.loads(pc_data, encoding='bytes') else: pc_data = cPickle.loads(pc_data) play_context = PlayContext() play_context.deserialize(pc_data) messages = ['updating play_context for connection'] if self._play_context.become ^ play_context.become: self._httpapi.set_become(play_context) self._play_context = play_context return messages
Example #12
Source File: foreman_helper.py From foreman-ansible-modules with GNU General Public License v3.0 | 6 votes |
def connect(self): self.foremanapi = apypie.Api( uri=self._foremanapi_server_url, username=to_bytes(self._foremanapi_username), password=to_bytes(self._foremanapi_password), api_version=2, verify_ssl=self._foremanapi_validate_certs, ) self.ping() self._patch_templates_resource_name() self._patch_location_api() self._patch_subnet_rex_api() self.check_required_plugins()
Example #13
Source File: csv.py From commcare-cloud with BSD 3-Clause "New" or "Revised" License | 6 votes |
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: podman_container.py From tripleo-ansible with Apache License 2.0 | 5 votes |
def addparam_label(self, c): for label in self.params['label'].items(): c += ['--label', b'='.join([to_bytes(l, errors='surrogate_or_strict') for l in label])] return c
Example #15
Source File: plugin_formatter.py From citrix-adc-ansible-modules with GNU General Public License v3.0 | 5 votes |
def write_data(text, output_dir, outputname, module=None): ''' dumps module output to a file or the screen, as requested ''' if output_dir is not None: if module: outputname = outputname % module if not os.path.exists(output_dir): os.makedirs(output_dir) fname = os.path.join(output_dir, outputname) fname = fname.replace(".py", "") update_file_if_different(fname, to_bytes(text)) else: print(text)
Example #16
Source File: json_patch.py From ansible-jsonpatch with MIT License | 5 votes |
def set_module_args(args): """For dynamic module args (such as for testing).""" args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) basic._ANSIBLE_ARGS = to_bytes(args)
Example #17
Source File: foreman.py From foreman-ansible-modules with GNU General Public License v3.0 | 5 votes |
def _get_session(self): if not self.session: self.session = requests.session() self.session.auth = HTTPBasicAuth(self.get_option('user'), to_bytes(self.get_option('password'))) self.session.verify = self.get_option('validate_certs') return self.session
Example #18
Source File: junit_report.py From infrared with Apache License 2.0 | 5 votes |
def _cleanse_string(self, value): """Cleanse string. Convert surrogate escapes to the unicode replacement character to avoid XML encoding errors. """ return to_text(to_bytes(value, errors='surrogateescape'), errors='replace')
Example #19
Source File: xml.py From ansible-xml with GNU General Public License v3.0 | 5 votes |
def child_to_element(module, child, in_type): if in_type == 'xml': infile = BytesIO(to_bytes(child, errors='surrogate_or_strict')) try: parser = etree.XMLParser() node = etree.parse(infile, parser) return node.getroot() except etree.XMLSyntaxError as e: module.fail_json(msg="Error while parsing child element: %s" % e) elif in_type == 'yaml': if isinstance(child, string_types): return etree.Element(child) elif isinstance(child, MutableMapping): if len(child) > 1: module.fail_json(msg="Can only create children from hashes with one key") (key, value) = next(iteritems(child)) if isinstance(value, MutableMapping): children = value.pop('_', None) node = etree.Element(key, value) if children is not None: if not isinstance(children, list): module.fail_json(msg="Invalid children type: %s, must be list." % type(children)) subnodes = children_to_nodes(module, children) node.extend(subnodes) else: node = etree.Element(key) node.text = value return node else: module.fail_json(msg="Invalid child type: %s. Children must be either strings or hashes." % type(child)) else: module.fail_json(msg="Invalid child input type: %s. Type must be either xml or yaml." % in_type)
Example #20
Source File: urls.py From ansible-role-jenkins with Apache License 2.0 | 5 votes |
def basic_auth_header(username, password): """Takes a username and password and returns a byte string suitable for using as value of an Authorization header to do basic auth. """ return b"Basic %s" % base64.b64encode(to_bytes("%s:%s" % (username, password), errors='surrogate_or_strict'))
Example #21
Source File: podman_container.py From tripleo-ansible with Apache License 2.0 | 5 votes |
def addparam_sysctl(self, c): for sysctl in self.params['sysctl'].items(): c += ['--sysctl', b"=".join([to_bytes(k, errors='surrogate_or_strict') for k in sysctl])] return c
Example #22
Source File: test_grafana_team.py From community.grafana with GNU General Public License v3.0 | 5 votes |
def set_module_args(args): """prepare arguments so that they will be picked up during module creation""" args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) basic._ANSIBLE_ARGS = to_bytes(args)
Example #23
Source File: podman_container.py From tripleo-ansible with Apache License 2.0 | 5 votes |
def addparam_env(self, c): for env_value in self.params['env'].items(): c += ['--env', b"=".join([to_bytes(k, errors='surrogate_or_strict') for k in env_value])] return c
Example #24
Source File: podman_container.py From tripleo-ansible with Apache License 2.0 | 5 votes |
def start_stop_delete(self): if self.action in ['stop', 'start']: cmd = [self.action, self.params['name']] return [to_bytes(i, errors='surrogate_or_strict') for i in cmd] if self.action == 'delete': cmd = ['rm', '-f', self.params['name']] return [to_bytes(i, errors='surrogate_or_strict') for i in cmd]
Example #25
Source File: display.py From litmus with Apache License 2.0 | 5 votes |
def prompt(msg, private=False): prompt_string = to_bytes(msg, encoding=Display._output_encoding()) if sys.version_info >= (3,): # Convert back into text on python3. We do this double conversion # to get rid of characters that are illegal in the user's locale prompt_string = to_text(prompt_string) if private: return getpass.getpass(prompt_string) else: return input(prompt_string)
Example #26
Source File: ConfManager.py From im with GNU General Public License v3.0 | 5 votes |
def get_vault_editor(vault_password): """ Get the correct VaultEditor object in different Ansible versions """ if LooseVersion(ansible_version) >= LooseVersion("2.4.0"): # for Ansible version 2.4.0 or higher vault_secrets = [('default', VaultSecret(_bytes=to_bytes(vault_password)))] return VaultEditor(VaultLib(vault_secrets)) else: # for Ansible version 2.3.2 or lower return VaultEditor(vault_password)
Example #27
Source File: utils.py From ansible-mysql-query with GNU General Public License v3.0 | 5 votes |
def set_module_args(**kwargs): args = json.dumps({'ANSIBLE_MODULE_ARGS': kwargs}) basic._ANSIBLE_ARGS = to_bytes(args)
Example #28
Source File: test_grafana_user.py From community.grafana with GNU General Public License v3.0 | 5 votes |
def set_module_args(args): """prepare arguments so that they will be picked up during module creation""" args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) basic._ANSIBLE_ARGS = to_bytes(args)
Example #29
Source File: urls.py From ansible-role-jenkins with Apache License 2.0 | 4 votes |
def _inet_paton(ipname): """Try to convert an IP address to packed binary form Supports IPv4 addresses on all platforms and IPv6 on platforms with IPv6 support. """ # inet_aton() also accepts strings like '1' # Divergence: We make sure we have native string type for all python versions try: b_ipname = to_bytes(ipname, errors='strict') except UnicodeError: raise ValueError("%s must be an all-ascii string." % repr(ipname)) # Set ipname in native string format if sys.version_info < (3,): n_ipname = b_ipname else: n_ipname = ipname if n_ipname.count('.') == 3: try: return socket.inet_aton(n_ipname) # Divergence: OSError on late python3. socket.error earlier. # Null bytes generate ValueError on python3(we want to raise # ValueError anyway), TypeError # earlier except (OSError, socket.error, TypeError): pass try: return socket.inet_pton(socket.AF_INET6, n_ipname) # Divergence: OSError on late python3. socket.error earlier. # Null bytes generate ValueError on python3(we want to raise # ValueError anyway), TypeError # earlier except (OSError, socket.error, TypeError): # Divergence .format() to percent formatting for Python < 2.6 raise ValueError("%s is neither an IPv4 nor an IP6 " "address." % repr(ipname)) except AttributeError: # AF_INET6 not available pass # Divergence .format() to percent formatting for Python < 2.6 raise ValueError("%s is not an IPv4 address." % repr(ipname))
Example #30
Source File: intellij_configure_jdk.py From ansible-role-intellij with MIT License | 4 votes |
def configure_jdk(module, intellij_user_config_dir, jdk_name, jdk_home, uid, gid): options_dir = os.path.join(intellij_user_config_dir, 'options') project_default_path = os.path.join(options_dir, 'jdk.table.xml') if (not os.path.isfile(project_default_path) ) or os.path.getsize(project_default_path) == 0: if not module.check_mode: if not os.path.isdir(options_dir): make_dirs(options_dir, 0o775, uid, gid) if not os.path.isfile(project_default_path): with open(project_default_path, 'wb', 0o664) as xml_file: xml_file.write(to_bytes('')) os.chown(project_default_path, uid, gid) jdk_table_root = etree.Element('application') jdk_table_doc = etree.ElementTree(jdk_table_root) before = '' else: jdk_table_doc = etree.parse(project_default_path) jdk_table_root = jdk_table_doc.getroot() before = pretty_print(jdk_table_root) if jdk_table_root.tag != 'application': module.fail_json( msg='Unsupported root element: %s' % jdk_table_root.tag) project_jdk_table = jdk_table_root.find( './component[@name="ProjectJdkTable"]') if project_jdk_table is None: project_jdk_table = etree.SubElement( jdk_table_root, 'component', name='ProjectJdkTable') new_jdk = create_jdk_xml(module, intellij_user_config_dir, jdk_name, jdk_home) new_jdk_string = pretty_print(new_jdk) old_jdk = project_jdk_table.find('./jdk/name[@value="%s"]/..' % jdk_name) if old_jdk is None: old_jdk_string = '' changed = True project_jdk_table.append(new_jdk) else: old_jdk_string = pretty_print(old_jdk) changed = old_jdk_string != new_jdk_string if changed: project_jdk_table.replace(old_jdk, new_jdk) after = pretty_print(jdk_table_root) if changed and not module.check_mode: with open(project_default_path, 'wb') as xml_file: xml_file.write(to_bytes(after)) return changed, {'before:': before, 'after': after}