ansible.utils package

Submodules

ansible.utils.boolean module

ansible.utils.boolean.boolean(value)[source]

ansible.utils.cmd_functions module

ansible.utils.cmd_functions.run_cmd(cmd, live=False, readsize=10)[source]

ansible.utils.color module

ansible.utils.color.stringc(text, color)[source]

String in color.

ansible.utils.color.colorize(lead, num, color)[source]

Print ‘lead’ = ‘num’ in ‘color’

ansible.utils.color.hostcolor(host, stats, color=True)[source]

ansible.utils.display module

class ansible.utils.display.Display(verbosity=0)[source]

Bases: object

banner(msg, color=None)[source]

Prints a header-looking line with stars taking up to 80 columns of width (3 columns, minimum)

banner_cowsay(msg, color=None)[source]
debug(msg)[source]
deprecated(msg, version=None, removed=False)[source]

used to print out a deprecation message.

display(msg, color=None, stderr=False, screen_only=False, log_only=False)[source]

Display a message to the user

Note: msg must be a unicode string to prevent UnicodeError tracebacks.

do_var_prompt(varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None)[source]
error(msg, wrap_text=True)[source]
static prompt(msg, private=False)[source]
set_cowsay_info()[source]
system_warning(msg)[source]
v(msg, host=None)[source]
verbose(msg, host=None, caplevel=2)[source]
vv(msg, host=None)[source]
vvv(msg, host=None)[source]
vvvv(msg, host=None)[source]
vvvvv(msg, host=None)[source]
vvvvvv(msg, host=None)[source]
warning(msg, formatted=False)[source]

ansible.utils.encrypt module

ansible.utils.encrypt.do_encrypt(result, encrypt, salt_size=None, salt=None)[source]

ansible.utils.hashing module

ansible.utils.hashing.secure_hash_s(data, hash_func=<built-in function openssl_sha1>)[source]

Return a secure hash hex digest of data.

ansible.utils.hashing.secure_hash(filename, hash_func=<built-in function openssl_sha1>)[source]

Return a secure hash hex digest of local file, None if file is not present or a directory.

ansible.utils.hashing.checksum(filename, hash_func=<built-in function openssl_sha1>)

Return a secure hash hex digest of local file, None if file is not present or a directory.

ansible.utils.hashing.checksum_s(data, hash_func=<built-in function openssl_sha1>)

Return a secure hash hex digest of data.

ansible.utils.hashing.md5s(data)[source]
ansible.utils.hashing.md5(filename)[source]

ansible.utils.listify module

ansible.utils.listify.listify_lookup_plugin_terms(terms, templar, loader, fail_on_undefined=False, convert_bare=True)[source]

ansible.utils.module_docs module

ansible.utils.module_docs.get_docstring(filename, verbose=False)[source]

Search for assignment of the DOCUMENTATION and EXAMPLES variables in the given file. Parse DOCUMENTATION from YAML and return the YAML doc or None together with EXAMPLES, as plain text.

DOCUMENTATION can be extended using documentation fragments loaded by the PluginLoader from the module_docs_fragments directory.

ansible.utils.path module

ansible.utils.path.unfrackpath(path)[source]

returns a path that is free of symlinks, environment variables, relative path traversals and symbols (~) example: ‘$HOME/../../var/mail’ becomes ‘/var/spool/mail’

ansible.utils.shlex module

ansible.utils.shlex.shlex_split(s, comments=False, posix=True)[source]

ansible.utils.unicode module

ansible.utils.unicode.to_unicode(obj, encoding='utf-8', errors='replace', nonstring=None)[source]

Convert an object (:obj:) into a :unicode: string.

Parameters:
  • obj – Object to convert to a unicode string. This should normally be a byte str
  • encoding – What encoding to try converting the byte str as. Defaults to utf-8
  • errors – If errors are found while decoding, perform this action. Defaults to replace which replaces the invalid bytes with a character that means the bytes were unable to be decoded. Other values are the same as the error handling schemes in the codec base classes. For instance strict which raises an exception and ignore which simply omits the non-decodable characters.
  • nonstring

    How to treat nonstring values. Possible values are:

    simplerepr:Attempt to call the object’s “simple representation” method and return that value. Python-2.3+ has two methods that try to return a simple representation: object.__unicode__() and object.__str__(). We first try to get a usable value from object.__unicode__(). If that fails we try the same with object.__str__().
    empty:Return an empty unicode string
    strict:Raise a TypeError
    passthru:Return the object unchanged
    repr:Attempt to return a unicode string of the repr of the object

    Default is simplerepr

Raises:
  • TypeError – if nonstring is strict and a non-basestring object is passed in or if nonstring is set to an unknown value
  • UnicodeDecodeError – if errors is strict and obj is not decodable using the given encoding
Returns:

unicode string or the original object depending on the value of nonstring.

Usually this should be used on a byte str but it can take both byte str and unicode strings intelligently. Nonstring objects are handled in different ways depending on the setting of the nonstring parameter.

