Repository: ansible/awx

Issue:
ad_hoc_command: execution_environment option is ignored
### Please confirm the following

- [X] I agree to follow this project's [code of conduct](https://docs.ansible.com/ansible/latest/community/code_of_conduct.html).
- [X] I have checked the [current issues](https://github.com/ansible/awx/issues) for duplicates.
- [X] I understand that AWX is open source software provided for free and that I might not receive a timely response.
- [X] I am **NOT** reporting a (potential) security vulnerability. (These should be emailed to `security@ansible.com` instead.)

### Bug Summary

The `execution_environment` option in `ad_hoc_command` module is ignored and runs with the default EE (`AWX EE (latest)`).



### AWX version

22.3.0

### Select the relevant components

- [ ] UI
- [ ] UI (tech preview)
- [ ] API
- [ ] Docs
- [X] Collection
- [ ] CLI
- [ ] Other

### Installation method

N/A

### Modifications

no

### Ansible version

_No response_

### Operating system

_No response_

### Web browser

_No response_

### Steps to reproduce



Run with `execution_environment` option in the `awx.awx.ad_hoc_command` module. As in the following Playbook.

```yaml
    - name: Ad Hoc command test
      awx.awx.ad_hoc_command:
        inventory: Demo Inventory
        credential: Demo Credential 
        module_name: command
        module_args: echo I <3 Ansible
        execution_environment: my_ee
        wait: true
````


### Expected results

Runs in the execution environment specified by the execution_environment option.


### Actual results

The execution_environment option is ignored and runs with the default EE (`AWX EE (latest)`).

### Additional information

_No response_


Discussion:

Resolving patch:
diff --git a/awx_collection/plugins/modules/ad_hoc_command.py b/awx_collection/plugins/modules/ad_hoc_command.py
--- 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})


Test patch:
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
--- 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 }}"