第 5 章 使用 Red Hat Satellite API

本章提供了一些示例,了解如何使用 Red Hat Satellite API 执行不同的任务。您可以在端口 443 上通过 HTTPS 在 Satellite 服务器上使用 API,或通过端口 8443 上的 HTTPS 在 Capsule 服务器上使用 API。

您可以在脚本本身内处理这些不同的端口要求。例如,在 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 服务器或 Capsule 服务器的主机,您可以在 [server] 部分的端口条目中确定从 /etc/rhsm/rhsm.conf 文件访问 API 所需的正确端口。您可以使用这些值来完全自动化脚本,删除任何需要验证要使用的端口。

本章使用 curl 发送 API 请求。更多信息请参阅 第 4.1 节 “使用 curl 的 API 请求”

本章中的示例使用 Python json.tool 模块格式化输出。

5.1. 使用主机

注意

以下示例请求使用 python3 来格式化 Satellite 服务器中的 respone。在 RHEL 7 和一些较旧的系统中,您必须使用 python 而不是 python3

列出主机

本例返回 Satellite 主机的列表。

请求示例:

$ curl -request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/api/v2/hosts | python3 -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 \
| python3 -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 \
| python3 -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 \
| python3 -m json.tool

响应示例:

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

搜索环境中的主机

此查询会返回生产环境中的所有主机。

请求示例:

$ curl --request GET --insecure --user sat_username:sat_password \
https://satellite.example.com/api/v2/hosts?search=environment=production \
| python3 -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\" \
| python3 -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 \
| python3 -m json.tool

下载完整引导磁盘镜像

此请求通过其 ID 为主机下载完整的引导磁盘镜像。

请求示例:

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