ansible.inventory package

class ansible.inventory.Inventory(loader, variable_manager, host_list='/etc/ansible/hosts')[source]

Bases: object

Host inventory for ansible.

serialize()[source]
deserialize(data)[source]
parse_inventory(host_list)[source]
get_hosts(pattern='all', ignore_limits_and_restrictions=False)[source]

Takes a pattern or list of patterns and returns a list of matching inventory host names, taking into account any active restrictions or applied subsets

classmethod split_host_pattern(pattern)[source]

Takes a string containing host patterns separated by commas (or a list thereof) and returns a list of single patterns (which may not contain commas). Whitespace is ignored.

Also accepts ‘:’ as a separator for backwards compatibility, but it is not recommended due to the conflict with IPv6 addresses and host ranges.

Example: ‘a,b[1], c[2:3] , d’ -> [‘a’, ‘b[1]’, ‘c[2:3]’, ‘d’]

classmethod order_patterns(patterns)[source]
clear_pattern_cache()[source]

called exclusively by the add_host plugin to allow patterns to be recalculated

groups_for_host(host)[source]
get_groups()[source]
get_host(hostname)[source]
get_group(groupname)[source]
get_group_variables(groupname, update_cached=False, vault_password=None)[source]
get_vars(hostname, update_cached=False, vault_password=None)[source]
get_host_variables(hostname, update_cached=False, vault_password=None)[source]
add_group(group)[source]
list_hosts(pattern='all')[source]

return a list of hostnames for a pattern

list_groups()[source]
restrict_to_hosts(restriction)[source]

Restrict list operations to the hosts given in restriction. This is used to batch serial operations in main playbook code, don’t use this for other reasons.

subset(subset_pattern)[source]

Limits inventory results to a subset of inventory that matches a given pattern, such as to select a given geographic of numeric slice amongst a previous ‘hosts’ selection that only select roles, or vice versa. Corresponds to –limit parameter to ansible-playbook

remove_restriction()[source]

Do not restrict list operations

is_file()[source]

Did inventory come from a file? We don’t use the equivalent loader methods in inventory, due to the fact that the loader does an implict DWIM on the path, which may be incorrect for inventory paths relative to the playbook basedir.

is_directory(path)[source]

Is the inventory host list a directory? Same caveat for here as with the is_file() method above.

basedir()[source]

if inventory came from a file, what’s the directory?

src()[source]

if inventory came from a file, what’s the directory and file name?

playbook_basedir()[source]

returns the directory of the current playbook

set_playbook_basedir(dir_name)[source]

Sets the base directory of the playbook so inventory can use it as basedir for host_vars and group_vars and more.

Parameters:dir_name (str) – A path to the directory to use as basedir.

Note

This invalidates self._vars_per_host.

get_host_vars(host, new_pb_basedir=False)[source]

Read host_vars/ files

get_group_vars(group, new_pb_basedir=False)[source]

Read group_vars/ files

refresh_inventory()[source]

Submodules

ansible.inventory.dir module

ansible.inventory.dir.get_file_parser(hostsfile, groups, loader)[source]

ansible.inventory.expand_hosts module

This module is for enhancing ansible’s inventory parsing capability such that it can deal with hostnames specified using a simple pattern in the form of [beg:end], example: [1:5], [a:c], [D:G]. If beg is not specified, it defaults to 0.

If beg is given and is left-zero-padded, e.g. ‘001’, it is taken as a formatting hint when the range is expanded. e.g. [001:010] is to be expanded into 001, 002 ...009, 010.

Note that when beg is specified with left zero padding, then the length of end must be the same as that of beg, else an exception is raised.

ansible.inventory.expand_hosts.detect_range(line=None)[source]

A helper function that checks a given host line to see if it contains a range pattern described in the docstring above.

Returnes True if the given line contains a pattern, else False.

ansible.inventory.expand_hosts.expand_hostname_range(line=None)[source]

A helper function that expands a given line that contains a pattern specified in top docstring, and returns a list that consists of the expanded version.

The ‘[‘ and ‘]’ characters are used to maintain the pseudo-code appearance. They are replaced in this function with ‘|’ to ease string splitting.

References: http://ansible.github.com/patterns.html#hosts-and-groups

ansible.inventory.group module

class ansible.inventory.group.Group(name=None)[source]

Bases: object

a group of ansible hosts

serialize()[source]
deserialize(data)[source]
get_name()[source]
add_child_group(group)[source]
add_host(host)[source]
set_variable(key, value)[source]
clear_hosts_cache()[source]
get_hosts()[source]
get_vars()[source]
get_ancestors()[source]
set_priority(priority)[source]

ansible.inventory.host module

class ansible.inventory.host.Host(name=None, port=None)[source]

Bases: object

a single ansible host

serialize()[source]
deserialize(data)[source]
get_name()[source]
gathered_facts
set_gathered_facts(gathered)[source]
add_group(group)[source]
set_variable(key, value)[source]
get_groups()[source]
get_vars()[source]
get_group_vars()[source]

ansible.inventory.ini module

class ansible.inventory.ini.InventoryParser(loader, groups, filename='/etc/ansible/hosts')[source]

Bases: object

Takes an INI-format inventory file and builds a list of groups and subgroups with their associated hosts and variable settings.

get_host_variables(host)[source]

ansible.inventory.script module

class ansible.inventory.script.InventoryScript(loader, groups=None, filename='/etc/ansible/hosts')[source]

Bases: object

Host inventory parser for ansible using external inventory scripts.

get_host_variables(host)[source]

Runs <script> –host <hostname> to determine additional host variables

ansible.inventory.yaml module

class ansible.inventory.yaml.InventoryParser(loader, groups, filename='/etc/ansible/hosts')[source]

Bases: object

Takes an INI-format inventory file and builds a list of groups and subgroups with their associated hosts and variable settings.

get_host_variables(host)[source]