Ansible AWX - Add host to nested group of ineventory

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 // Correct inventory
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)

Thanks in advance

Check out the operations/Host/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/

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.

3 Likes

Hello @utoddl ,

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

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

1 Like

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.