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

Solution Verified - Updated -

Environment

  • Red Hat Insights
  • Red Hat Hybrid Cloud Console

Issue

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

Resolution

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

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/workspaces 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/workspace 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/workspace 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/workspace 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/workspace can be passed in the array by delimiting the system_ids with a comma.

  • Create and assign systems to a new group/workspace 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/workspace can be passed in the array by delimiting the system_ids with a comma.

  • Remove a system from a group/workspace 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/workspace can be passed in by delimiting the system_ids with a comma.

  • Delete an inventory group/workspace 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