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

@@ -52,6 +52,18 @@ options:
elements: str
aliases:
- groups
preserve_existing_hosts:
description:
- Provide option (False by default) to preserves existing hosts in an existing group in tower.
default: False
type: bool
preserve_existing_children:
description:
- Provide option (False by default) to preserves existing children in an existing group in tower.
default: False
type: bool
aliases:
- preserve_existing_groups
state:
description:
- Desired state of the resource.
@@ -74,6 +86,18 @@ EXAMPLES = '''
inventory: "Local Inventory"
state: present
tower_config_file: "~/tower_cli.cfg"
- name: Add tower group
tower_group:
name: Cities
description: "Local Host Group"
inventory: Default Inventory
hosts:
- fda
children:
- NewYork
preserve_existing_hosts: True
preserve_existing_children: True
'''
from ..module_utils.tower_api import TowerAPIModule
@@ -90,6 +114,8 @@ def main():
variables=dict(type='dict'),
hosts=dict(type='list', elements='str'),
children=dict(type='list', elements='str', aliases=['groups']),
preserve_existing_hosts=dict(type='bool', default=False),
preserve_existing_children=dict(type='bool', default=False, aliases=['preserve_existing_groups']),
state=dict(choices=['present', 'absent'], default='present'),
)
@@ -102,6 +128,8 @@ def main():
inventory = module.params.get('inventory')
description = module.params.get('description')
state = module.params.pop('state')
preserve_existing_hosts = module.params.get('preserve_existing_hosts')
preserve_existing_children = module.params.get('preserve_existing_groups')
variables = module.params.get('variables')
# Attempt to look up the related items the user specified (these will fail the module if not found)
@@ -141,6 +169,11 @@ def main():
if sub_obj is None:
module.fail_json(msg='Could not find {0} with name {1}'.format(resource, sub_name))
id_list.append(sub_obj['id'])
# Preserve existing objects
if (preserve_existing_hosts and relationship == 'hosts') or (preserve_existing_children and relationship == 'children'):
preserve_existing_check = module.get_endpoint(group['related'][relationship])
for sub_obj in preserve_existing_check['json']['results']:
id_list.append(sub_obj['id'])
if id_list:
association_fields[relationship] = id_list