The default values of this function are set so as to always return a unicode string and never raise an error when converting from a byte str to a unicode string. However, when you do not pass validly encoded text (or a nonstring object), you may end up with output that you don’t expect. Be sure you understand the requirements of your data, not just ignore errors by passing it through this function.

ansible.utils.unicode.to_bytes(obj, encoding='utf-8', errors='replace', nonstring=None)[source]

Convert an object into a byte str

Parameters:
  • obj – Object to convert to a byte str. This should normally be a unicode string.
  • encoding – Encoding to use to convert the unicode string into a byte str. Defaults to utf-8.
  • errors

    If errors are found while encoding, perform this action. Defaults to replace which replaces the invalid bytes with a character that means the bytes were unable to be encoded. Other values are the same as the error handling schemes in the codec base classes. For instance strict which raises an exception and ignore which simply omits the non-encodable characters.

  • nonstring

    How to treat nonstring values. Possible values are:

    simplerepr:Attempt to call the object’s “simple representation” method and return that value. Python-2.3+ has two methods that try to return a simple representation: object.__unicode__() and object.__str__(). We first try to get a usable value from object.__str__(). If that fails we try the same with object.__unicode__().
    empty:Return an empty byte str
    strict:Raise a TypeError
    passthru:Return the object unchanged
    repr:Attempt to return a byte str of the repr() of the object

    Default is simplerepr.

Raises:
  • TypeError – if nonstring is strict and a non-basestring object is passed in or if nonstring is set to an unknown value.
  • UnicodeEncodeError – if errors is strict and all of the bytes of obj are unable to be encoded using encoding.
Returns:

byte str or the original object depending on the value of nonstring.

Warning

If you pass a byte str into this function the byte str is returned unmodified. It is not re-encoded with the specified encoding. The easiest way to achieve that is:

to_bytes(to_unicode(text), encoding='utf-8')

The initial to_unicode() call will ensure text is a unicode string. Then, to_bytes() will turn that into a byte str with the specified encoding.

Usually, this should be used on a unicode string but it can take either a byte str or a unicode string intelligently. Nonstring objects are handled in different ways depending on the setting of the nonstring parameter.

The default values of this function are set so as to always return a byte str and never raise an error when converting from unicode to bytes. However, when you do not pass an encoding that can validly encode the object (or a non-string object), you may end up with output that you don’t expect. Be sure you understand the requirements of your data, not just ignore errors by passing it through this function.

ansible.utils.unicode.unicode_wrap(func, *args, **kwargs)[source]
ansible.utils.unicode.to_str(obj, encoding='utf-8', errors='replace', nonstring=None)

Convert an object into a byte str

Parameters:
  • obj – Object to convert to a byte str. This should normally be a unicode string.
  • encoding – Encoding to use to convert the unicode string into a byte str. Defaults to utf-8.
  • errors

    If errors are found while encoding, perform this action. Defaults to replace which replaces the invalid bytes with a character that means the bytes were unable to be encoded. Other values are the same as the error handling schemes in the codec base classes. For instance strict which raises an exception and ignore which simply omits the non-encodable characters.

  • nonstring

    How to treat nonstring values. Possible values are:

    simplerepr:Attempt to call the object’s “simple representation” method and return that value. Python-2.3+ has two methods that try to return a simple representation: object.__unicode__() and object.__str__(). We first try to get a usable value from object.__str__(). If that fails we try the same with object.__unicode__().
    empty:Return an empty byte str
    strict:Raise a TypeError
    passthru:Return the object unchanged
    repr:Attempt to return a byte str of the repr() of the object

    Default is simplerepr.

Raises:
  • TypeError – if nonstring is strict and a non-basestring object is passed in or if nonstring is set to an unknown value.
  • UnicodeEncodeError – if errors is strict and all of the bytes of obj are unable to be encoded using encoding.
Returns:

byte str or the original object depending on the value of nonstring.

Warning

If you pass a byte str into this function the byte str is returned unmodified. It is not re-encoded with the specified encoding. The easiest way to achieve that is:

to_bytes(to_unicode(text), encoding='utf-8')

The initial to_unicode() call will ensure text is a unicode string. Then, to_bytes() will turn that into a byte str with the specified encoding.

Usually, this should be used on a unicode string but it can take either a byte str or a unicode string intelligently. Nonstring objects are handled in different ways depending on the setting of the nonstring parameter.

The default values of this function are set so as to always return a byte str and never raise an error when converting from unicode to bytes. However, when you do not pass an encoding that can validly encode the object (or a non-string object), you may end up with output that you don’t expect. Be sure you understand the requirements of your data, not just ignore errors by passing it through this function.

ansible.utils.vars module

ansible.utils.vars.combine_vars(a, b)[source]

Return a copy of dictionaries of variables based on configured hash behavior

ansible.utils.vars.merge_hash(a, b)[source]

Recursively merges hash b into a so that keys from b take precedence over keys from a

ansible.utils.vars.load_extra_vars(loader, options)[source]
ansible.utils.vars.load_options_vars(options)[source]
ansible.utils.vars.isidentifier(ident)[source]

Determines, if string is valid Python identifier using the ast module. Orignally posted at: http://stackoverflow.com/a/29586366