第5章 Red Hat Satellite API の使用

本章では、Red Hat Satellite API を使用して各種タスクを実行する方法をさまざまな例を挙げて説明します。Satellite Server で API を使用するには、ポート 443 の HTTPS を、Capsule Server で API を使用するには、ポート 8443 の HTTPS を使用します。

スクリプト自体で、異なるポートの要件に対応することができます。たとえば、Ruby では Satellite および Capsule の URL を以下のように指定することができます。

url = 'https://satellite.example.com/api/v2/'
capsule_url = 'https://capsule.example.com:8443/api/v2/'
katello_url = 'https://satellite.example.com/katello/api/v2/'

ホストが Satellite Server または Capsule Server にサブスクライブしている場合は、/etc/rhsm/rhsm.conf[server] セクションのポートエントリーをもとに、API へのアクセスに必要な、正しいポートを判断できます。これらの値を使用してスクリプトを完全に自動化し、使用するポートを検証する必要性をなくします。

本章では、curl を使用して API 要求を送信します。詳細は、「curl を使用した API 要求」 を参照してください。

本章の例では、Python json.tool モジュールを使用して出力をフォーマットしています。

5.1. ホストの使用方法

ホストのリスト

以下の例では、Satellite ホストのリストを返します。

要求例:

$ curl -request GET --insecure --user sat_username:sat_password \
https://satellite.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" => [
      ...
}

ホストの情報要求

この要求は、satellite.example.com ホストの情報を返します。

要求例:

$  curl --request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/api/v2/hosts/satellite.example.com \
| python -m json.tool

応答例:

{
    "all_puppetclasses": [],
    "architecture_id": 1,
    "architecture_name": "x86_64",
    "build": false,
    "capabilities": [
        "build"
    ],
    "certname": "satellite.example.com",
    "comment": null,
    "compute_profile_id": null,
    ...
}

ホストのファクト表示

この要求は、satellite.example.com ホストの全ファクトを返します。

要求例:

$ curl --request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/api/v2/hosts/satellite.example.com/facts \
| python -m json.tool

応答例:

{
    ...
    "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",
            ...
}

パターンが一致するホストの検索

以下のクエリーは、example というパターンと一致するホストをすべて返します。

要求例:

$ curl --request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/api/v2/hosts?search=example \
| python -m json.tool

応答例:

{
    ...
    "results": [
        {
            "name": "satellite.example.com",
            ...
        }
    ],
    "search": "example",
    ...
}

環境でのホストの検索

以下のクエリーは、production 環境内の全ホストを返します。

要求例:

$ curl --request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/api/v2/hosts?search=environment=production \
| python -m json.tool

応答例:

{
    ...
    "results": [
        {
            "environment_name": "production",
            "name": "satellite.example.com",
            ...
        }
    ],
    "search": "environment=production",
    ...
}

特定のファクト値を含むホストの検索

以下のクエリーでは、RHEV Hypervisor というモデル名のホストがすべて返されます。

要求例:

$ curl --request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/api/v2/hosts?search=model=\"RHEV+Hypervisor\" \
| python -m json.tool

応答例:

{
    ...
    "results": [
        {
            "model_id": 1,
            "model_name": "RHEV Hypervisor",
            "name": "satellite.example.com",
            ...
        }
    ],
    "search": "model=\"RHEV Hypervisor\"",
    ...
}

ホストの削除

この要求は、名前が host1.example.com のホストを削除します。

要求例:

$ curl --request DELETE --insecure --user sat_username:sat_password \
https://satellite.example.com/api/v2/hosts/host1.example.com \
| python -m json.tool

完全な起動ディスクイメージのダウンロード

以下の要求では、ホストの完全な起動ディスクイメージを ID を使用してダウンロードします。

要求例:

$ curl --request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/bootdisk/api/hosts/host_ID?full=true \
--output image.iso