第5章 API チートシート
Red Hat Satellite API を使用してさまざまなタスクを実行する方法は、次の例を参照してください。ポート 443 の HTTPS 経由で Satellite Server 上の API を使用できます。
たとえば、Ruby では、Satellite Server の URL を次のように指定できます。
url = 'https://satellite.example.com/api/v2/'
katello_url = 'https://satellite.example.com/katello/api/v2/'
これらの値を使用してスクリプトを完全に自動化し、使用するポートを検証する必要性をなくします。
次の例では、API リクエストの送信に curl を使用します。詳細は、「curl での API の呼び出し」 を参照してください。
5.1. ホストの使用方法 リンクのコピーリンクがクリップボードにコピーされました!
5.1.1. ホストのリスト表示 リンクのコピーリンクがクリップボードにコピーされました!
この例では、登録されたホストの一覧を返します。
API 要求
$ curl \
--request GET \
--user My_User_Name:My_Password \
https://satellite.example.com/api/v2/hosts \
| python3 -m json.tool
API 応答
{
...
"total" => 2,
"subtotal" => 2,
"page" => 1,
"per_page" => 1000,
"search" => nil,
"sort" => {
"by" => nil,
"order" => nil
},
"results" => [
...
}
5.1.2. ホストの情報要求 リンクのコピーリンクがクリップボードにコピーされました!
この要求は、satellite.example.com ホストの情報を返します。
API 要求
$ curl \
--request GET \
--user My_User_Name:My_Password \
https://satellite.example.com/api/v2/hosts/satellite.example.com \
| python3 -m json.tool
API 応答
{
"all_puppetclasses": [],
"architecture_id": 1,
"architecture_name": "x86_64",
"build": false,
"capabilities": [
"build"
],
"certname": "satellite.example.com",
"comment": null,
"compute_profile_id": null,
...
}
5.1.3. ホストのファクト表示 リンクのコピーリンクがクリップボードにコピーされました!
この要求は、satellite.example.com ホストの全ファクトを返します。
API 要求
$ 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 応答
{
...
"results": {
"satellite.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",
...
}
5.1.4. パターンが一致するホストの検索 リンクのコピーリンクがクリップボードにコピーされました!
以下のクエリーは、"example" というパターンと一致するホストをすべて返します。
API 要求
$ curl \
--request GET \
--user My_User_Name:My_Password \
https://satellite.example.com/api/v2/hosts?search=example \
| python3 -m json.tool
API 応答
{
...
"results": [
{
"name": "satellite.example.com",
...
}
],
"search": "example",
...
}
5.1.5. 環境でのホストの検索 リンクのコピーリンクがクリップボードにコピーされました!
以下のクエリーは、production 環境内の全ホストを返します。
API 要求
$ curl \
--request GET \
--user My_User_Name:My_Password \
https://satellite.example.com/api/v2/hosts?search=environment=production \
| python3 -m json.tool
API 応答
{
...
"results": [
{
"environment_name": "production",
"name": "satellite.example.com",
...
}
],
"search": "environment=production",
...
}
5.1.6. 特定のファクト値を含むホストの検索 リンクのコピーリンクがクリップボードにコピーされました!
このクエリーは、ホストグループが My Host Group のホストをすべて返します。
API 要求
$ curl \
--request GET \
--user My_User_Name:My_Password \
https://satellite.example.com/api/v2/hosts?search=hostgroup=%22My+Host+Group%22 \
| python3 -m json.tool
API 応答
{
...
"results": [
{
...
"hostgroup_id": 1,
"hostgroup_name": "My Host Group",
"name": "my-host.example.com",
...
}
],
"search": "hostgroup=\"My Host Group\"",
...
}
5.1.7. ホストの削除 リンクのコピーリンクがクリップボードにコピーされました!
この要求は、名前が host1.example.com のホストを削除します。
API 要求
$ curl \
--request DELETE \
--user My_User_Name:My_Password \
https://satellite.example.com/api/v2/hosts/host1.example.com \
| python3 -m json.tool
5.1.8. 全ホストのブートディスクイメージのダウンロード リンクのコピーリンクがクリップボードにコピーされました!
以下の要求では、ホストの完全な起動ディスクイメージを ID を使用してダウンロードします。
API 要求
$ curl \
--request GET \
--user My_User_Name:My_Password \
--output My_Image.iso \
https://satellite.example.com/api/bootdisk/hosts/host_ID?full=true