Chapter 6. API cheat sheet
You can review the following examples of how to use the Red Hat Satellite API to perform various tasks. You can use the API on Satellite Server via HTTPS on port 443.
For example, in Ruby, you can specify the Satellite Server URL as follows:
url = 'https://satellite.example.com/api/v2/' katello_url = 'https://satellite.example.com/katello/api/v2/'
url = 'https://satellite.example.com/api/v2/'
katello_url = 'https://satellite.example.com/katello/api/v2/'
You can use these values to fully automate your scripts, removing any need to verify which ports to use.
The following examples use curl
for sending API requests. For more information, see Section 5.1, “Calling the API in curl”.
6.1. Working with hosts Copy linkLink copied to clipboard!
6.1.1. Listing hosts Copy linkLink copied to clipboard!
This example returns a list of registered hosts.
API request
curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/api/v2/hosts \ | python3 -m json.tool
$ curl \
--request GET \
--user My_User_Name:My_Password \
https://satellite.example.com/api/v2/hosts \
| python3 -m json.tool
API response
6.1.2. Requesting information for a host Copy linkLink copied to clipboard!
This request returns information for the host satellite.example.com
.
API request
curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/api/v2/hosts/satellite.example.com \ | python3 -m json.tool
$ curl \
--request GET \
--user My_User_Name:My_Password \
https://satellite.example.com/api/v2/hosts/satellite.example.com \
| python3 -m json.tool
API response
6.1.3. Listing host facts Copy linkLink copied to clipboard!
This request returns all facts for the host satellite.example.com
.
API request
curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/api/v2/hosts/satellite.example.com/facts \ | python3 -m json.tool
$ curl \
--request GET \
--user My_User_Name:My_Password \
https://satellite.example.com/api/v2/hosts/satellite.example.com/facts \
| python3 -m json.tool
API response
6.1.4. Searching for hosts with matching patterns Copy linkLink copied to clipboard!
This query returns all hosts that match the pattern "example".
API request
curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/api/v2/hosts?search=example \ | python3 -m json.tool
$ curl \
--request GET \
--user My_User_Name:My_Password \
https://satellite.example.com/api/v2/hosts?search=example \
| python3 -m json.tool
API response
6.1.5. Searching for hosts in an environment Copy linkLink copied to clipboard!
This query returns all hosts in the production
environment.
API request
curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/api/v2/hosts?search=environment=production \ | python3 -m json.tool
$ curl \
--request GET \
--user My_User_Name:My_Password \
https://satellite.example.com/api/v2/hosts?search=environment=production \
| python3 -m json.tool
API response
6.1.6. Searching for hosts with a specific fact value Copy linkLink copied to clipboard!
This query returns all hosts with a model name RHV Hypervisor
.
API request
curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/api/v2/hosts?search=model=\"RHV+Hypervisor\" \ | python3 -m json.tool
$ curl \
--request GET \
--user My_User_Name:My_Password \
https://satellite.example.com/api/v2/hosts?search=model=\"RHV+Hypervisor\" \
| python3 -m json.tool
API response
6.1.7. Deleting a host Copy linkLink copied to clipboard!
This request deletes a host with a name host1.example.com.
API request
curl \ --request DELETE \ --user My_User_Name:My_Password \ https://satellite.example.com/api/v2/hosts/host1.example.com \ | python3 -m json.tool
$ curl \
--request DELETE \
--user My_User_Name:My_Password \
https://satellite.example.com/api/v2/hosts/host1.example.com \
| python3 -m json.tool
6.1.8. Downloading a full-host boot disk image Copy linkLink copied to clipboard!
This request downloads a full boot disk image for a host by its ID.
API request
curl \ --request GET \ --user My_User_Name:My_Password \ --output My_Image.iso \ https://satellite.example.com/api/bootdisk/hosts/host_ID?full=true
$ curl \
--request GET \
--user My_User_Name:My_Password \
--output My_Image.iso \
https://satellite.example.com/api/bootdisk/hosts/host_ID?full=true
6.2. Working with lifecycle environments Copy linkLink copied to clipboard!
Satellite divides application life cycles into lifecycle environments, which represent each stage of the application life cycle. Lifecycle environments are linked to from an environment path. To create linked lifecycle environments with the API, use the prior_id
parameter.
You can find the built-in API reference for lifecycle environments at https://satellite.example.com/apidoc/v2/lifecycle_environments.html
. The API routes include /katello/api/environments
and /katello/api/organizations/:organization_id/environments
.
6.2.1. Listing lifecycle environments Copy linkLink copied to clipboard!
Use this API call to list all the current lifecycle environments on your Satellite for the default organization with ID 1
.
API request
API response
6.2.2. Creating linked lifecycle environments Copy linkLink copied to clipboard!
Use this example to create a path of lifecycle environments. This procedure uses the default Library environment with ID 1
as the starting point for creating lifecycle environments.
API procedure
Choose an existing lifecycle environment that you want to use as a starting point. List the environment by using its ID. In this case, the environment with ID
1
:Example request:
curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/katello/api/environments/1 \ | python3 -m json.tool
$ curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/katello/api/environments/1 \ | python3 -m json.tool
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example response:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a JSON file, for example,
life-cycle.json
, with the following content:{"organization_id":1,"label":"api-dev","name":"API Development","prior":1}
{"organization_id":1,"label":"api-dev","name":"API Development","prior":1}
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a lifecycle environment by using the
prior
option set to1
.Example request:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example response:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the command output, you can see the ID for this lifecycle environment is
2
, and the lifecycle environment before this one is1
. Use the lifecycle environment with ID2
to create a successor to this environment.Edit the previously created
life-cycle.json
file to update thelabel
,name
, andprior
values.{"organization_id":1,"label":"api-qa","name":"API QA","prior":2}
{"organization_id":1,"label":"api-qa","name":"API QA","prior":2}
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a lifecycle environment using the
prior
option set to2
.Example request:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example response:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the command output, you can see the ID for this lifecycle environment is
3
, and the lifecycle environment before this one is2
.
6.2.3. Updating a lifecycle environment Copy linkLink copied to clipboard!
You can update a lifecycle environment using a PUT command. This example request updates a description of the lifecycle environment with ID 3
.
API request
API response
6.2.4. Deleting a lifecycle environment Copy linkLink copied to clipboard!
You can delete a lifecycle environment if it has no successor. Therefore, delete them in reverse order using a command in the following format:
API request
curl \ --request DELETE \ --user My_User_Name:My_Password \ https://satellite.example.com/katello/api/environments/:id
$ curl \
--request DELETE \
--user My_User_Name:My_Password \
https://satellite.example.com/katello/api/environments/:id
6.3. Uploading Content to Satellite Server Copy linkLink copied to clipboard!
You can use the Satellite API to upload and import large files to your Satellite Server. This process involves four steps:
- Create an upload request.
- Upload the content.
- Import the content.
- Delete the upload request.
The maximum file size that you can upload is 2 MB. For information about uploading larger content, see API procedure.
Procedure
Assign the package name to the variable
name
:Example request:
export name=jq-1.6-2.el7.x86_64.rpm
$ export name=jq-1.6-2.el7.x86_64.rpm
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Assign the checksum of the file to the variable
checksum
:Example request:
export checksum=$(sha256sum $name|cut -c 1-65)
$ export checksum=$(sha256sum $name|cut -c 1-65)
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Assign the file size to the variable
size
:Example request:
export size=$(du -bs $name|cut -f 1)
$ export size=$(du -bs $name|cut -f 1)
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create an upload request that returns the upload ID of the request by using
size
andchecksum
.Example request:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow where 76, in this case, is an example Repository ID.
Example request:
{"upload_id":"37eb5900-597e-4ac3-9bc5-2250c302fdc4"}
{"upload_id":"37eb5900-597e-4ac3-9bc5-2250c302fdc4"}
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Assign the upload ID to the variable
upload_id
:export upload_id=37eb5900-597e-4ac3-9bc5-2250c302fdc4
$ export upload_id=37eb5900-597e-4ac3-9bc5-2250c302fdc4
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Assign the path of the package you want to upload to the variable
path
:export path=/root/jq/jq-1.6-2.el7.x86_64.rpm
$ export path=/root/jq/jq-1.6-2.el7.x86_64.rpm
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Upload your content. Ensure you use the correct MIME type when you upload data. The API uses the
application/json
MIME type for the requests to Satellite unless stated otherwise. Combine the upload ID, MIME type, and other parameters to upload content.Example request:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow After you have uploaded the content to your Satellite Server, you need to import it into the appropriate repository. Until you complete this step, Satellite Server does not detect the new content.
Example request:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow After you have successfully uploaded and imported your content, you can delete the upload request. This frees any temporary disk space that data is using during the upload.
Example request:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
6.3.1. Uploading content larger than 2 MB Copy linkLink copied to clipboard!
The following example demonstrates how to split a large file into chunks, create an upload request, upload the individual files, import them to Satellite, and then delete the upload request. Note that this example uses sample content, host names, user names, repository ID, and file names.
API procedure
Assign the package name to the variable
name
:export name=bpftool-3.10.0-1160.2.1.el7.centos.plus.x86_64.rpm
$ export name=bpftool-3.10.0-1160.2.1.el7.centos.plus.x86_64.rpm
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Assign the checksum of the file to the variable
checksum
:export checksum=$(sha256sum $name|cut -c 1-65)
$ export checksum=$(sha256sum $name|cut -c 1-65)
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Assign the file size to the variable
size
:export size=$(du -bs $name|cut -f 1)
$ export size=$(du -bs $name|cut -f 1)
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The following command creates a new upload request and returns the upload ID of the request using
size
andchecksum
.Example request:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow where 76, in this case, is an example Repository ID.
Example output
{"upload_id":"37eb5900-597e-4ac3-9bc5-2250c302fdc4"}
{"upload_id":"37eb5900-597e-4ac3-9bc5-2250c302fdc4"}
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Assign the upload ID to the variable
upload_id
:export upload_id=37eb5900-597e-4ac3-9bc5-2250c302fdc4
$ export upload_id=37eb5900-597e-4ac3-9bc5-2250c302fdc4
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Split the file in 2MB chunks:
split \ --bytes 2MB \ --numeric-suffixes \ --suffix-length=1 \ bpftool-3.10.0-1160.2.1.el7.centos.plus.x86_64.rpm bpftool
$ split \ --bytes 2MB \ --numeric-suffixes \ --suffix-length=1 \ bpftool-3.10.0-1160.2.1.el7.centos.plus.x86_64.rpm bpftool
Copy to Clipboard Copied! Toggle word wrap Toggle overflow View the file chunks:
ls -l bpftool[0-9]
$ ls -l bpftool[0-9]
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output:
-rw-r--r--. 1 root root 2000000 Mar 31 14:15 bpftool0 -rw-r--r--. 1 root root 2000000 Mar 31 14:15 bpftool1 -rw-r--r--. 1 root root 2000000 Mar 31 14:15 bpftool2 -rw-r--r--. 1 root root 2000000 Mar 31 14:15 bpftool3 -rw-r--r--. 1 root root 868648 Mar 31 14:15 bpftool4
-rw-r--r--. 1 root root 2000000 Mar 31 14:15 bpftool0 -rw-r--r--. 1 root root 2000000 Mar 31 14:15 bpftool1 -rw-r--r--. 1 root root 2000000 Mar 31 14:15 bpftool2 -rw-r--r--. 1 root root 2000000 Mar 31 14:15 bpftool3 -rw-r--r--. 1 root root 868648 Mar 31 14:15 bpftool4
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Assign the prefix of the split files to the variable path.
export path=/root/tmp/bpftool
$ export path=/root/tmp/bpftool
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Upload the file chunks. The offset starts at 0 bytes for the first chunk and increases by 2000000 bytes for each file. Note the use of the offset parameter and how it relates to the file size. Note also that the indexes are used after the path variable, for example, ${path}0, ${path}1.
Example requests:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Import the complete upload to the repository:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Delete the upload request:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
6.3.2. Uploading duplicate content Copy linkLink copied to clipboard!
You can reuse existing content in Satellite instead of uploading duplicate content to Satellite through the API.
API procedure
Upload content to Satellite:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The call will return a content unit ID instead of an upload ID, similar to this:
{"content_unit_href":"/pulp/api/v3/content/file/files/c1bcdfb8-d840-4604-845e-86e82454c747/"}
{"content_unit_href":"/pulp/api/v3/content/file/files/c1bcdfb8-d840-4604-845e-86e82454c747/"}
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can copy this output and call import uploads directly to add the content to a repository:
API response
Note that the call changes from using upload_id
to using content_unit_id
.
6.4. Using extended searches Copy linkLink copied to clipboard!
You can find search parameters that you can use to build your search queries in the Satellite web UI. For more information, see Building search queries in Administering Red Hat Satellite.
For example, you can search for hosts.
Procedure
- In the Satellite web UI, navigate to Hosts > All Hosts.
- Click the Search field to display a list of search parameters.
- Locate the search parameters that you want to use. For this example, locate os_title and model.
Combine the search parameters in your API query as follows:
Example request:
curl \ --user My_User_Name:My_Password \ https://satellite.example.com/api/v2/hosts?search=os_title=\"RedHat+7.7\",model=\"PowerEdge+R330\" \ | python3 -m json.tool
$ curl \ --user My_User_Name:My_Password \ https://satellite.example.com/api/v2/hosts?search=os_title=\"RedHat+7.7\",model=\"PowerEdge+R330\" \ | python3 -m json.tool
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example response:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
6.5. Using searches with pagination control Copy linkLink copied to clipboard!
You can use the per_page
and page
pagination parameters to limit the search results that an API search query returns. The per_page
parameter specifies the number of results per page and the page
parameter specifies which page, as calculated by the per_page
parameter, to return.
The default number of items to return is set to 1000 when you do not specify any pagination parameters, but the per_page
value has a default of 20 which applies when you specify the page
parameter.
This example returns a list of activation keys for an organization with ID 1
in pages. The list contains 30 keys per page and returns the second page.
API request
curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/katello/api/activation_keys?organization_id=1&per_page=30&page=2
$ curl \
--request GET \
--user My_User_Name:My_Password \
https://satellite.example.com/katello/api/activation_keys?organization_id=1&per_page=30&page=2
6.5.1. Returning multiple pages Copy linkLink copied to clipboard!
You can use a for
loop structure to get multiple pages of results. This example returns pages 1 to 3 of Content Views with 5 results per page.
Bash script
6.6. Overriding Smart Class parameters Copy linkLink copied to clipboard!
You can search for Smart Parameters by using the API and supply a value to override a Smart Parameter in a Class. You can find the full list of attributes that you can modify in the built-in API reference at https://satellite.example.com/apidoc/v2/smart_class_parameters/update.html
.
Procedure
Find the ID of the Smart Class parameter you want to change:
List all Smart Class Parameters.
Example request:
curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/api/smart_class_parameters
$ curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/api/smart_class_parameters
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you know the Puppet class ID, for example 5, you can restrict the scope: Example request:
curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/api/puppetclasses/5/smart_class_parameters
$ curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/api/puppetclasses/5/smart_class_parameters
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Both calls accept a search parameter. You can view the full list of searchable fields in the Satellite web UI. Navigate to Configure > Smart variables and click in the search query box to reveal the list of fields.
Two particularly useful search parameters are
puppetclass_name
andkey
, which you can use to search for a specific parameter. For example, use the--data
option to pass URL encoded data.Example request:
curl \ --request GET \ --user My_User_Name:My_Password \ --data 'search=puppetclass_name = access_insights_client and key = authmethod' \ https://satellite.example.com/api/smart_class_parameters
$ curl \ --request GET \ --user My_User_Name:My_Password \ --data 'search=puppetclass_name = access_insights_client and key = authmethod' \ https://satellite.example.com/api/smart_class_parameters
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Satellite supports standard scoped-search syntax.
When you find the ID of the parameter, list the full details including current override values.
Example request:
curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/api/smart_class_parameters/63
$ curl \ --request GET \ --user My_User_Name:My_Password \ https://satellite.example.com/api/smart_class_parameters/63
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enable overriding of parameter values.
Example request:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note that you cannot create or delete the parameters manually. You can only modify their attributes. Satellite creates and deletes parameters only upon class import from Capsules.
Add custom override matchers.
Example request:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For more information about override values, see
https://satellite.example.com/apidoc/v2/override_values.html
.You can delete override values.
Example request:
curl \ --request DELETE \ --user My_User_Name:My_Password \ https://satellite.example.com/api/smart_class_parameters/63/override_values/3
$ curl \ --request DELETE \ --user My_User_Name:My_Password \ https://satellite.example.com/api/smart_class_parameters/63/override_values/3
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
6.7. Modifying a Smart Class parameter by using an external file Copy linkLink copied to clipboard!
You can modify a Puppet Smart Class parameter by using an external file.
Using external files simplifies working with JSON data. You can use an editor with syntax highlighting to avoid and locate mistakes. This example uses a MOTD Puppet manifest.
API procedure
Search for the Puppet Class by name,
motd
in this case.Example request:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Examine the following output. Each Smart Class Parameter has an ID that is global for the same Satellite instance. The
content
parameter of themotd
class hasid=3
. Do not confuse this with the Puppet Class ID that displays before the Puppet Class name.Example response:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the parameter ID
3
to get the information specific to themotd
parameter and redirect the output to a file, for example, output_file.json.Example request:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Copy the file created in the previous step to a new file for editing, for example,
changed_file.json
:cp output_file.json changed_file.json
$ cp output_file.json changed_file.json
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Modify the required values in the file. In this example, change the content parameter of the
motd
module, which requires changing theoverride
option fromfalse
totrue
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow After editing the file, verify that it looks as follows and then save the changes:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Submit the file to Satellite:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
6.8. Deleting OpenSCAP reports Copy linkLink copied to clipboard!
In Satellite Server, you can delete one or more OpenSCAP reports. However, when you delete reports, you must delete one page at a time. If you want to delete all OpenSCAP reports, use the bash script that follows.
API Procedure
List all OpenSCAP reports. Note the IDs of the reports that you want to delete.
Example request:
curl \ --user My_User_Name:My_Password \ https://satellite.example.com/api/v2/compliance/arf_reports/ \ | python3 -m json.tool
$ curl \ --user My_User_Name:My_Password \ https://satellite.example.com/api/v2/compliance/arf_reports/ \ | python3 -m json.tool
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example response:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Using an ID from the previous step, delete the OpenSCAP report. Repeat for each ID that you want to delete.
Example request:
curl \ --user My_User_Name:My_Password \ --header "Content-Type: application/json" \ --request DELETE \ https://satellite.example.com/api/v2/compliance/arf_reports/405
$ curl \ --user My_User_Name:My_Password \ --header "Content-Type: application/json" \ --request DELETE \ https://satellite.example.com/api/v2/compliance/arf_reports/405
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example response:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Example BASH script to delete all OpenSCAP reports