Add option to remove group from inventory

Added `-d` option to inventory-manage.py that will remove provided
group from the inventory. It won't touch conf.d so you need to remove
entry from there before re-running dynamic_inventory

Depends-On: https://review.opendev.org/c/openstack/openstack-ansible-galera_server/+/791107
Change-Id: I665ae5ce999c36d8d999ed5f9d659095a900d9eb
Closes-Bug: #1642051
This commit is contained in:
Dmitriy Rabotyagov 2021-05-13 18:50:07 +03:00 committed by Dmitriy Rabotyagov
parent 9a5bef9a8c
commit 13fcd09b0b
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. . .')