From 66f14bfe8f2774e509e0914b8b61c483a50b877c Mon Sep 17 00:00:00 2001 From: Akira Yokochi Date: Fri, 23 Jun 2023 02:10:01 +0900 Subject: [PATCH] Using execution_environment option in ad_hoc_command module (#14105) --- .../plugins/modules/ad_hoc_command.py | 3 ++ .../targets/ad_hoc_command/tasks/main.yml | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/awx_collection/plugins/modules/ad_hoc_command.py b/awx_collection/plugins/modules/ad_hoc_command.py index bfe22eb890..3cdcd5aa47 100644 --- a/awx_collection/plugins/modules/ad_hoc_command.py +++ b/awx_collection/plugins/modules/ad_hoc_command.py @@ -145,6 +145,7 @@ def main(): wait = module.params.get('wait') interval = module.params.get('interval') timeout = module.params.get('timeout') + execution_environment = module.params.get('execution_environment') # Create a datastructure to pass into our command launch post_data = { @@ -158,6 +159,8 @@ def main(): # 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) post_data['credential'] = module.resolve_name_to_id('credentials', credential) + if execution_environment: + post_data['execution_environment'] = module.resolve_name_to_id('execution_environments', execution_environment) # Launch the ad hoc command results = module.post_endpoint('ad_hoc_commands', **{'data': post_data}) 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 316315df30..3949d2631e 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 @@ -9,6 +9,7 @@ inv_name: "AWX-Collection-tests-ad_hoc_command-inventory-{{ test_id }}" ssh_cred_name: "AWX-Collection-tests-ad_hoc_command-ssh-cred-{{ test_id }}" org_name: "AWX-Collection-tests-ad_hoc_command-org-{{ test_id }}" + ee_name: "AWX-Collection-tests-ad_hoc_command-ee-{{ test_id }}" - name: Create a New Organization organization: @@ -34,6 +35,16 @@ credential_type: 'Machine' state: present +- name: Create an Execution Environment + execution_environment: + name: "{{ ee_name }}" + organization: "{{ org_name }}" + description: "EE for Testing" + image: quay.io/ansible/awx-ee + pull: always + state: present + register: result_ee + - name: Launch an Ad Hoc Command waiting for it to finish ad_hoc_command: inventory: "{{ inv_name }}" @@ -61,6 +72,21 @@ - "result is changed" - "result.status == 'successful'" +- name: Launch an Ad Hoc Command with Execution Environment specified + ad_hoc_command: + inventory: "Demo Inventory" + credential: "{{ ssh_cred_name }}" + execution_environment: "{{ ee_name }}" + module_name: "ping" + wait: true + register: result + +- assert: + that: + - "result is changed" + - "result.status == 'successful'" + - "lookup('awx.awx.controller_api', 'ad_hoc_commands/' ~ result.id)['execution_environment'] == result_ee.id" + - name: Check module fails with correct msg ad_hoc_command: inventory: "{{ inv_name }}" @@ -75,6 +101,13 @@ - "result is not changed" - "'Does not exist' in result.response['json']['module_name'][0]" +- name: Delete the Execution Environment + execution_environment: + name: "{{ ee_name }}" + organization: "{{ org_name }}" + image: quay.io/ansible/awx-ee + state: absent + - name: Delete the Credential credential: name: "{{ ssh_cred_name }}"