diff --git a/doc/source/reference/inventory/manage-inventory.rst b/doc/source/reference/inventory/manage-inventory.rst index eeb418dcfe..8442d80bc3 100644 --- a/doc/source/reference/inventory/manage-inventory.rst +++ b/doc/source/reference/inventory/manage-inventory.rst @@ -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 ~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/osa_toolkit/manage.py b/osa_toolkit/manage.py index d803e4c087..3192730f3d 100644 --- a/osa_toolkit/manage.py +++ b/osa_toolkit/manage.py @@ -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. . .')