This page documents utilities intended to be helpful when writing Ansible modules in Python.
To use this functionality, include from ansible.module_utils.basic import AnsibleModule
in your module.
Common code for quickly building an ansible module in Python (although you can write modules with anything that can return JSON).
See Developing Ansible modules for a general introduction and Ansible module architecture for more detailed explanation.
for results that are files, supplement the info about the file in the return path with stats about the file path.
atomically move src to dest, copying attributes from dest, returns true on success it uses os.rename to ensure this as it is an atomic operation, rest of the function is to work around limitations, corner cases and ensure selinux context is saved if possible
make a date-marked backup of the specified file, return True or False on success or failure
Convert the argument to a boolean
Return hex digest of local file for a digest_method specified by name, or None if file is not present.
return from the module, without error
return from the module, with an error message
Takes a path and returns it’s mount point
path – a string type with a filesystem path
the path to the mount point as a text type
Find system executable in PATH.
True
, fail_jsonPATH
if found return full path; otherwise return None
is the given path executable?
path – The path of the file to check.
Limitations:
Returns a tuple containing (True, selinux_context) if the given path is on a NFS or other ‘special’ fs mount point, otherwise the return will be (False, None).
many modules deal with files, this encapsulates common options that the file module accepts such that it is directly available to all modules and they can share code.
Allows to overwrite the path/dest module argument by providing path.
Return MD5 hex digest of local file using digest_from_file().
This function will not work on systems complying with FIPS-140-2.
Most uses of this function can use the module.sha1 function instead.
Copy a file with preserved ownership, permissions and context
Execute a command, returns rc, stdout, and stderr.
args – is the command to run * If args is a list, the command will be run with shell=False. * If args is a string and use_unsafe_shell=False it will split args to a list and run with shell=False * If args is a string and use_unsafe_shell=True it runs with shell=True.
Whether to call fail_json in case of non zero RC. Default False
See documentation for subprocess.Popen(). Default True
See documentation for subprocess.Popen(). Default None
If given, information to write to the stdin of the command
If False, append a newline to the data. Default False
If given, additional path to find the command in. This adds to the PATH environment variable so helper commands in the same directory can also be found
If given, working directory to run the command inside
See args
parameter. Default False
Regex string (not a compiled regex) which can be used to detect prompts in the stdout which would otherwise cause the execution to hang (especially if no input data is specified)
dictionary to update os.environ with
Umask to be used when running the command. Default None
Since we return native strings, on python3 we need to know the encoding to use to transform from bytes to text. If you want to always get bytes back, use encoding=None. The default is “utf-8”. This does not affect transformation of strings given as args.
Since we return native strings, on python3 we need to transform stdout and stderr from bytes to text. If the bytes are undecodable in the encoding
specified, then use this error handler to deal with them. The default is surrogate_or_strict
which means that the bytes will be decoded using the surrogateescape error handler if available (available on all python3 versions we support) otherwise a UnicodeError traceback will be raised. This does not affect transformations of strings given as args.
When use_unsafe_shell=False
this argument dictates whether ~
is expanded in paths and environment variables are expanded before running the command. When True
a string such as $SHELL
will be expanded regardless of escaping. When False
and use_unsafe_shell=False
no path or variable expansion will be done.
When running on Python 3 this argument dictates which file descriptors should be passed to an underlying Popen
constructor. On Python 2, this will set close_fds
to False.
This function will be called after Popen
object will be created but before communicating to the process. (Popen
object will be passed to callback as a first argument)
This flag indicates whether an invalid cwd
(non-existent or not a directory) should be ignored or should raise an exception.
A 3-tuple of return code (integer), stdout (native string), and stderr (native string). On python2, stdout and stderr are both byte strings. On python3, stdout and stderr are text strings converted according to the encoding and errors parameters. If you want byte strings on python3, use encoding=None to turn decoding to text off.
Return SHA1 hex digest of local file using digest_from_file().
Return SHA-256 hex digest of local file using digest_from_file().
To use this functionality, include import ansible.module_utils.basic
in your module.
ansible.module_utils.basic.get_all_subclasses(cls)
Deprecated: Use ansible.module_utils.common._utils.get_all_subclasses instead
ansible.module_utils.basic.get_platform()
Deprecated Use platform.system()
directly.
Name of the platform the module is running on in a native string
Returns a native string that labels the platform (“Linux”, “Solaris”, etc). Currently, this is the result of calling platform.system()
.
ansible.module_utils.basic.heuristic_log_sanitize(data, no_log_values=None)
Remove strings that look like passwords from log messages
ansible.module_utils.basic.load_platform_subclass(cls, *args, **kwargs)
Deprecated: Use ansible.module_utils.common.sys_info.get_platform_subclass instead
Classes and functions for validating parameters against an argument spec.
class ansible.module_utils.common.arg_spec.ArgumentSpecValidator(argument_spec, mutually_exclusive=None, required_together=None, required_one_of=None, required_if=None, required_by=None)
Argument spec validation class
Creates a validator based on the argument_spec
that can be used to validate a number of parameters using the validate()
method.
[parameter, value, [parameters]]
where one of [parameters]
is required if parameter == value
.validate(parameters, *args, **kwargs)
Validate parameters
against argument spec.
Error messages in the ValidationResult
may contain no_log values and should be sanitized with sanitize_keys()
before logging or displaying.
parameters (dict[str, dict]) – Parameters to validate against the argument spec
ValidationResult
containing validated parameters.
argument_spec = { 'name': {'type': 'str'}, 'age': {'type': 'int'}, } parameters = { 'name': 'bo', 'age': '42', } validator = ArgumentSpecValidator(argument_spec) result = validator.validate(parameters) if result.error_messages: sys.exit("Validation failed: {0}".format(", ".join(result.error_messages)) valid_params = result.validated_parameters
class ansible.module_utils.common.arg_spec.ValidationResult(parameters)
Result of argument spec validation.
This is the object returned by ArgumentSpecValidator.validate()
containing the validated parameters and any errors.
parameters (dict) – Terms to be validated and coerced to the correct type.
errors
AnsibleValidationErrorMultiple
containing all AnsibleValidationError
objects if there were any failures during validation.
property validated_parameters
Validated and coerced parameters.
property unsupported_parameters
set
of unsupported parameter names.
ansible.module_utils.common.parameters.DEFAULT_TYPE_VALIDATORS
dict
of type names, such as 'str'
, and the default function used to check that type, check_type_str()
in this case.
ansible.module_utils.common.parameters.env_fallback(*args, **kwargs)
Load value from environment variable
ansible.module_utils.common.parameters.remove_values(value, no_log_strings)
Remove strings in no_log_strings
from value.
If value is a container type, then remove a lot more.
Use of deferred_removals
exists, rather than a pure recursive solution, because of the potential to hit the maximum recursion depth when dealing with large amounts of data (see issue #24560).
ansible.module_utils.common.parameters.sanitize_keys(obj, no_log_strings, ignore_keys=frozenset({}))
Sanitize the keys in a container object by removing no_log
values from key names.
This is a companion function to the remove_values()
function. Similar to that function, we make use of deferred_removals
to avoid hitting maximum recursion depth in cases of large data structures.
An object with sanitized keys.
Standalone functions for validating various parameter types.
ansible.module_utils.common.validation.check_missing_parameters(parameters, required_parameters=None)
This is for checking for required params when we can not check via argspec because we need more information than is simply given in the argspec.
Raises TypeError
if any required parameters are missing
Empty list or raises TypeError
if the check fails.
ansible.module_utils.common.validation.check_mutually_exclusive(terms, parameters, options_context=None)
Check mutually exclusive terms against argument parameters
Accepts a single list or list of lists that are groups of terms that should be mutually exclusive with one another
Empty list or raises TypeError
if the check fails.
ansible.module_utils.common.validation.check_required_arguments(argument_spec, parameters, options_context=None)
Check all parameters in argument_spec and return a list of parameters that are required but not present in parameters.
Raises TypeError
if the check fails
Empty list or raises TypeError
if the check fails.
ansible.module_utils.common.validation.check_required_by(requirements, parameters, options_context=None)
For each key in requirements, check the corresponding list to see if they exist in parameters.
Accepts a single string or list of values for each key.
Empty dictionary or raises TypeError
if the
ansible.module_utils.common.validation.check_required_if(requirements, parameters, options_context=None)
Check parameters that are conditionally required
Raises TypeError
if the check fails
requirements – List of lists specifying a parameter, value, parameters required when the given parameter is the specified value, and optionally a boolean indicating any or all parameters are required.
required_if=[ ['state', 'present', ('path',), True], ['someint', 99, ('bool_param', 'string_param')], ]
parameters – Dictionary of parameters
Empty list or raises TypeError
if the check fails. The results attribute of the exception contains a list of dictionaries. Each dictionary is the result of evaluating each item in requirements. Each return dictionary contains the following keys:
List of parameters that are required but missing
’any’ or ‘all’
Parameter name that has the requirement
Original value of the parameter
Original required parameters
[ { 'parameter': 'someint', 'value': 99 'requirements': ('bool_param', 'string_param'), 'missing': ['string_param'], 'requires': 'all', } ]
ansible.module_utils.common.validation.check_required_one_of(terms, parameters, options_context=None)
Check each list of terms to ensure at least one exists in the given module parameters
Accepts a list of lists or tuples
terms
are in a sub spec.Empty list or raises TypeError
if the check fails.
ansible.module_utils.common.validation.check_required_together(terms, parameters, options_context=None)
Check each list of terms to ensure every parameter in each list exists in the given parameters.
Accepts a list of lists or tuples.
Empty list or raises TypeError
if the check fails.
ansible.module_utils.common.validation.check_type_bits(value)
Convert a human-readable string bits value to bits in integer.
Example: check_type_bits('1Mb')
returns integer 1048576.
Raises TypeError
if unable to covert the value.
ansible.module_utils.common.validation.check_type_bool(value)
Verify that the value is a bool or convert it to a bool and return it.
Raises TypeError
if unable to convert to a bool
value – String, int, or float to convert to bool. Valid booleans include: ‘1’, ‘on’, 1, ‘0’, 0, ‘n’, ‘f’, ‘false’, ‘true’, ‘y’, ‘t’, ‘yes’, ‘no’, ‘off’
Boolean True or False
ansible.module_utils.common.validation.check_type_bytes(value)
Convert a human-readable string value to bytes
Raises TypeError
if unable to covert the value
ansible.module_utils.common.validation.check_type_dict(value)
Verify that value is a dict or convert it to a dict and return it.
Raises TypeError
if unable to convert to a dict
value – Dict or string to convert to a dict. Accepts k1=v2, k2=v2
.
value converted to a dictionary
ansible.module_utils.common.validation.check_type_float(value)
Verify that value is a float or convert it to a float and return it
Raises TypeError
if unable to convert to a float
value – float, int, str, or bytes to verify or convert and return.
float of given value.
ansible.module_utils.common.validation.check_type_int(value)
Verify that the value is an integer and return it or convert the value to an integer and return it
Raises TypeError
if unable to convert to an int
value – String or int to convert of verify
int of given value
ansible.module_utils.common.validation.check_type_jsonarg(value)
Return a jsonified string. Sometimes the controller turns a json string into a dict/list so transform it back into json here
Raises TypeError
if unable to covert the value
ansible.module_utils.common.validation.check_type_list(value)
Verify that the value is a list or convert to a list
A comma separated string will be split into a list. Raises a TypeError
if unable to convert to a list.
value – Value to validate or convert to a list
Original value if it is already a list, single item list if a float, int, or string without commas, or a multi-item list if a comma-delimited string.
ansible.module_utils.common.validation.check_type_path(value)
Verify the provided value is a string or convert it to a string, then return the expanded path
ansible.module_utils.common.validation.check_type_raw(value)
Returns the raw value
ansible.module_utils.common.validation.check_type_str(value, allow_conversion=True, param=None, prefix='')
Verify that the value is a string or convert to a string.
Since unexpected changes can sometimes happen when converting to a string, allow_conversion
controls whether or not the value will be converted or a TypeError will be raised if the value is not a string and would be converted
Original value if it is a string, the value converted to a string if allow_conversion=True, or raises a TypeError if allow_conversion=False.
ansible.module_utils.common.validation.count_terms(terms, parameters)
Count the number of occurrences of a key in a given dictionary
An integer that is the number of occurrences of the terms values in the provided dictionary.
exception ansible.module_utils.errors.AnsibleFallbackNotFound
Fallback validator was not found
exception ansible.module_utils.errors.AnsibleValidationError(message)
Single argument spec validation error
error_message
The error message passed in when the exception was raised.
property msg
The error message passed in when the exception was raised.
exception ansible.module_utils.errors.AnsibleValidationErrorMultiple(errors=None)
Multiple argument spec validation errors
errors
list
of AnsibleValidationError
objects
property msg
The first message from the first error in errors
.
property messages
list
of each error message in errors
.
append(error)
Append a new error to self.errors
.
Only AnsibleValidationError
should be added.
extend(errors)
Append each item in errors
to self.errors
. Only AnsibleValidationError
should be added.
exception ansible.module_utils.errors.AliasError(message)
Error handling aliases
exception ansible.module_utils.errors.ArgumentTypeError(message)
Error with parameter type
exception ansible.module_utils.errors.ArgumentValueError(message)
Error with parameter value
exception ansible.module_utils.errors.ElementError(message)
Error when validating elements
exception ansible.module_utils.errors.MutuallyExclusiveError(message)
Mutually exclusive parameters were supplied
exception ansible.module_utils.errors.NoLogError(message)
Error converting no_log values
exception ansible.module_utils.errors.RequiredByError(message)
Error with parameters that are required by other parameters
exception ansible.module_utils.errors.RequiredDefaultError(message)
A required parameter was assigned a default value
exception ansible.module_utils.errors.RequiredError(message)
Missing a required parameter
exception ansible.module_utils.errors.RequiredIfError(message)
Error with conditionally required parameters
exception ansible.module_utils.errors.RequiredOneOfError(message)
Error with parameters where at least one is required
exception ansible.module_utils.errors.RequiredTogetherError(message)
Error with parameters that are required together
exception ansible.module_utils.errors.SubParameterTypeError(message)
Incorrect type for subparameter
exception ansible.module_utils.errors.UnsupportedError(message)
Unsupported parameters were supplied
© 2012–2018 Michael DeHaan
© 2018–2021 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/latest/reference_appendices/module_utils.html