From 44255f378d1b1aaa7fd34a39b19d914299cf79e9 Mon Sep 17 00:00:00 2001 From: jessicamack Date: Wed, 25 Oct 2023 10:38:45 -0400 Subject: [PATCH] Fix extra_vars bug in ansible.controller.ad_hoc_command (#14585) * convert to valid type for serializer * check that extra_vars are in request * remove doubled line * add integration test for change * move change to the ad_hoc_command module Signed-off-by: jessicamack * fix imports Signed-off-by: jessicamack --------- Signed-off-by: jessicamack --- awx_collection/plugins/modules/ad_hoc_command.py | 7 ++++++- .../targets/ad_hoc_command/tasks/main.yml | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/awx_collection/plugins/modules/ad_hoc_command.py b/awx_collection/plugins/modules/ad_hoc_command.py index d1e8cd63b4..5864d392a5 100644 --- a/awx_collection/plugins/modules/ad_hoc_command.py +++ b/awx_collection/plugins/modules/ad_hoc_command.py @@ -118,6 +118,7 @@ status: ''' from ..module_utils.controller_api import ControllerAPIModule +import json def main(): @@ -161,7 +162,11 @@ def main(): } for arg in ['job_type', 'limit', 'forks', 'verbosity', 'extra_vars', 'become_enabled', 'diff_mode']: if module.params.get(arg): - post_data[arg] = module.params.get(arg) + # extra_var can receive a dict or a string, if a dict covert it to a string + if arg == 'extra_vars' and type(module.params.get(arg)) is not str: + post_data[arg] = json.dumps(module.params.get(arg)) + else: + post_data[arg] = module.params.get(arg) # Attempt to look up the related items the user specified (these will fail the module if not found) post_data['inventory'] = module.resolve_name_to_id('inventories', inventory) diff --git a/awx_collection/tests/integration/targets/ad_hoc_command/tasks/main.yml b/awx_collection/tests/integration/targets/ad_hoc_command/tasks/main.yml index 3949d2631e..3180045e80 100644 --- a/awx_collection/tests/integration/targets/ad_hoc_command/tasks/main.yml +++ b/awx_collection/tests/integration/targets/ad_hoc_command/tasks/main.yml @@ -72,6 +72,21 @@ - "result is changed" - "result.status == 'successful'" +- name: Launch an Ad Hoc Command with extra_vars + ad_hoc_command: + inventory: "Demo Inventory" + credential: "{{ ssh_cred_name }}" + module_name: "ping" + extra_vars: + var1: "test var" + wait: true + register: result + +- assert: + that: + - "result is changed" + - "result.status == 'successful'" + - name: Launch an Ad Hoc Command with Execution Environment specified ad_hoc_command: inventory: "Demo Inventory"