Moar constructive (#28254)

* made composite vars and groups generic

now you can do both in every plugin that chooses to suport it
renamed constructed_groups as it now also constructs vars ... to constructed
moved most of constructed_groups logic into base class to easily share

* documented inventory_hostname

* typo fix
This commit is contained in:
Brian Coca 2017-08-21 16:06:15 -04:00 committed by GitHub
parent ec11cd2696
commit a897193bce
5 changed files with 60 additions and 26 deletions

12
lib/ansible/plugins/inventory/virtualbox.py Executable file → Normal file
View file

@ -24,6 +24,7 @@ DOCUMENTATION:
description:
- Get inventory hosts from the local virtualbox installation.
- Uses a <name>.vbox.yaml (or .vbox.yml) YAML configuration file.
- The inventory_hostname is always the 'Name' of the virtualbox instance.
options:
running_only:
description: toggles showing all vms vs only those currently running
@ -42,6 +43,10 @@ DOCUMENTATION:
description: create vars from jinja2 expressions, these are created AFTER the query block
type: dictionary
default: {}
groups:
description: add hosts to group based on Jinja2 conditionals, these also run after query block
type: dictionary
default: {}
EXAMPLES:
# file must be named vbox.yaml or vbox.yml
simple_config_file:
@ -94,14 +99,15 @@ class InventoryModule(BaseInventoryPlugin):
hostvars[host][varname] = self._query_vbox_data(host, data['query'][varname])
# create composite vars
if data.get('compose') and isinstance(data['compose'], dict):
for varname in data['compose']:
hostvars[host][varname] = self._compose(data['compose'][varname], hostvars[host])
self._set_composite_vars(data.get('compose'), hostvars, host)
# actually update inventory
for key in hostvars[host]:
self.inventory.set_variable(host, key, hostvars[host][key])
# constructed groups based on conditionals
self._add_host_to_composed_groups(data.get('groups'), hostvars, host)
def _populate_from_source(self, source_data, config_data):
hostvars = {}
prevkey = pref_k = ''