ansible.parsing package

Submodules

ansible.parsing.dataloader module

class ansible.parsing.dataloader.DataLoader[source]

Bases: object

The DataLoader class is used to load and parse YAML or JSON content, either from a given file name or from a string that was previously read in through other means. A Vault password can be specified, and any vault-encrypted files will be decrypted.

Data read from files will also be cached, so the file will never be read from disk more than once.

Usage:

dl = DataLoader() # optionally: dl.set_vault_password(‘foo’) ds = dl.load(‘...’) ds = dl.load_from_file(‘/path/to/file’)
set_vault_password(vault_password)[source]
load(data, file_name='<string>', show_content=True)[source]

Creates a python datastructure from the given data, which can be either a JSON or YAML string.

load_from_file(file_name)[source]

Loads data from a file, which can contain either JSON or YAML.

path_exists(path)[source]
is_file(path)[source]
is_directory(path)[source]
list_directory(path)[source]
is_executable(path)[source]

is the given path executable?

get_basedir()[source]

returns the current basedir

set_basedir(basedir)[source]

sets the base directory, used to find files when a relative path is given

path_dwim(given)[source]

make relative paths work like folks expect.

path_dwim_relative(path, dirname, source)[source]

find one file in either a role or playbook dir with or without explicitly named dirname subdirs

Used in action plugins and lookups to find supplemental files that could be in either place.

read_vault_password_file(vault_password_file)[source]

Read a vault password from a file or if executable, execute the script and retrieve password from STDOUT

get_real_file(file_path)[source]

If the file is vault encrypted return a path to a temporary decrypted file If the file is not encrypted then the path is returned Temporary files are cleanup in the destructor

cleanup_tmp_file(file_path)[source]

Removes any temporary files created from a previous call to get_real_file. file_path must be the path returned from a previous call to get_real_file.

cleanup_all_tmp_files()[source]

ansible.parsing.mod_args module

class ansible.parsing.mod_args.ModuleArgsParser(task_ds={})[source]

Bases: object

Base module argument parsing class.

There are several ways a module and argument set can be expressed:

# legacy form (for a shell command)
- action: shell echo hi

# common shorthand for local actions vs delegate_to
- local_action: shell echo hi

# most commonly:
- copy: src=a dest=b

# legacy form
- action: copy src=a dest=b

# complex args form, for passing structured data
- copy:
    src: a
    dest: b

# gross, but technically legal
- action:
    module: copy
    args:
    src: a
    dest: b

# extra gross, but also legal. in this case, the args specified
# will act as 'defaults' and will be overridden by any args specified
# in one of the other formats (complex args under the action, or
# parsed from the k=v string
- command: 'pwd'
args:
    chdir: '/tmp'

This class has some of the logic to canonicalize these into the form:

- module: <module_name>
delegate_to: <optional>
args: <args>

Args may also be munged for certain shell command parameters.

parse()[source]

Given a task in one of the supported forms, parses and returns returns the action, arguments, and delegate_to values for the task, dealing with all sorts of levels of fuzziness.

ansible.parsing.quoting module

ansible.parsing.quoting.is_quoted(data)[source]
ansible.parsing.quoting.unquote(data)[source]

removes first and last quotes from a string, if the string starts and ends with the same quotes

ansible.parsing.splitter module

ansible.parsing.splitter.parse_kv(args, check_raw=False)[source]

Convert a string of key/value items to a dict. If any free-form params are found and the check_raw option is set to True, they will be added to a new parameter called ‘_raw_params’. If check_raw is not enabled, they will simply be ignored.

ansible.parsing.splitter.split_args(args)[source]

Splits args on whitespace, but intelligently reassembles those that may have been split over a jinja2 block or quotes.

When used in a remote module, we won’t ever have to be concerned about jinja2 blocks, however this function is/will be used in the core portions as well before the args are templated.

example input: a=b c=”foo bar” example output: [‘a=b’, ‘c=”foo bar”’]

Basically this is a variation shlex that has some more intelligence for how Ansible needs to use it.