From 377fba3d766a57957f5b1f56f03e98ecddbb7844 Mon Sep 17 00:00:00 2001 From: Hideki Saito Date: Tue, 9 Apr 2019 22:08:11 +0900 Subject: [PATCH] Fix handling of inventory and credential options for tower_job_launch (#54967) - Fixed issue #25017,#37567 - Add example for prompt on launch - Add integration test for prompt on launch Signed-off-by: Hideki Saito --- .../fragments/tower_job_launch-options.yaml | 4 +++ .../ansible_tower/tower_job_launch.py | 24 ++++++++++--- .../targets/tower_job_launch/tasks/main.yml | 35 +++++++++++++++++++ test/runner/lib/cloud/tower.py | 2 ++ 4 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/tower_job_launch-options.yaml diff --git a/changelogs/fragments/tower_job_launch-options.yaml b/changelogs/fragments/tower_job_launch-options.yaml new file mode 100644 index 00000000000..bd95f1ae8e7 --- /dev/null +++ b/changelogs/fragments/tower_job_launch-options.yaml @@ -0,0 +1,4 @@ +--- +bugfixes: + - Fixed to handle arguments correctly even if inventory and credential + variables are not specified (#25017,#37567) diff --git a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_launch.py b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_launch.py index d05abba0975..87e9b2be22d 100644 --- a/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_launch.py +++ b/lib/ansible/modules/web_infrastructure/ansible_tower/tower_job_launch.py @@ -58,11 +58,24 @@ extends_documentation_fragment: tower ''' EXAMPLES = ''' +# Launch a job template - name: Launch a job tower_job_launch: job_template: "My Job Template" register: job +- name: Wait for job max 120s + tower_job_wait: + job_id: job.id + timeout: 120 + +# Launch job template with inventory and credential for prompt on launch +- name: Launch a job with inventory and credential + tower_job_launch: + job_template: "My Job Template" + inventory: "My Inventory" + credential: "My Credential" + register: job - name: Wait for job max 120s tower_job_wait: job_id: job.id @@ -96,10 +109,10 @@ except ImportError: def main(): argument_spec = dict( - job_template=dict(required=True), + job_template=dict(required=True, type='str'), job_type=dict(choices=['run', 'check', 'scan']), - inventory=dict(), - credential=dict(), + inventory=dict(type='str', default=None), + credential=dict(type='str', default=None), limit=dict(), tags=dict(type='list'), extra_vars=dict(type='list'), @@ -126,8 +139,9 @@ def main(): for field in lookup_fields: try: name = params.pop(field) - result = tower_cli.get_resource(field).get(name=name) - params[field] = result['id'] + if name: + result = tower_cli.get_resource(field).get(name=name) + params[field] = result['id'] except exc.NotFound as excinfo: module.fail_json(msg='Unable to launch job, {0}/{1} was not found: {2}'.format(field, name, excinfo), changed=False) diff --git a/test/integration/targets/tower_job_launch/tasks/main.yml b/test/integration/targets/tower_job_launch/tasks/main.yml index 9bf271c105d..7550377905b 100644 --- a/test/integration/targets/tower_job_launch/tasks/main.yml +++ b/test/integration/targets/tower_job_launch/tasks/main.yml @@ -10,6 +10,18 @@ - "result is changed" - "result.status == 'pending'" +- name: Wait for a job template to complete + tower_job_wait: + job_id: "{{ result.id }}" + max_interval: 10 + timeout: 120 + register: result + +- assert: + that: + - "result is not changed" + - "result.status == 'successful'" + - name: Check module fails with correct msg tower_job_launch: job_template: "Non Existing Job Template" @@ -21,3 +33,26 @@ - assert: that: - "result.msg =='Unable to launch job, job_template/Non Existing Job Template was not found: The requested object could not be found.'" + +- name: Create a Job Template for testing prompt on launch + tower_job_template: + name: "Demo Job Template - ask inventory and credential" + project: Demo Project + playbook: hello_world.yml + job_type: run + ask_credential: yes + ask_inventory: yes + state: present + register: result + +- name: Launch job template with inventory and credential for prompt on launch + tower_job_launch: + job_template: "Demo Job Template - ask inventory and credential" + inventory: "Demo Inventory" + credential: "Demo Credential" + register: result + +- assert: + that: + - "result is changed" + - "result.status == 'pending'" diff --git a/test/runner/lib/cloud/tower.py b/test/runner/lib/cloud/tower.py index 9843b022d0f..3d50bf23e85 100644 --- a/test/runner/lib/cloud/tower.py +++ b/test/runner/lib/cloud/tower.py @@ -69,6 +69,8 @@ class TowerCloudProvider(CloudProvider): tower_cli_version_map = { '3.1.5': '3.1.8', '3.2.3': '3.3.0', + '3.3.5': '3.3.3', + '3.4.3': '3.3.3', } cli_version = tower_cli_version_map.get(self.version, fallback)