Bookmark this page

Chapter 10.  Automate Red Hat Satellite Management

Abstract

Goal

Automate Red Hat Satellite functionality at scale with Satellite API integration and Satellite Ansible Collection playbooks.

Objectives
  • Query the Red Hat Satellite REST API to observe Satellite functionality, syntax, and authentication methods.

  • Manage Red Hat Satellite infrastructure and automate operations by using playbooks from the Satellite Ansible Collection.

Sections
  • Query the Red Hat Satellite API (and Guided Exercise)

  • Manage Red Hat Satellite with Ansible (and Guided Exercise)

Lab
  • Automate Red Hat Satellite Management

Query the Red Hat Satellite API

Objectives

  • Query the Red Hat Satellite REST API to observe Satellite functionality, syntax, and authentication methods.

Describe the Red Hat Satellite API

Red Hat Satellite includes a Representational State Transfer (REST) API. You can use the REST API to control a Satellite environment without using the Satellite web UI. The Satellite REST API helps to combine Satellite functionality with any third-party application in your environment that can communicate over HTTP.

The Satellite REST API supports the following use cases:

  • Integration of Satellite functionality with production IT systems or third-party applications.

  • Automation of maintenance tasks to ensure an operational Satellite environment.

  • Automation of recurring tasks by using scripts.

Describe Satellite API Syntax

Use the following curl command syntax to make a Satellite REST API request:

[user@host]$ curl --request HTTP_VERB \1
--insecure \ 2
--user sat_username:sat_password \3
--data @file.json \ 4
--header "Content-Type:application/json" \ 5
API_ROUTE \ 6
| python -m json.tool 7

1

The HTTP_VERB argument is the intended API call to make with the curl command. The API call determines the action to perform on the Satellite environment. Use the --request option to specify an appropriate HTTP verb. The following list summarizes some of the HTTP verbs:

  • GET retrieves data from the Satellite API.

  • POST submits data to the Satellite API to create a resource.

  • PUT submits data to the Satellite API to modify a resource.

  • DELETE instructs the Satellite API to delete a resource.

2

Use the --insecure option to skip validating SSL certificates.

3

Use the --user option to specify user authentication credentials. The sat_username argument represents the Satellite username. The sat_password argument represents the password of the specified Satellite user.

4

Satellite API accepts data in JSON format only. Use the --data option to pass JSON data to the Satellite API. You can pass the JSON data from either standard input (stdin) or from a file. The file.json argument represents a file that contains data in JSON format. This option applies only to the POST and PUT HTTP verbs.

5

Use the --header option to pass extra headers to the API request. For JSON data, use the Content-Type:application/json extra header.

6

The API route of the request. For example, the GET HTTP verb with the https://satellite.example.com/katello/api/activation_keys API route lists the activation keys in the Satellite Server. In Satellite 6, the default API version is 2. If you are using the default version, you can ignore adding v2 to the URL for API calls.

7

For better readability, feed the output from the curl command to the json.tool Python module. The curl command can return structured output on any type of HTTP request, whether retrieving, creating, modifying, or deleting a resource. All of the examples in this section pipe the curl output to the Python json.tool parser.

Retrieve Data from Satellite API

Use the GET HTTP verb with the API request to retrieve information about a resource from the Satellite API.

[user@host]$ curl --request GET \
--insecure \
--user sat_username:sat_password \
API_ROUTE \
| python -m json.tool

For example, the following curl command retrieves the list of organizations in your Satellite Server, and details about them.

[user@host]$ curl --request GET \
--insecure \
--user admin:redhat \
https://satellite.lab.example.com/katello/api/organizations \
| python -m json.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   710    0   710    0     0    232      0 --:--:--  0:00:03 --:--:--   232
{
    "total": 3,
    "subtotal": 3,
    "page": 1,
    "per_page": 20,
    "search": null,
    "sort": {
        "by": null,
        "order": null
    },
    "results": [
        {
            "label": "Default_Organization",
            "created_at": "2021-12-03 13:18:50 UTC",
            "updated_at": "2021-12-03 13:30:32 UTC",
            "id": 1,
            "name": "Default Organization",
            "title": "Default Organization",
            "description": null
        },
...output omitted...

Create a Resource from Satellite API

Use the POST HTTP verb with an API request to create a resource in the Satellite Server. Ensure that you submit the data to the API in JSON format while running a POST HTTP request to the Satellite Server. To submit the JSON data from a file, create the file with its content in JSON format. The JSON file contains attribute values for the resource that you are creating.

{"parameter1":"value1", "parameter2":"value2", "parameter3":"value3"}

After creating the JSON file, run the curl command, and pass the JSON data to the Satellite API to create the resource.

[user@host]$ curl --request POST \
--insecure \
--user sat_username:sat_password \
--header "Content-Type:application/json" \
--data @file.json \
API_ROUTE \
| python -m json.tool

Modify a Resource from Satellite API

Use the PUT HTTP verb with the API request to modify a resource in the Satellite Server. Ensure that you are submitting the data to the API in JSON format while running a PUT HTTP request to the Satellite Server. To submit the JSON data from a file, create the file with its content in JSON format. The JSON file contains attribute values for the resource that you are modifying. Based on those values, the parameters of the intended resource are modified.

{"parameter1":"newvalue1", "parameter2":"newvalue2", "parameter3":"newvalue3"}

After creating the JSON file, run the curl command, and pass the JSON data to the Satellite API to modify the existing resource.

[user@host]$ curl --request PUT \
--insecure \
--user sat_username:sat_password \
--header "Content-Type:application/json" \
--data @file.json \
API_ROUTE \
| python -m json.tool

Delete a Resource from Satellite API

Use the DELETE HTTP verb with the API request to delete a resource from the Satellite Server. To delete the resource, specify an API route that includes the numeric identifier of the intended resource. Based on this numeric identifier, the API command instructs the Satellite Server to delete that resource.

[user@host]$ curl --request DELETE \
--insecure \
--user sat_username:sat_password \
--header "Content-Type:application/json" \
API_ROUTE \
| python -m json.tool

For example, the following curl command deletes the Test organization, which has an ID of 15.

[user@host]$ curl --request DELETE \
--insecure \
--user admin:redhat \
--header "Content-Type:application/json" \
https://satellite.lab.example.com/katello/api/organizations/15 \
| python -m json.tool

Revision: rh403-6.11-3ad886e