How to automate the process of assigning systems to groups in Insights?

Solution Verified - Updated -

Environment

  • Red Hat Insights
  • Red Hat Hybrid Cloud Console

Issue

  • Is it possible to auto assign servers to groups in Inventory?
  • Is there a way to automate the process of assigning a group to systems, to avoid having to manually assign them using the Red Hat Insights user interface?
  • How to assign a system or a group of systems to an existing inventory group through an automation process using the Red Hat Insights API?

Resolution

The existing Managed Inventory API can be used to manage inventory groups (create, read, update, delete) and automate the process of assigning systems to a group.

Note that the examples provided in this document use basic authentication for simplicity (basic authentication support will be discontinued starting Dec 31, 2024). The preferred way to authenticate is to use the token-based authentication with service accounts. More information is available on Transition of Red Hat Hybrid Cloud Console APIs from basic authentication to token-based authentication via service accounts.

  • Get a list of existing inventory groups using Managed Inventory API:
# curl -X GET "https://console.redhat.com/api/inventory/v1/groups" -H 'accept: application/vnd.api+json' -u <login_id> | jq '.results[] | .id +" "+ .name'
  • Create an inventory group using Managed Inventory API:
# curl -X POST "https://console.redhat.com/api/inventory/v1/groups" -H 'accept: application/vnd.api+json' -H 'Content-Type: application/vnd.api+json' -d '{ "name":"<group_name>" }' -u <login_id> | jq
  • Update the name of a group using Managed Inventory API:
# curl -X PATCH "https://console.redhat.com/api/inventory/v1/groups/<group_id>" -H 'accept: application/vnd.api+json' -H 'Content-Type: application/vnd.api+json' -d '{ "name":"<group_name>" }' -u <login_id> | jq
  • Retrieve System IDs using Managed Inventory API:
# curl -X GET "https://console.redhat.com/api/inventory/v1/hosts" -H 'accept: application/vnd.api+json' -u <login_id> | jq '.results[] | .id +" "+ .fqdn'
  • Assign systems to an existing group using Managed Inventory API:
# curl -X POST "https://console.redhat.com/api/inventory/v1/groups/<group_id>/hosts" -H 'accept: application/vnd.api+json' -H 'Content-Type: application/vnd.api+json' -d '[ "<system_id>" ]' -u <login_id> | jq

or

# curl -X PATCH "https://console.redhat.com/api/inventory/v1/groups/<group_id>" -H 'accept: application/vnd.api+json' -H 'Content-Type: application/vnd.api+json' -d '{ "host_ids": [ "<system_id>" ] }' -u <login_id> | jq

Note: A list of system_ids to be associated with the existing group can be passed in the array by delimiting the system_ids with a comma.

  • Create and assign systems to a new group using Managed Inventory API:
# curl -X POST "https://console.redhat.com/api/inventory/v1/groups" -H 'accept: application/vnd.api+json' -H 'Content-Type: application/vnd.api+json' -d '{ "host_ids": [ "<system_id>" ], "name":"<group_name>" }' -u <login_id> | jq

Note: A list of system_ids to be associated with the new group can be passed in the array by delimiting the system_ids with a comma.

  • Remove a system from a group using Managed Inventory API:
# curl -X DELETE "https://console.redhat.com/api/inventory/v1/groups/<group_id>/hosts/<system_id>" -H 'accept: application/vnd.api+json' -u <login_id> | jq

or

# curl -X DELETE "https://console.redhat.com/api/inventory/v1/groups/hosts/<system_id>" -H 'accept: application/vnd.api+json' -u <login_id> | jq

Note: A list of system_ids to be removed from their group can be passed in by delimiting the system_ids with a comma.

  • Delete an inventory group using Managed Inventory API:
# curl -X DELETE "https://console.redhat.com/api/inventory/v1/groups/<group_id>" -H 'accept: application/vnd.api+json' -u <login_id> | jq

Note: A list of group_ids to be deleted can be passed in by delimiting the group_ids with a comma.

For more information, please see the Managed Inventory API documentation.

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments