Skip to content

Instantly share code, notes, and snippets.

@cbwest3
Created November 6, 2017 18:28
Show Gist options
  • Save cbwest3/cb4e9ac16261595a82f8b4ab54629bac to your computer and use it in GitHub Desktop.
Save cbwest3/cb4e9ac16261595a82f8b4ab54629bac to your computer and use it in GitHub Desktop.
Programmatic Ansible 2.4.1 inventory creation
def create_inventory(host_list, variable_manager, loader):
"""
Returns an inventory object for playbook execution.
Args:
host_list (list): list of IP addresses or hostnames
variable_manager (ansible.vars.manager.VariableManager): Ansible variable
manager for PlaybookExecutor
loader (ansible.parsing.dataloader.DataLoader): Ansible data loader for
PlaybookExecutor
Returns: ansible.inventory.manager.InventoryManager for PlaybookExecutor
"""
# create inventory and pass to var manager
data = InventoryData()
for host in host_list:
data.add_host(host)
data.reconcile_inventory()
inventory = InventoryManager(loader=loader)
inventory._inventory = data
variable_manager.set_inventory(inventory)
return inventory
@nemmeviu
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment