diff --git a/awx_collection/plugins/modules/tower_job_template.py b/awx_collection/plugins/modules/tower_job_template.py index b1e143e3bd..1f1d776f28 100644 --- a/awx_collection/plugins/modules/tower_job_template.py +++ b/awx_collection/plugins/modules/tower_job_template.py @@ -44,6 +44,14 @@ options: description: - Name of the inventory to use for the job template. type: str + organization: + description: + - Organization the job template exists in. + - Used to help lookup the object, cannot be modified using this module. + - The Organization is inferred from the associated project + - If not provided, will lookup by name only, which does not work with duplicates. + - Requires Tower Version 3.7.0 or AWX 10.0.0 IS NOT backwards compatible with earlier versions. + type: str project: description: - Name of the project to use for the job template. @@ -282,6 +290,7 @@ EXAMPLES = ''' tower_job_template: name: "Ping" job_type: "run" + organization: "Default" inventory: "Local" project: "Demo" playbook: "ping.yml" @@ -332,6 +341,7 @@ def main(): name=dict(required=True), new_name=dict(), description=dict(default=''), + organization=dict(), job_type=dict(choices=['run', 'check']), inventory=dict(), project=dict(), @@ -398,19 +408,24 @@ def main(): credentials = [] credentials.append(credential) + new_fields = {} + search_fields = {'name': name} + + # Attempt to look up the related items the user specified (these will fail the module if not found) + organization_id = None + organization = module.params.get('organization') + if organization: + organization_id = module.resolve_name_to_id('organizations', organization) + search_fields['organization'] = new_fields['organization'] = organization_id + # Attempt to look up an existing item based on the provided data - existing_item = module.get_one('job_templates', **{ - 'data': { - 'name': name, - } - }) + existing_item = module.get_one('job_templates', **{'data': search_fields}) if state == 'absent': # 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(existing_item) # Create the data that gets sent for create and update - new_fields = {} new_fields['name'] = new_name if new_name else name for field_name in ( 'description', 'job_type', 'playbook', 'scm_branch', 'forks', 'limit', 'verbosity', @@ -437,7 +452,20 @@ def main(): if inventory is not None: new_fields['inventory'] = module.resolve_name_to_id('inventories', inventory) if project is not None: - new_fields['project'] = module.resolve_name_to_id('projects', project) + if organization_id is not None: + project_data = module.get_one('projects', **{ + 'data': { + 'name': project, + 'organization': organization_id, + } + }) + if project_data is None: + module.fail_json(msg="The project {0} in organization {1} was not found on the Tower server".format( + project, organization + )) + new_fields['project'] = project_data['id'] + else: + new_fields['project'] = module.resolve_name_to_id('projects', project) if webhook_credential is not None: new_fields['webhook_credential'] = module.resolve_name_to_id('credentials', webhook_credential)