# Ansible AWX - Add host to nested group of ineventory

**URL:** <https://forum.ansible.com/t/ansible-awx-add-host-to-nested-group-of-ineventory/41683>\
**Category:** Get Help\
**Tags:** rhel, awx\
**Created:** [April 11, 2025, 4:43pm UTC](https://forum.ansible.com/t/ansible-awx-add-host-to-nested-group-of-ineventory/41683 "2025-04-11T16:43:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alainseys](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alainseys/32/2757_2.png) [@alainseys](https://forum.ansible.com/u/alainseys)\
**Post date:** [April 11, 2025, 4:43pm UTC](https://forum.ansible.com/t/ansible-awx-add-host-to-nested-group-of-ineventory/41683/1 "2025-04-11T16:43:00Z")

</div>

Hello,

I am currently using Ansible with AWX to deploy our VMs across different hypervisors. While the deployment process is functioning well, I would like to optimize it further. However, I am having difficulty finding the correct API information.

Specifically, I need to know the appropriate endpoint to target groups within an inventory. My goal is to ensure that when a VM is deployed based on its template (Debian or RHEL), it is automatically added to the corresponding group. Currently, all VMs are deployed under the top inventory group (AWX\_LINUX), but I would like Debian-based VMs to be categorized under DEBIAN\_BASED and Red Hat VMs under RHEL\_BASED.

This adjustment will allow the subsequent playbooks to run automatically on the correct systems, rather than having to manually target RHEL and Debian in the workflow, which is causing some delays.

Any guidance on the API endpoint or best practices for achieving this would be greatly appreciated.

What i have found so far  
[https://localhost/api/v2/inventories?id=11](https://localhost/api/v2/inventories?id=11) // Correct inventory  
[https://localhost/api/v2/groups?name=DEBIAN\_BASED](https://localhost/api/v2/groups?name=DEBIAN_BASED) // Correct group

How can i add now the exisitng host that is in awx to the group (the host is in the inventory with id 11)

 ![image](https://us1.discourse-cdn.com/ansible/original/2X/d/de760ec1d7d7895cd23b61c94ba33e3655d20cfe.png)

 ![image](https://us1.discourse-cdn.com/ansible/original/2X/b/b3152a3e4ff31e3fd0305db97a71bcc88e1ec5c4.png)

Thanks in advance

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [April 12, 2025, 3:08pm UTC](https://forum.ansible.com/t/ansible-awx-add-host-to-nested-group-of-ineventory/41683/2 "2025-04-12T15:08:15Z")

</div>

Check out the [operations/Host/Hosts\_hosts\_groups\_create](https://ansible.readthedocs.io/projects/awx/en/latest/rest_api/api_ref.html#/operations/Hosts/Hosts_hosts_groups_create) API POST function. Don’t be misled by the “groups create” name. It’s also used to associate an existing host with an existing group, and to remove (“`disassociate`”) a host from a group.

> POST [/api/v2/hosts/{id}/groups/](https://ansible.readthedocs.io/projects/awx/en/latest/rest_api/api_ref.html#/operations/Hosts/Hosts_hosts_groups_create)
> 
> Create a Group for a Host
> 
> Make a POST request to this resource with the following group  
> fields to create a new group associated with this  
> host.
> 
> - `name`: Name of this group. (string, required)
> - `description`: Optional description of this group. (string, default=`""`)
> - `inventory`: (id, required)
> - `variables`: Group variables in JSON or YAML format. (json, default=``)
> 
> # Add Groups for a Host:
> 
> Make a POST request to this resource with only an `id` field to associate an  
> existing group with this host.
> 
> # Remove Groups from this Host:
> 
> Make a POST request to this resource with `id` and `disassociate` fields to  
> remove the group from this host  
> without deleting the group.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![alainseys](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alainseys/32/2757_2.png) [@alainseys](https://forum.ansible.com/u/alainseys)\
**Post date:** [April 13, 2025, 11:34am UTC](https://forum.ansible.com/t/ansible-awx-add-host-to-nested-group-of-ineventory/41683/3 "2025-04-13T11:34:15Z")

</div>

Hello @utoddl ,

Thanks for the info i will take a look in to it.  
I will keep you posted

---

<div class="post-metadata">

**Author:** ![alainseys](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alainseys/32/2757_2.png) [@alainseys](https://forum.ansible.com/u/alainseys)\
**Post date:** [April 14, 2025, 6:30am UTC](https://forum.ansible.com/t/ansible-awx-add-host-to-nested-group-of-ineventory/41683/4 "2025-04-14T06:30:50Z")

</div>

> [@utoddl](#):
>
> api/v2/hosts/{id}/groups/

Justed tested this out and this works thanks for sending the api reference , this wil come in handy in future projects 😉

---

<div class="post-metadata">

**Author:** ![Klaas](https://avatars.discourse-cdn.com/v4/letter/k/ecc23a/32.png) [@Klaas](https://forum.ansible.com/u/Klaas)\
**Post date:** [April 16, 2025, 8:12am UTC](https://forum.ansible.com/t/ansible-awx-add-host-to-nested-group-of-ineventory/41683/5 "2025-04-16T08:12:28Z")

</div>

I am using a constructed inventory with keyed groups. So a host will have variable like template: rhel or template: debian and from those I automatically create groups.

---

<div class="post-metadata">

**Author:** ![alainseys](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alainseys/32/2757_2.png) [@alainseys](https://forum.ansible.com/u/alainseys)\
**Post date:** [April 28, 2025, 2:50pm UTC](https://forum.ansible.com/t/ansible-awx-add-host-to-nested-group-of-ineventory/41683/6 "2025-04-28T14:50:22Z")

</div>

nice to know but that was not a valid option in our setup.

good to know tough 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 28, 2025, 2:51pm UTC](https://forum.ansible.com/t/ansible-awx-add-host-to-nested-group-of-ineventory/41683/7 "2025-05-28T14:51:04Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
