add copy to modules

This commit is contained in:
sean-m-sullivan
2021-02-21 19:49:14 -06:00
parent 74a5247d9d
commit 9a7dd38cbb
15 changed files with 536 additions and 28 deletions

View File

@@ -27,6 +27,14 @@ options:
- The name to use for the inventory.
required: True
type: str
copy_from:
description:
- Name or id to copy the inventory from.
- This will copy an existing inventory and change any parameters supplied.
- The new inventory name will be the one provided in the name parameter.
- The organization parameter is not used in this, to facilitate copy from one organization to another.
- Provide the id or use the lookup plugin to provide the id if multiple inventories share the same name.
type: str
description:
description:
- The description to use for the inventory.
@@ -72,6 +80,14 @@ EXAMPLES = '''
organization: "Bar Org"
state: present
tower_config_file: "~/tower_cli.cfg"
- name: Copy tower inventory
tower_inventory:
name: Copy Foo Inventory
copy_from: Default Inventory
description: "Our Foo Cloud Servers"
organization: Foo
state: present
'''
@@ -83,6 +99,7 @@ def main():
# Any additional arguments that are not fields of the item can be added here
argument_spec = dict(
name=dict(required=True),
copy_from=dict(),
description=dict(),
organization=dict(required=True),
variables=dict(type='dict'),
@@ -97,6 +114,7 @@ def main():
# Extract our parameters
name = module.params.get('name')
copy_from = module.params.get('copy_from')
description = module.params.get('description')
organization = module.params.get('organization')
variables = module.params.get('variables')
@@ -108,6 +126,29 @@ def main():
# Attempt to look up the related items the user specified (these will fail the module if not found)
org_id = module.resolve_name_to_id('organizations', organization)
# Attempt to look up inventory to copy based on the provided name
if copy_from:
# Check if inventory exists, as API will allow you to create an identical item with the same name in same org, but GUI will not.
inventory = module.get_one('inventories', name_or_id=name, **{
'data': {
'organization': org_id
}
})
if inventory is not None:
module.fail_json(msg="A inventory with the name {0} already exists.".format(name))
else:
# Lookup existing inventory.
copy_from_lookup = module.get_one('inventories', name_or_id=copy_from)
if copy_from_lookup is None:
module.fail_json(msg="An inventory with the name {0} was not able to be found.".format(copy_from))
else:
# Because the initial copy will keep its organization, this can be different then the specified one.
org_id = copy_from_lookup['organization']
module.copy_item(
copy_from_lookup, name,
item_type='inventory'
)
# Attempt to look up inventory based on the provided name and org ID
inventory = module.get_one('inventories', name_or_id=name, **{
'data': {
@@ -119,6 +160,9 @@ def main():
# If the state was absent we can let the module delete it if needed, the module will handle exiting from this
module.delete_if_needed(inventory)
# Reset Org id to push in case copy_from was used.
org_id = module.resolve_name_to_id('organizations', organization)
# Create the data that gets sent for create and update
inventory_fields = {
'name': module.get_item_name(inventory) if inventory else name,