From f37ac1dcc9e632785b7141aef34a3f21723682e9 Mon Sep 17 00:00:00 2001 From: beeankha Date: Fri, 20 Dec 2019 13:56:12 -0500 Subject: [PATCH] Add extra_vars example to Job Launch module, update extra_vars type to dict, update unit test, add details to Collections release notes. --- awx_collection/README.md | 1 + .../plugins/modules/tower_job_launch.py | 35 ++++++++++++++++--- awx_collection/test/awx/test_job_template.py | 8 ++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/awx_collection/README.md b/awx_collection/README.md index e817ea5ecd..64705907b0 100644 --- a/awx_collection/README.md +++ b/awx_collection/README.md @@ -20,6 +20,7 @@ The following notes are changes that may require changes to playbooks. `tower_credential_type` module is no longer supported. Provide as dictionaries instead. - When a project is created, it will wait for the update/sync to finish by default; this can be turned off with the `wait` parameter, if desired. - Creating a "scan" type job template is no longer supported. + - `extra_vars` in the `tower_job_launch` module worked with a list previously, but is now configured to work solely in a `dict` format. ## Running diff --git a/awx_collection/plugins/modules/tower_job_launch.py b/awx_collection/plugins/modules/tower_job_launch.py index 40fc193b63..65566cb316 100644 --- a/awx_collection/plugins/modules/tower_job_launch.py +++ b/awx_collection/plugins/modules/tower_job_launch.py @@ -43,8 +43,10 @@ options: type: str extra_vars: description: - - Extra_vars to use for the job_template. Prepend C(@) if a file. - type: list + - extra_vars to use for the Job Template. Prepend C(@) if a file. + - ask_extra_vars needs to be set to True via tower_job_template module + when creating the Job Template. + type: dict limit: description: - Limit to use for the I(job_template). @@ -68,6 +70,15 @@ EXAMPLES = ''' job_id: "{{ job.id }}" timeout: 120 +- name: Launch a job template with extra_vars on remote Tower instance + tower_job_launch: + job_template: "My Job Template" + extra_vars: + var1: "My First Variable" + var2: "My Second Variable" + var3: "My Third Variable" + job_type: run + # Launch job template with inventory and credential for prompt on launch - name: Launch a job with inventory and credential tower_job_launch: @@ -94,6 +105,7 @@ status: sample: pending ''' +import json from ..module_utils.ansible_tower import TowerModule, tower_auth_config, tower_check_mode @@ -106,6 +118,19 @@ except ImportError: pass +def update_fields(module, p): + params = p.copy() + + params_update = {} + extra_vars = params.get('extra_vars') + + if extra_vars: + params_update['extra_vars'] = [json.dumps(extra_vars)] + + params.update(params_update) + return params + + def main(): argument_spec = dict( job_template=dict(required=True, type='str'), @@ -114,11 +139,11 @@ def main(): credential=dict(type='str', default=None), limit=dict(), tags=dict(type='list'), - extra_vars=dict(type='list'), + extra_vars=dict(type='dict', required=False), ) module = TowerModule( - argument_spec, + argument_spec=argument_spec, supports_check_mode=True ) @@ -134,6 +159,8 @@ def main(): params['tags'] = ','.join(tags) job = tower_cli.get_resource('job') + params = update_fields(module, params) + lookup_fields = ('job_template', 'inventory', 'credential') for field in lookup_fields: try: diff --git a/awx_collection/test/awx/test_job_template.py b/awx_collection/test/awx/test_job_template.py index f6dc004772..c56fe47e2a 100644 --- a/awx_collection/test/awx/test_job_template.py +++ b/awx_collection/test/awx/test_job_template.py @@ -42,17 +42,23 @@ def test_job_launch_with_prompting(run_module, admin_user, project, inventory, m name='foo', project=project, playbook='helloworld.yml', + ask_variables_on_launch=True, ask_inventory_on_launch=True, ask_credential_on_launch=True ) result = run_module('tower_job_launch', dict( job_template='foo', inventory=inventory.name, - credential=machine_credential.name + credential=machine_credential.name, + extra_vars={"var1": "My First Variable", + "var2": "My Second Variable", + "var3": "My Third Variable" + } ), admin_user) assert result.pop('changed', None), result job = Job.objects.get(id=result['id']) + assert job.extra_vars == '{"var1": "My First Variable", "var2": "My Second Variable", "var3": "My Third Variable"}' assert job.inventory == inventory assert [cred.id for cred in job.credentials.all()] == [machine_credential.id]