Implement multiple inventory for ansible.targets

This commit is contained in:
vzhestkov 2025-02-26 09:42:53 +01:00 committed by Daniel Wozniak
parent 0ad9b3beee
commit b51e75e540
2 changed files with 52 additions and 19 deletions

View file

@ -420,7 +420,7 @@ def playbooks(
return retdata
def targets(inventory="/etc/ansible/hosts", yaml=False, export=False):
def targets(inventory=None, inventories=None, yaml=False, export=False):
"""
.. versionadded:: 3005
@ -429,6 +429,10 @@ def targets(inventory="/etc/ansible/hosts", yaml=False, export=False):
:param inventory:
The inventory file to read the inventory from. Default: "/etc/ansible/hosts"
:param inventories:
The list of inventory files to read the inventory from.
Uses `inventory` in case if `inventories` is not specified.
:param yaml:
Return the inventory as yaml output. Default: False
@ -443,7 +447,9 @@ def targets(inventory="/etc/ansible/hosts", yaml=False, export=False):
salt 'ansiblehost' ansible.targets inventory=my_custom_inventory
"""
return salt.utils.ansible.targets(inventory=inventory, yaml=yaml, export=export)
return salt.utils.ansible.targets(
inventory=inventory, inventories=inventories, yaml=yaml, export=export
)
def discover_playbooks(

View file

@ -18,30 +18,57 @@ def __virtual__():
return (False, "Install `ansible` to use inventory")
def targets(inventory="/etc/ansible/hosts", yaml=False, export=False):
def targets(inventory=None, inventories=None, yaml=False, export=False):
"""
Return the targets from the ansible inventory_file
Default: /etc/salt/roster
"""
if not os.path.isfile(inventory):
raise CommandExecutionError(f"Inventory file not found: {inventory}")
if not os.path.isabs(inventory):
raise CommandExecutionError("Path to inventory file must be an absolute path")
if inventory is None and inventories is None:
inventory = "/etc/ansible/hosts"
multi_inventory = True
if not isinstance(inventories, list):
multi_inventory = False
inventories = []
if inventory is not None and inventory not in inventories:
inventories.append(inventory)
extra_cmd = []
if export:
extra_cmd.append("--export")
if yaml:
extra_cmd.append("--yaml")
inv = salt.modules.cmdmod.run(
"ansible-inventory -i {} --list {}".format(inventory, " ".join(extra_cmd)),
env={"ANSIBLE_DEPRECATION_WARNINGS": "0"},
reset_system_locale=False,
)
if yaml:
return salt.utils.stringutils.to_str(inv)
else:
try:
return salt.utils.json.loads(salt.utils.stringutils.to_str(inv))
except ValueError:
raise CommandExecutionError(f"Error processing the inventory: {inv}")
ret = {}
for inventory in inventories:
if not os.path.isfile(inventory):
raise CommandExecutionError(f"Inventory file not found: {inventory}")
if not os.path.isabs(inventory):
raise CommandExecutionError(
f"Path to inventory file must be an absolute path: {inventory}"
)
inv = salt.modules.cmdmod.run(
"ansible-inventory -i {} --list {}".format(inventory, " ".join(extra_cmd)),
env={"ANSIBLE_DEPRECATION_WARNINGS": "0"},
reset_system_locale=False,
)
if yaml:
inv = salt.utils.stringutils.to_str(inv)
else:
try:
inv = salt.utils.json.loads(salt.utils.stringutils.to_str(inv))
except ValueError:
raise CommandExecutionError(
f"Error processing the inventory {inventory}: {inv}"
)
if not multi_inventory:
ret = inv
break
ret[inventory] = inv
return ret