Merge "Add option to remove group from inventory"
This commit is contained in:
commit
3ae81678e3
@ -43,6 +43,15 @@ Use the host's name as an argument.
|
|||||||
|
|
||||||
.. _`dynamic inventory functionality`: https://docs.ansible.com/ansible/intro_dynamic_inventory.html
|
.. _`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
|
Exporting host information
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@ -58,6 +58,14 @@ def args():
|
|||||||
action='append',
|
action='append',
|
||||||
default=[]
|
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(
|
exclusive_action.add_argument(
|
||||||
'-l',
|
'-l',
|
||||||
'--list-host',
|
'--list-host',
|
||||||
@ -310,6 +318,16 @@ def remove_ip_addresses(inventory, filepath=None):
|
|||||||
filesys.save_inventory(inventory_json, filepath)
|
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):
|
def remove_inventory_item(remove_item, inventory, filepath=None):
|
||||||
"""Removes inventory item from the inventory dictionary
|
"""Removes inventory item from the inventory dictionary
|
||||||
|
|
||||||
@ -350,6 +368,9 @@ def main():
|
|||||||
elif user_args['clear_ips'] is True:
|
elif user_args['clear_ips'] is True:
|
||||||
remove_ip_addresses(inventory, filepath)
|
remove_ip_addresses(inventory, filepath)
|
||||||
print('Success. . .')
|
print('Success. . .')
|
||||||
|
elif user_args['remove_group']:
|
||||||
|
remove_inventory_group(user_args['remove_group'], inventory, filepath)
|
||||||
|
print('Success. . .')
|
||||||
else:
|
else:
|
||||||
remove_inventory_item(user_args['remove_item'], inventory, filepath)
|
remove_inventory_item(user_args['remove_item'], inventory, filepath)
|
||||||
print('Success. . .')
|
print('Success. . .')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user