From 792f68eaec83bb9a302634b17de90377fe022c18 Mon Sep 17 00:00:00 2001 From: beeankha Date: Fri, 17 Jan 2020 11:40:41 -0500 Subject: [PATCH 1/2] When Job Template is not found, fail more gracefully --- awx_collection/plugins/modules/tower_job_launch.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/awx_collection/plugins/modules/tower_job_launch.py b/awx_collection/plugins/modules/tower_job_launch.py index 7b00cd2907..303de65407 100644 --- a/awx_collection/plugins/modules/tower_job_launch.py +++ b/awx_collection/plugins/modules/tower_job_launch.py @@ -127,8 +127,8 @@ def update_fields(module, p): try: ask_extra_vars = tower_cli.get_resource('job_template').get(name=job_template)['ask_variables_on_launch'] survey_enabled = tower_cli.get_resource('job_template').get(name=job_template)['survey_enabled'] - except (exc.ConnectionError, exc.BadRequest, exc.AuthError) as excinfo: - module.fail_json(msg='Failed to get ask_extra_vars parameter, job template not found: {0}'.format(excinfo), changed=False) + except (exc.NotFound) as excinfo: + module.fail_json(msg='Unable to launch job, job_template/{0} was not found: {1}'.format(job_template, excinfo), changed=False) if extra_vars and (ask_extra_vars or survey_enabled): params_update['extra_vars'] = [json.dumps(extra_vars)] From ef36b4fffde3946397726099809736d193a82a72 Mon Sep 17 00:00:00 2001 From: beeankha Date: Fri, 17 Jan 2020 15:08:05 -0500 Subject: [PATCH 2/2] Reduce number of requests running in the try/except block --- awx_collection/plugins/modules/tower_job_launch.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/awx_collection/plugins/modules/tower_job_launch.py b/awx_collection/plugins/modules/tower_job_launch.py index 303de65407..7a85e3c82c 100644 --- a/awx_collection/plugins/modules/tower_job_launch.py +++ b/awx_collection/plugins/modules/tower_job_launch.py @@ -125,11 +125,13 @@ def update_fields(module, p): job_template = params.get('job_template') extra_vars = params.get('extra_vars') try: - ask_extra_vars = tower_cli.get_resource('job_template').get(name=job_template)['ask_variables_on_launch'] - survey_enabled = tower_cli.get_resource('job_template').get(name=job_template)['survey_enabled'] + job_template_to_launch = tower_cli.get_resource('job_template').get(name=job_template) except (exc.NotFound) as excinfo: module.fail_json(msg='Unable to launch job, job_template/{0} was not found: {1}'.format(job_template, excinfo), changed=False) + ask_extra_vars = job_template_to_launch['ask_variables_on_launch'] + survey_enabled = job_template_to_launch['survey_enabled'] + if extra_vars and (ask_extra_vars or survey_enabled): params_update['extra_vars'] = [json.dumps(extra_vars)]