Repository: ansible/awx

Issue:
awx.awx.group option preserve hosts fails when there are no hosts.
### 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.

### Summary

when using awx.awx.groups the preserve hosts/groups options fails if there are no groups/hosts.




### AWX version

19.4.0

### Select the relevant components

- [ ] UI
- [ ] API
- [ ] Docs

### Installation method

minikube

### Modifications

no

### Ansible version

_No response_

### Operating system

_No response_

### Web browser

_No response_

### Steps to reproduce

```
    - name: Add host to temporary group for new vms
      awx.awx.group:
        name: new_vms
        inventory: "Temporary Inventory"
        hosts:
          - "{{ name }}"
        validate_certs: false
        preserve_existing_hosts: True
      when:
        - use_aap_inventory
        - vm is succeeded
```

### Expected results

It runs and adds the host to the empty group

### Actual results

```
TypeError: 'NoneType' object is not subscriptabl
```

### Additional information

Because [This line](https://github.com/ansible/awx/blob/593eebf062cd1f73c117502e0491f3c8532695a3/awx_collection/plugins/modules/group.py#L174) returns NoneType when there are no hosts in the group.

module needs to be updated to fix this issue.


Discussion:
Thanks for creating the issue. I verified now that the task works if I have the group and an existing host in the group.

So some error while doing it for a totally new group.
it works if the group is there but empty
so it fails only in the first ever run: there is no group.
just for reference, here are the steps after creating the vm

```
        - name: Create tower inventory
          awx.awx.inventory:
            name: "Temporary Inventory"
            description: "Temporary inventory for hosts to be created"
            organization: "Default"
            state: present
            validate_certs: false
          when:
            - use_aap_inventory
            - vm is succeeded

        - name: Add tower host
          awx.awx.host:
            name: "{{ vm_name }}"
            inventory: "Temporary Inventory"
            state: present
            validate_certs: false
            variables:
              ansible_host: "{{ vm.instance.ipv4 }}"
          when:
            - use_aap_inventory
            - vm is succeeded

        - name: Add host to temporary group for new vms
          awx.awx.group:
            name: new_vms
            inventory: "Temporary Inventory"
            hosts:
              - "{{ vm_name }}"
            validate_certs: false
            preserve_existing_hosts: True
          when:
            - use_aap_inventory
            - vm is succeeded

 ```
I'm picking up issue. Assign it to me @john-westcott-iv 

Resolving patch:
diff --git a/awx_collection/plugins/modules/group.py b/awx_collection/plugins/modules/group.py
--- a/awx_collection/plugins/modules/group.py
+++ b/awx_collection/plugins/modules/group.py
@@ -170,9 +170,11 @@ def main():
             id_list.append(sub_obj['id'])
         # Preserve existing objects
         if (preserve_existing_hosts and relationship == 'hosts') or (preserve_existing_children and relationship == 'children'):
-            preserve_existing_check = module.get_all_endpoint(group['related'][relationship])
-            for sub_obj in preserve_existing_check['json']['results']:
-                id_list.append(sub_obj['id'])
+            if group:
+                preserve_existing_check = module.get_all_endpoint(group['related'][relationship])
+                for sub_obj in preserve_existing_check['json']['results']:
+                    if 'id' in sub_obj:
+                        id_list.append(sub_obj['id'])
         if id_list:
             association_fields[relationship] = id_list
 


Test patch:
diff --git a/awx_collection/tests/integration/targets/group/tasks/main.yml b/awx_collection/tests/integration/targets/group/tasks/main.yml
--- a/awx_collection/tests/integration/targets/group/tasks/main.yml
+++ b/awx_collection/tests/integration/targets/group/tasks/main.yml
@@ -9,10 +9,12 @@
     group_name1: "AWX-Collection-tests-group-group1-{{ test_id }}"
     group_name2: "AWX-Collection-tests-group-group2-{{ test_id }}"
     group_name3: "AWX-Collection-tests-group-group3-{{ test_id }}"
+    group_name4: "AWX-Collection-tests-group-group4-{{ test_id }}"
     inv_name: "AWX-Collection-tests-group-inv-{{ test_id }}"
     host_name1: "AWX-Collection-tests-group-host1-{{ test_id }}"
     host_name2: "AWX-Collection-tests-group-host2-{{ test_id }}"
     host_name3: "AWX-Collection-tests-group-host3-{{ test_id }}"
+    host_name4: "AWX-Collection-tests-group-host4-{{ test_id }}"
 
 - name: Create an Inventory
   inventory:
@@ -21,6 +23,26 @@
     state: present
   registuer: result
 
+- name: Create a Host
+  host:
+    name: "{{ host_name4 }}"
+    inventory: "{{ inv_name }}"
+    state: present
+  register: result
+
+- name: Add Host to Group
+  group:
+    name: "{{ group_name1 }}"
+    inventory: "{{ inv_name }}"
+    hosts:
+      - "{{ host_name4 }}"
+    preserve_existing_hosts: true
+  register: result
+
+- assert:
+    that:
+      - "result is changed"
+
 - name: Create Group 1
   group:
     name: "{{ group_name1 }}"