第4章 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/'
/etc/rhsm/rhsm.conf ファイルにある [server] セクションの port エントリーをもとに、API にアクセスする際に必要な、正しいポートを判断することができます。これらの値を使用して、スクリプトを完全に自動化して、使用するポートを検証する必要性をなくすことができます。
4.1. curl を使用した API の例
curl を使用して Satellite API でさまざまなタスクを実行する方法について説明します。
4.1.1. シンプルなクエリーの実行
curl を使用して Satellite デプロイメントの情報を検索する方法を説明します。以下の例には、実際のコマンドと、サンプルの出力、ユーザー名やパスワードの値の例などが含まれます。デプロイメントごとに結果は異なります。また、以下の例では python -m json.tool コマンドを使用して出力をフォーマットしています。
注記
-k (セキュアではありません) オプションを使用して証明書チェックを省略することができます。
-u username:password の形式を使用するか、パスワードを追加していない場合にはコマンドでパスワードの入力が求められます。コマンドの一部としてパスワードを追加すると shell の履歴に残り、セキュリティーリスクとなる可能性があるため、Red Hat ではこれは推奨していません。以下の例では、単純化の目的としてのみ、パスワードをコマンドに含めています。
curl で -s (サイレント) オプションを使用する場合は、進捗バーやエラーメッセージが表示されないので注意してください。
以下は、リソースの一覧を返す基本的なクエリーです。このような要求は、メタデータでラップされたデータ一覧を返しますが、他の要求タイプでは実際のオブジェクトのみを返します。
$ curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/v2/hosts | python -m json.tool
{
"total" => 2,
"subtotal" => 2,
"page" => 1,
"per_page" => 1000,
"search" => nil,
"sort" => {
"by" => nil,
"order" => nil
},
"results" => [
...
}
例4.1 ユーザーの一覧表示
$ curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/users
{
"total": 1,
"subtotal": 1,
"page": 1,
"per_page": 20,
"search": null,
"sort": {
"by": null,
"order": null
},
"results": [{"firstname":"Admin","lastname":"User","mail":"root@example.com","admin":true,"auth_source_id":1,"auth_source_name":"Internal","timezone":null,"locale":null,"last_login_on":"2017-02-08 23:25:51 UTC","created_at":"2017-01-09 12:10:02 UTC","updated_at":"2017-02-08 23:25:51 UTC","id":3,"login":"admin","default_location":null,"locations":[],"default_organization":{"id":1,"name":"Default Organization","title":"Default Organization","description":null},"organizations":[]}]
}
以下のクエリーは、ホスト satellite6.example.com の情報を返します。
$ curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/v2/hosts/satellite6.example.com | python -m json.tool
{
"all_puppetclasses": [],
"architecture_id": 1,
"architecture_name": "x86_64",
"build": false,
"capabilities": [
"build"
],
"certname": "satellite6.example.com",
"comment": null,
"compute_profile_id": null,
...
}
以下のクエリーは、ホスト satellite6.example.com の全ファクトを返します。
$ curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/v2/hosts/satellite6.example.com/facts | python -m json.tool
{
...
"results": {
"satellite6.example.com": {
"augeasversion": "1.0.0",
"bios_release_date": "01/01/2007",
"bios_version": "0.5.1",
"blockdevice_sr0_size": "1073741312",
"facterversion": "1.7.6",
...
}以下のクエリーは、「example」というパターンと一致するホストすべてを返します。
$ curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/v2/hosts?search=example | python -m json.tool
{
...
"results": [
{
"name": "satellite6.example.com",
...
}
],
"search": "example",
...
}以下のクエリーは、「production」環境内の全ホストを返します。
$ curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/v2/hosts?search=environment=production | python -m json.tool
{
...
"results": [
{
"environment_name": "production",
"name": "satellite6.example.com",
...
}
],
"search": "environment=production",
...
}以下のクエリーでは、「RHEV Hypervisor」というモデル名を持つホストすべてが返されます。
$ curl -X GET -s -k -u sat_username:sat_password https://satellite6.example.com/api/v2/hosts?search=model=\"RHEV+Hypervisor\" | python -m json.tool
{
...
"results": [
{
"model_id": 1,
"model_name": "RHEV Hypervisor",
"name": "satellite6.example.com",
...
}
],
"search": "model=\"RHEV Hypervisor\"",
...
}
4.1.2. リソースの作成および変更
Accept:version=2 を使用するのと同じです。URL の指定が優先されます。
$ 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{"name":"i686","id":3,"created_at":"2015-10-29T13:21:09Z","updated_at":"2015-10-29T13:21:09Z","operatingsystems":[],"images":[]}$ curl -X GET -u sat_username:sat_password -k https://satellite6.example.com/api/v2/architectures | python -m json.tool
{
"page": 1,
"per_page": 20,
"results": [
{
"created_at": "2015-04-02T05:29:46Z",
"id": 2,
"name": "i386",
"updated_at": "2015-04-02T05:29:46Z"
},
{
"created_at": "2015-04-02T05:29:46Z",
"id": 1,
"name": "x86_64",
"updated_at": "2015-04-02T05:29:46Z"
},
{
"created_at": "2015-11-04T19:40:15Z",
"id": 3,
"name": "i686",
"updated_at": "2015-11-04T19:40:15Z"
}
],
"search": null,
"sort": {
"by": null,
"order": null
},
"subtotal": 3,
"total": 3
}hammer を使用して結果を検証することも可能です。
$ hammer -u sat_username -p sat_password architecture list ---|------- ID | NAME ---|------- 2 | i386 1 | x86_64 3 | i686 ---|-------
例4.2 新しいユーザーの作成
$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
4.1.2.1. Satellite サーバーへのコンテンツのアップロード
curl を使用して、Satellite サーバーに大容量ファイルをアップロードしてインポートする方法を説明します。このプロセスには 4 つの手順が含まれます。
- アップロード要求を作成します。
- コンテンツをアップロードします。
- コンテンツをインポートします。
- アップロード要求を削除します。
手順4.1 Satellite サーバーへのコンテンツのアップロード
- アップロード要求を作成します。デプロイメントに適したサンプルパラメーターを変更するようにしてください。
$ 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このコマンドは、以下のようなupload_idを返します。{"upload_id":"0be156b1-f373-4cad-89d0-924f8f4491d2","_href":"/pulp/api/v2/content/uploads/0be156b1-f373-4cad-89d0-924f8f4491d2/"} - コンテンツをアップロードします。データのアップロード時には、正しい MIME タイプを使用していることを確認します。Satellite 6 に対する要求にはほぼ、「application/json」の MIME タイプが使用されます。
upload_idと MIME タイプ、他のパラメーターを組み合わせてコンテンツをアップロードします。$ curl -H "Accept:application/json,version=2" \ -H "Content-Type:multipart/form-data" \ -X PUT \ -u sat_username:sat_password \ -k --data-urlencode "content@/home/sat6user/rpmbuild/RPMS/noarch/python-scripttest-1.1.1-1.fc21.noarch.rpm" \ --data-urlencode offset=0 \ https://satellite6.example.com/katello/api/repositories/3/content_uploads/0be156b1-f373-4cad-89d0-924f8f4491d2 - Satellite サーバーにコンテンツをアップロードした後に、適切なリポジトリーにそのコンテンツをインポートする必要があります。この手順を完了するまで、Satellite サーバーではこの新しいコンテンツは認識されません。
$ curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" \ -X PUT \ -u sat_username:sat_password \ -k -d "{\"upload_ids\":[\"0be156b1-f373-4cad-89d0-924f8f4491d2\"]}" \ https://satellite6.example.com/katello/api/repositories/3/import_uploads - コンテンツのアップロードおよびインポートが正常に完了したら、アップロード要求を削除することができます。削除することで、アップロード中に使用した一時的なディスク領域を解放することができます。
$ 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
例4.3 30 MB よりも大きいコンテンツのアップロード
- サンプルモジュールをダウンロードします。
$ 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
50,000 バイトのチャンクにモジュールを分割します。$ split --bytes 50000 --numeric-suffixes --suffix-length=1 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
- 新規アップロード要求を作成します (これは Satellite サーバーでの
catと同じです)。$ curl -H "Accept:application/json,version=2" \ -H "Content-Type:application/json" \ -X POST \ -u sat_username:sat_password -k -d "{}" \ https://ibm-vm01.example.com/katello/api/repositories/2/content_uploads上記のコマンドはアップロード ID を返します。{"upload_id":"9585528f-07ad-4bb1-9c80-ccece249b2b7","_href":"/pulp/api/v2/content/uploads/9585528f-07ad-4bb1-9c80-ccece249b2b7/"} - 手順 1 で作成したファイルのチャンクをアップロードします。以下の例で
offsetパラメーターを使用して、ファイルサイズと関連付けている点に注意してください。$ curl -H "Accept:application/json,version=2" \ -H "Content-Type:multipart/form-data" \ -X PUT \ -u sat_username:sat_password \ -k --data-urlencode "content@foreman_module.0" \ --data-urlencode offset=0 \ 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:multipart/form-data" \ -X PUT \ -u sat_username:sat_password \ -k --data-urlencode "content@foreman_module.1" \ --data-urlencode offset=50000 \ 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 PUT \ -u sat_username:sat_password \ -k -d "{\"upload_ids\":[\"9585528f-07ad-4bb1-9c80-ccece249b2b7\"]}" \ https://ibm-vm01.example.com/katello/api/repositories/2/import_uploads - アップロード要求を削除します。
$ 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 - Satellite サーバーにログインして、ファイルが正しく転送されたかどうかを確認します。
$ 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
ファイルを比較します。$ cmp /var/lib/pulp/content/puppet_module/theforeman-foreman-5.0.1.tar.gz theforeman-foreman-5.0.1.tar.gz
$ echo $? 0
4.1.3. スマートクラスのオーバーライド
https://satellite6.example.com/apidoc/v2/smart_class_parameters/update.html にある同梱の API リファレンスで確認できます。
GET /api/smart_class_parameters のようになります。curl を使用する場合はコマンドは以下のようになります。
$5 など、Puppet クラス ID が分かる場合には、以下のように範囲を絞り込むことができます。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 https://satellite6.example.com/api/puppetclasses/5/smart_class_parameters
puppetclass_name と key の 2 つで、特定のパラメーターの検索が可能になります。たとえば、-d, --data オプションを使用して URL のエンコードデータを渡すことができます。
$ 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 です。curl を使用する場合はコマンドは以下のようになります。
$これで、PUT 呼び出しでパラメーターの値をオーバーライドすることができます。curl -X GET -s -k -u sat_username:sat_password \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 \-d '{"smart_class_parameter":{"override":true}}' \https://satellite6.example.com/api/smart_class_parameters/63
$API 呼び出しの全パラメーターに関する情報は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
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
4.1.3.1. 外部ファイルを使用したスマートクラスパラメーターの変更
手順4.2 外部ファイルを使用したスマートクラスパラメーターの変更
- 今回は
motdという名前で Puppet クラスを検索します。$
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?search=puppetclass_name=motd” \| python -m json.tool{ "page": 1, "per_page": 20, "results": [ { "avoid_duplicates": false, "created_at": "2017-02-06 12:37:48 UTC", "default_value": "", "description": "", "hidden_value": "*****", "hidden_value?": false, "id": 3, "merge_default": false, "merge_overrides": false, "override": false, "override_value_order": "fqdn\nhostgroup\nos\ndomain", "override_values_count": 0, "parameter": "content", "parameter_type": "string", "puppetclass_id": 3, "puppetclass_name": "motd", "required": false, "updated_at": "2017-02-07 13:08:42 UTC", "use_puppet_default": false, "validator_rule": null, "validator_type": "" }, { "avoid_duplicates": false, "created_at": "2017-02-06 12:37:48 UTC", "default_value": true, "description": "", "hidden_value": "*****", "hidden_value?": false, "id": 1, "merge_default": false, "merge_overrides": false, "override": false, "override_value_order": "fqdn\nhostgroup\nos\ndomain", "override_values_count": 0,"parameter": "dynamic_motd", "parameter_type": "boolean", "puppetclass_id": 3, "puppetclass_name": "motd", "required": false, "updated_at": "2017-02-06 15:21:06 UTC", "use_puppet_default": null, "validator_rule": null, "validator_type": null }, { "avoid_duplicates": false, "created_at": "2017-02-06 12:37:48 UTC", "default_value": "", "description": "", "hidden_value": "*****", "hidden_value?": false, "id": 2, "merge_default": false, "merge_overrides": false, "override": false, "override_value_order": "fqdn\nhostgroup\nos\ndomain", "override_values_count": 0, "parameter": "template", "parameter_type": "string", "puppetclass_id": 3, "puppetclass_name": "motd", "required": false, "updated_at": "2017-02-06 15:21:06 UTC", "use_puppet_default": null, "validator_rule": null, "validator_type": null } ], "search": "puppetclass_name=motd", "sort": { "by": null, "order": null }, "subtotal": 3, "total": 66 }スマートクラスパラメーターはそれぞれ、同じ Satellite インスタンスにおいてグローバルな ID となっています。motdクラスのcontentパラメーターは、この Satellite サーバーではid=3です。この ID は、Puppet クラス名の前に表示される Puppet クラス ID と間違わないようにしてください。 - パラメーター ID
3を使用して、motdパラメーター固有の情報を取得して、出力を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 - 以前の手順で作成したファイルを
changed_file.jsonという新しいファイルにコピーして編集します。エディターでファイルを開き、任意の値に変更します。以下の例では、motdモジュールのコンテンツパラメーターを変更します。この際、overrideオプションをfalseからtrueに変更する必要があります。{ "avoid_duplicates": false, "created_at": "2017-02-06 12:37:48 UTC", # This line must be removed. "default_value": "", # A new value should be supplied here. "description": "", "hidden_value": "*****", "hidden_value?": false, "id": 3, "merge_default": false, "merge_overrides": false, "override": false, # The override value must be set totrue. "override_value_order": "fqdn\nhostgroup\nos\ndomain", "override_values": [], # This line must be removed. "override_values_count": 0, "parameter": "content", "parameter_type": "string", "puppetclass_id": 3, "puppetclass_name": "motd", "required": false, "updated_at": "2017-02-07 11:56:55 UTC", # This line must be removed. "use_puppet_default": false, "validator_rule": null, "validator_type": "" } - ファイルの編集後に、以下のようになっていることを確認して、変更を保存します。
{ "avoid_duplicates": false, "default_value": "No Unauthorized Access Allowed", "description": "", "hidden_value": "*****", "hidden_value?": false, "id": 3, "merge_default": false, "merge_overrides": false, "override": true, "override_value_order": "fqdn\nhostgroup\nos\ndomain", "override_values_count": 0, "parameter": "content", "parameter_type": "string", "puppetclass_id": 3, "puppetclass_name": "motd", "required": false, "use_puppet_default": false, "validator_rule": null, "validator_type": "" } - 以下のように PUT コマンドを使用して、Satellite サーバーに変更を適用します。
$
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
4.1.4. エラータのホストまたはホストコレクションへの適用
curl を使用して、エラータをホスト、ホストグループまたはホストコレクションに適用することができます。以下は、PUT 要求の基本的な構文です。
$ 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) を参照してエラータの適用に使用する URL を検索します。Satellite Web UI を使用すると、検索クエリー形式の検索に役立ちます。 → の順に移動して、ホストコレクションを選択します。 → に移動して、検索クエリーボックスに注目します。たとえば my-collection と呼ばれるホストコレクションでは、検索ボックスに host_collection="my-collection" が含まれます。以下のホストコレクションの例では、これを使用します。
例4.4 ホストへのエラータの適用
/katello/api/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
例4.5 ホストコレクションへのエラータの適用
host_collection="my-collection" を渡すのに必要なエスケープレベルに注目してください。
$ 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

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.