Merge "Add option to remove group from inventory"

This commit is contained in:
Zuul 2021-05-17 12:28:06 +00:00 committed by Gerrit Code Review
commit 3ae81678e3
2 changed files with 30 additions and 0 deletions

View File

@ -43,6 +43,15 @@ Use the host's name as an argument.
.. _`dynamic inventory functionality`: https://docs.ansible.com/ansible/intro_dynamic_inventory.html
Removing a group
~~~~~~~~~~~~~~~~
A host group can be removed with the ``--remove-group/-d`` parameter.
Use the groups's name as an argument. You can repeat argument multiple times
to remove several groups at once.
Exporting host information
~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -58,6 +58,14 @@ def args():
action='append',
default=[]
)
exclusive_action.add_argument(
'-d',
'--remove-group',
help='group name to remove from inventory, this can be used multiple'
' times.',
action='append',
default=[]
)
exclusive_action.add_argument(
'-l',
'--list-host',
@ -310,6 +318,16 @@ def remove_ip_addresses(inventory, filepath=None):
filesys.save_inventory(inventory_json, filepath)
def remove_inventory_group(remove_group, inventory, filepath=None):
for group in remove_group:
inventory.pop(group, None)
if filepath is not None:
inventory_json = json.dumps(inventory, indent=2,
separators=(',', ': '))
filesys.save_inventory(inventory_json, filepath)
def remove_inventory_item(remove_item, inventory, filepath=None):
"""Removes inventory item from the inventory dictionary
@ -350,6 +368,9 @@ def main():
elif user_args['clear_ips'] is True:
remove_ip_addresses(inventory, filepath)
print('Success. . .')
elif user_args['remove_group']:
remove_inventory_group(user_args['remove_group'], inventory, filepath)
print('Success. . .')
else:
remove_inventory_item(user_args['remove_item'], inventory, filepath)
print('Success. . .')