Chapter 4. Getting Started with the Red Hat Satellite API
url = 'https://satellite6.example.com/api/v2/' capsule_url = 'https://capsule.example.com:8443/api/v2/' katello_url = 'https://satellite6.example.com/katello/api/v2/'
url = 'https://satellite6.example.com/api/v2/'
capsule_url = 'https://capsule.example.com:8443/api/v2/'
katello_url = 'https://satellite6.example.com/katello/api/v2/'
/etc/rhsm/rhsm.conf
file, in the port
entry of the [server] section. You can use these values to fully automate your scripts, removing any need to verify which ports to use.
4.1. API Examples Using Curl Copy linkLink copied to clipboard!
curl
to perform various tasks using the Satellite API.
4.1.1. Performing Simple Queries Copy linkLink copied to clipboard!
curl
to search for information about your Satellite deployment. These examples include both the actual command and some sample output, and example values for user names and passwords. Expect different results for each deployment. These examples also use the python -m json.tool
command to format the output.
Note
-k
(insecure) option to bypass certificate checks.
-u username:password
or, if you do not include the password, the command prompts you to enter it. Red Hat recommends that you do not include the password as part of the command, because it then becomes part of your shell history and might present a security risk. These examples include the password only for the sake of simplicity.
-s
(silent) option with curl
that you will not see a progress meter or any error messages.
The following is a basic query that returns a list of resources. Such requests return a list of data wrapped in metadata, while other request types only return the actual object.
Example 4.1. Listing Users
The following query returns information for the host satellite6.example.com
:
The following query returns all facts for the host satellite6.example.com
:
The following query returns all hosts that match the pattern "example":
The following query returns all hosts in the "production" environment:
The following query returns all hosts with a model name "RHEV Hypervisor":
4.1.2. Creating and Modifying Resources Copy linkLink copied to clipboard!
Accept:version=2
in the request header. The URL specification takes precedence.
curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" -X POST \ -u username:password -k \ -d json-formatted-data https://satellite6.example.com
$ curl -H "Accept:application/json,version=2" \
-H "Content-Type:application/json" -X POST \
-u username:password -k \
-d json-formatted-data https://satellite6.example.com
curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" -X POST -u sat_username:sat_password \ -k -d "{\"architecture\":{\"name\":\"i686\"}}" \ https://satellite6.example.com/api/architectures
$ curl -H "Accept:application/json,version=2" \
-H "Content-Type:application/json" -X POST -u sat_username:sat_password \
-k -d "{\"architecture\":{\"name\":\"i686\"}}" \
https://satellite6.example.com/api/architectures
{"name":"i686","id":3,"created_at":"2015-10-29T13:21:09Z","updated_at":"2015-10-29T13:21:09Z","operatingsystems":[],"images":[]}
{"name":"i686","id":3,"created_at":"2015-10-29T13:21:09Z","updated_at":"2015-10-29T13:21:09Z","operatingsystems":[],"images":[]}
hammer
on the Satellite Server to verify the results:
Example 4.2. Creating a New User
curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" -X POST \ -u sat_username:sat_password -k \ -d "{\"firstname\":\"Test\",\"lastname\":\"API-User\",\"mail\":\"test@example.com\",\"login\":\"test_api\",\"password\":\"123456\",\"auth_source_id\":1}" \ https://satellite6.example.com/api/users
$ curl -H "Accept:application/json,version=2" \
-H "Content-Type:application/json" -X POST \
-u sat_username:sat_password -k \-u sat_username:sat_password -k \-u sat_username:sat_password -k \-u sat_username:sat_password -k \-u sat_username:sat_password -k \
-d "{\"firstname\":\"Test\",\"lastname\":\"API-User\",\"mail\":\"test@example.com\",\"login\":\"test_api\",\"password\":\"123456\",\"auth_source_id\":1}" \-d "{\"firstname\":\"Test\",\"lastname\":\"API-User\",\"mail\":\"test@example.com\",\"login\":\"test_api\",\"password\":\"123456\",\"auth_source_id\":1}" \-d "{\"firstname\":\"Test\",\"lastname\":\"API-User\",\"mail\":\"test@example.com\",\"login\":\"test_api\",\"password\":\"123456\",\"auth_source_id\":1}" \-d "{\"firstname\":\"Test\",\"lastname\":\"API-User\",\"mail\":\"test@example.com\",\"login\":\"test_api\",\"password\":\"123456\",\"auth_source_id\":1}" \-d "{\"firstname\":\"Test\",\"lastname\":\"API-User\",\"mail\":\"test@example.com\",\"login\":\"test_api\",\"password\":\"123456\",\"auth_source_id\":1}" \-d "{\"firstname\":\"Test\",\"lastname\":\"API-User\",\"mail\":\"test@example.com\",\"login\":\"test_api\",\"password\":\"123456\",\"auth_source_id\":1}" \-d "{\"firstname\":\"Test\",\"lastname\":\"API-User\",\"mail\":\"test@example.com\",\"login\":\"test_api\",\"password\":\"123456\",\"auth_source_id\":1}" \-d "{\"firstname\":\"Test\",\"lastname\":\"API-User\",\"mail\":\"test@example.com\",\"login\":\"test_api\",\"password\":\"123456\",\"auth_source_id\":1}" \-d "{\"firstname\":\"Test\",\"lastname\":\"API-User\",\"mail\":\"test@example.com\",\"login\":\"test_api\",\"password\":\"123456\",\"auth_source_id\":1}" \-d "{\"firstname\":\"Test\",\"lastname\":\"API-User\",\"mail\":\"test@example.com\",\"login\":\"test_api\",\"password\":\"123456\",\"auth_source_id\":1}" \-d "{\"firstname\":\"Test\",\"lastname\":\"API-User\",\"mail\":\"test@example.com\",\"login\":\"test_api\",\"password\":\"123456\",\"auth_source_id\":1}" \-d "{\"firstname\":\"Test\",\"lastname\":\"API-User\",\"mail\":\"test@example.com\",\"login\":\"test_api\",\"password\":\"123456\",\"auth_source_id\":1}" \-d "{\"firstname\":\"Test\",\"lastname\":\"API-User\",\"mail\":\"test@example.com\",\"login\":\"test_api\",\"password\":\"123456\",\"auth_source_id\":1}" \
https://satellite6.example.com/api/usershttps://satellite6.example.com/api/usershttps://satellite6.example.com/api/users
4.1.2.1. Uploading Content to the Satellite Server Copy linkLink copied to clipboard!
curl
with the Satellite 6 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.
Procedure 4.1. Uploading Content to the Satellite Server
- Create the upload request. Ensure you modify the example parameters to suit your deployment:
curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" \ -X POST \ -u sat_username:sat_password -k -d "{}" \ https://satellite6.example.com/katello/api/repositories/3/content_uploads
$ curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" \ -X POST \ -u sat_username:sat_password -k -d "{}" \ https://satellite6.example.com/katello/api/repositories/3/content_uploads
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This command returns theupload_id
similar to the following:{"upload_id":"0be156b1-f373-4cad-89d0-924f8f4491d2","_href":"/pulp/api/v2/content/uploads/0be156b1-f373-4cad-89d0-924f8f4491d2/"}
{"upload_id":"0be156b1-f373-4cad-89d0-924f8f4491d2","_href":"/pulp/api/v2/content/uploads/0be156b1-f373-4cad-89d0-924f8f4491d2/"}
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Upload your content. Ensure you use the correct MIME type when you upload data. The "application/json" MIME type is used for the majority of requests to Satellite 6. Combine the
upload_id
, MIME type, and other parameters to upload content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - After you have uploaded the content to the Satellite Server, you need to import it into the appropriate repository. Until you complete this step, the Satellite Server will not be aware of the new content:
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 was used during the upload:
curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" \ -X DELETE -d "{}" \ -u sat_username:sat_password \ -k https://satellite6.example.com/katello/api/repositories/3/content_uploads/0be156b1-f373-4cad-89d0-924f8f4491d2
$ curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" \ -X DELETE -d "{}" \ -u sat_username:sat_password \ -k https://satellite6.example.com/katello/api/repositories/3/content_uploads/0be156b1-f373-4cad-89d0-924f8f4491d2
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Example 4.3. Uploading Content Larger than 30 MB
- Download the sample module:
wget https://forgeapi.puppetlabs.com/v3/files/theforeman-foreman-5.0.1.tar.gz?_ga=1.267255502.1792403825.1430297670 -O theforeman-foreman-5.0.1.tar.gz
$ wget https://forgeapi.puppetlabs.com/v3/files/theforeman-foreman-5.0.1.tar.gz?_ga=1.267255502.1792403825.1430297670 -O theforeman-foreman-5.0.1.tar.gz
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Split the module into 50,000 byte chunks:split --bytes 50000 --numeric-suffixes --suffix-length=1 theforeman-foreman-5.0.1.tar.gz foreman_module.
$ split --bytes 50000 --numeric-suffixes --suffix-length=1 theforeman-foreman-5.0.1.tar.gz foreman_module.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow View the resulting files:ls -la theforeman-foreman-5.0.1.tar.gz foreman_module.*
$ ls -la theforeman-foreman-5.0.1.tar.gz foreman_module.* -rw-r--r--. 1 root root 50000 Nov 4 04:42 foreman_module.0 -rw-r--r--. 1 root root 32928 Nov 4 04:42 foreman_module.1 -rw-r--r--. 1 root root 82928 Nov 4 04:41 theforeman-foreman-5.0.1.tar.gz
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Create a new upload request (this is the equivalent of
cat
on the Satellite Server).Copy to Clipboard Copied! Toggle word wrap Toggle overflow The above command returns an upload ID:{"upload_id":"9585528f-07ad-4bb1-9c80-ccece249b2b7","_href":"/pulp/api/v2/content/uploads/9585528f-07ad-4bb1-9c80-ccece249b2b7/"}
{"upload_id":"9585528f-07ad-4bb1-9c80-ccece249b2b7","_href":"/pulp/api/v2/content/uploads/9585528f-07ad-4bb1-9c80-ccece249b2b7/"}
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Upload the file chunks that you created in Step 1. Notice the use of the
offset
parameter in this example and how it relates to the file size:Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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:
curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" \ -X DELETE -d "{}" \ -u sat_username:sat_password \ -k https://ibm-vm01.example.com/katello/api/repositories/2/content_uploads/9585528f-07ad-4bb1-9c80-ccece249b2b7
$ curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" \ -X DELETE -d "{}" \ -u sat_username:sat_password \ -k https://ibm-vm01.example.com/katello/api/repositories/2/content_uploads/9585528f-07ad-4bb1-9c80-ccece249b2b7
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Log in to the Satellite Server to check if the file was transferred correctly:
ls -la /var/lib/pulp/content/puppet_module/theforeman-foreman-5.0.1.tar.gz
$ ls -la /var/lib/pulp/content/puppet_module/theforeman-foreman-5.0.1.tar.gz -rw-r--r--. 1 apache apache 82928 Nov 4 04:55 /var/lib/pulp/content/puppet_module/theforeman-foreman-5.0.1.tar.gz
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compare the files:cmp /var/lib/pulp/content/puppet_module/theforeman-foreman-5.0.1.tar.gz theforeman-foreman-5.0.1.tar.gz
$ cmp /var/lib/pulp/content/puppet_module/theforeman-foreman-5.0.1.tar.gz theforeman-foreman-5.0.1.tar.gz
Copy to Clipboard Copied! Toggle word wrap Toggle overflow echo $?
$ echo $? 0
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.1.3. Overriding Smart Class Parameters Copy linkLink copied to clipboard!
https://satellite6.example.com/apidoc/v2/smart_class_parameters/update.html
.
GET /api/smart_class_parameters
. Using curl
, the command is as follows:
curl -X GET -s -k -u sat_username:sat_password \ https://satellite6.example.com/api/smart_class_parameters
$ curl -X GET -s -k -u sat_username:sat_password \curl -X GET -s -k -u sat_username:sat_password \curl -X GET -s -k -u sat_username:sat_password \curl -X GET -s -k -u sat_username:sat_password \curl -X GET -s -k -u sat_username:sat_password \
https://satellite6.example.com/api/smart_class_parametershttps://satellite6.example.com/api/smart_class_parametershttps://satellite6.example.com/api/smart_class_parameters
curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/puppetclasses/5/smart_class_parameters
$ curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/puppetclasses/5/smart_class_parameterscurl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/puppetclasses/5/smart_class_parameterscurl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/puppetclasses/5/smart_class_parameterscurl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/puppetclasses/5/smart_class_parameterscurl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/puppetclasses/5/smart_class_parameterscurl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/puppetclasses/5/smart_class_parameterscurl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/puppetclasses/5/smart_class_parameters
puppetclass_name
and key
, which enables you to search for a specific parameter. For example, using the -d, --data
option to pass URL encoded data:
curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/smart_class_parameters -d 'search=puppetclass_name = access_insights_client and key = authmethod'
$ curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/smart_class_parameters -d 'search=puppetclass_name = access_insights_client and key = authmethod'curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/smart_class_parameters -d 'search=puppetclass_name = access_insights_client and key = authmethod'curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/smart_class_parameters -d 'search=puppetclass_name = access_insights_client and key = authmethod'curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/smart_class_parameters -d 'search=puppetclass_name = access_insights_client and key = authmethod'curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/smart_class_parameters -d 'search=puppetclass_name = access_insights_client and key = authmethod'curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/smart_class_parameters -d 'search=puppetclass_name = access_insights_client and key = authmethod'curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/smart_class_parameters -d 'search=puppetclass_name = access_insights_client and key = authmethod'
GET /api/smart_class_parameters/63
. Using curl
, the command would be:
curl -X GET -s -k -u sat_username:sat_password \ https://satellite6.example.com/api/smart_class_parameters/63
$ curl -X GET -s -k -u sat_username:sat_password \curl -X GET -s -k -u sat_username:sat_password \curl -X GET -s -k -u sat_username:sat_password \curl -X GET -s -k -u sat_username:sat_password \curl -X GET -s -k -u sat_username:sat_password \
https://satellite6.example.com/api/smart_class_parameters/63https://satellite6.example.com/api/smart_class_parameters/63https://satellite6.example.com/api/smart_class_parameters/63
curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" -X PUT \ -s -k -u sat_username:sat_password \ -d '{"smart_class_parameter":{"override":true}}' \ https://satellite6.example.com/api/smart_class_parameters/63
$ curl -H "Accept:application/json,version=2" \
-H "Content-Type:application/json" -X PUT \
-s -k -u sat_username:sat_password \-s -k -u sat_username:sat_password \-s -k -u sat_username:sat_password \-s -k -u sat_username:sat_password \-s -k -u sat_username:sat_password \
-d '{"smart_class_parameter":{"override":true}}' \
https://satellite6.example.com/api/smart_class_parameters/63https://satellite6.example.com/api/smart_class_parameters/63https://satellite6.example.com/api/smart_class_parameters/63
curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" -X PUT \ -s -k -u sat_username:sat_password \ -d '{"smart_class_parameter":{"override_value":{"match":"hostgroup=Test","value":"2.4.6"}}}' \ https://satellite6.example.com/api/smart_class_parameters/63
$ curl -H "Accept:application/json,version=2" \
-H "Content-Type:application/json" -X PUT \
-s -k -u sat_username:sat_password \-s -k -u sat_username:sat_password \-s -k -u sat_username:sat_password \-s -k -u sat_username:sat_password \-s -k -u sat_username:sat_password \
-d '{"smart_class_parameter":{"override_value":{"match":"hostgroup=Test","value":"2.4.6"}}}' \
https://satellite6.example.com/api/smart_class_parameters/63https://satellite6.example.com/api/smart_class_parameters/63https://satellite6.example.com/api/smart_class_parameters/63
https://satellite6.example.com/apidoc/v2/override_values.html
.
curl -X DELETE -s -u sat_username:sat_password \ https://satellite6.example.com/api/smart_class_parameters/63/override_values/3
$ curl -X DELETE -s -u sat_username:sat_password \curl -X DELETE -s -u sat_username:sat_password \curl -X DELETE -s -u sat_username:sat_password \curl -X DELETE -s -u sat_username:sat_password \curl -X DELETE -s -u sat_username:sat_password \
https://satellite6.example.com/api/smart_class_parameters/63/override_values/3https://satellite6.example.com/api/smart_class_parameters/63/override_values/3https://satellite6.example.com/api/smart_class_parameters/63/override_values/3
4.1.3.1. Modifying a Smart Class Parameter Using an External File Copy linkLink copied to clipboard!
Procedure 4.2. Modifying a Smart Class Parameter Using an External File
- Search for the Puppet Class by name, in this case,
motd
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Each Smart Class Parameter has an ID that is global for the same Satellite instance. Thecontent
parameter of themotd
class hasid=3
in this Satellite Server. Do not confuse this with the Puppet Class ID which appears just before the Puppet Class name. - 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
:curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" -X GET \ -u sat_user:sat_passwd -k \ "https://satellite6.example.com/api/smart_class_parameters/3 \ | python -m json.tool > output_file.json
$ curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" -X GET \ -u sat_user:sat_passwd -k \ "https://satellite6.example.com/api/smart_class_parameters/3 \"https://satellite6.example.com/api/smart_class_parameters/3 \"https://satellite6.example.com/api/smart_class_parameters/3 \ | python -m json.tool > output_file.json
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
. Open the file in an editor and modify the desired values. In this example, we wish to change the content parameter of themotd
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 - Use a PUT command as follows to apply the changes to Satellite Server:
curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" \ -X PUT -u $user:$passwd \ -d @changed_file.json \ -k "https://satellite6.example.com/api/smart_class_parameters/3
$ curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" \ -X PUT -u $user:$passwd \ -d @changed_file.json \ -k "https://satellite6.example.com/api/smart_class_parameters/3-k "https://satellite6.example.com/api/smart_class_parameters/3-k "https://satellite6.example.com/api/smart_class_parameters/3
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.1.4. Applying Errata to a Host or Host Collection Copy linkLink copied to clipboard!
curl
with the PUT command to apply errata to a host, host group, or host collection. The following is the basic syntax of a PUT request:
curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" -X PUT \ -u sat_username:sat_password -k \ -d json-formatted-data https://satellite6.example.com
$ curl -H "Accept:application/json,version=2" \
-H "Content-Type:application/json" -X PUT \
-u sat_username:sat_password -k \
-d json-formatted-data https://satellite6.example.com
https://satellite6.example.com/apidoc/v2.html
) to find a URL to use for applying Errata. You can use the Satellite web UI to help discover the format for the search query. Navigate to host_collection="my-collection"
. This will be used in the example below for Host Collections.
Example 4.4. Applying Errata to a Host
/katello/api/hosts/bulk/install_content
, is used to show the format required for a simple search.
curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" -X PUT \ -u sat_username:sat_password -k \ -d "{\"organization_id\":1,\"included\":{\"search\":\"my-host\"},\"content_type\":\"errata\",\"content\":[\"RHBA-2016:1981\"]}" https://satellite6.example.com/api/v2/hosts/bulk/install_content
$ curl -H "Accept:application/json,version=2" \
-H "Content-Type:application/json" -X PUT \
-u sat_username:sat_password -k \
-d "{\"organization_id\":1,\"included\":{\"search\":\"my-host\"},\"content_type\":\"errata\",\"content\":[\"RHBA-2016:1981\"]}" https://satellite6.example.com/api/v2/hosts/bulk/install_content
Example 4.5. Applying Errata to a Host Collection
host_collection="my-collection"
as seen in the Satellite web UI.
curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" -X PUT \ -u sat_username:sat_password -k \ -d "{\"organization_id\":1,\"included\":{\"search\":\"host_collection=\\\"my-collection\\\"\"},\"content_type\":\"errata\",\"content\":[\"RHBA-2016:1981\"]}" https://satellite6.example.com/api/v2/hosts/bulk/install_content
$ curl -H "Accept:application/json,version=2" \
-H "Content-Type:application/json" -X PUT \
-u sat_username:sat_password -k \
-d "{\"organization_id\":1,\"included\":{\"search\":\"host_collection=\\\"my-collection\\\"\"},\"content_type\":\"errata\",\"content\":[\"RHBA-2016:1981\"]}" https://satellite6.example.com/api/v2/hosts/bulk/install_content