第 4 章 过渡到 Satellite 6 API


Red Hat Satellite 5 和 Satellite 6 之间的一个区别是 API。Satellite 5 使用基于 XMLRPC 的 API。Satellite 6 使用基于 REST 的 API。这种基本差异要求任何已经与 Satellite 5 API 集成的现有脚本或工具都必须被检查,并在与 Satellite 6 REST API 搭配使用前至少进行部分重写。

本节提供了如何在每个产品中实现相同的用例的一些比较。这不是设计为任何特定编程语言的教程。既不是通过 HTTPS 保护的脚本。它们为维护 Satellite 5 API 脚本的用户提供了一个起点,以开始转换到 Satellite 6 API。

重要

Satellite 6 转换工具不会将 Satellite 5 API 或脚本转换为 Satellite 6。使用这个部分作为开始您自己的转换过程。

更多信息

您可以在 API 指南中找到更多 API 文档,并位于您自己的 Satellite 服务器上的以下位置:

4.1. API 脚本示例

本节中的示例涵盖了以下内容:

  1. Red Hat Satellite 中的系统和主机

    1. 身份验证并请求登录的用户使用的系统(Satellite 5)或主机(Satellite 6)列表。
  2. 用户和角色

    1. 身份验证并请求对登录的用户可见的所有用户列表。
    2. 如果 example 用户存在,则删除它。
    3. 创建一个新的 示例 用户,并确保其角色设置为 Administrator

本节提供了五种不同方法以实现相同的结果:两个用于 Satellite 5 的示例和 Satellite 6 的三个示例。

  • Satellite 5 Python 脚本
  • Satellite 6 Python 脚本
  • Satellite 6 Ruby 脚本
  • Satellite 5 spacecmd 示例
  • Satellite 6 hammer 示例
注意

spacecmd 命令不是 Satellite 5.6 中支持的功能。Satellite 5.7 支持它,并在此处显示保证完整性。

提供总计 10 个示例。第一个示例涵盖了列出系统和主机的简单用例,以及涵盖用户和角色的复杂示例。

4.1.1. 列出系统和主机

本节中的示例描述了列出登录的用户可用的系统和主机的不同方法。

使用 Python 列出 Satellite 5 上的可用系统

本例使用 Python 脚本连接到 Satellite 5,身份验证并检索可用系统列表。

#!/usr/bin/python
import xmlrpclib

# Define Satellite location and login details
SATELLITE_URL = "http://localhost/rpc/api"
SATELLITE_LOGIN = "admin"
SATELLITE_PASSWORD = "password"

client = xmlrpclib.Server(SATELLITE_URL, verbose=0)

# Authenticate and get session key
key = client.auth.login(SATELLITE_LOGIN, SATELLITE_PASSWORD)

# Get list of systems available to user
list = client.system.listSystems(key)
for system in list:
   print system.get('id')
   print system.get('name')

# Logout
client.auth.logout(key)
Copy to Clipboard Toggle word wrap

使用 Python 列出 Satellite 6 上的可用主机

本例使用 Python 脚本连接到 Satellite 6,身份验证并检索可用主机的列表。

#!/usr/bin/python
import json
import requests

# Define Satellite location and login details
SAT_API = "https://localhost/api/v2/"
USERNAME = "admin"
PASSWORD = "changeme"
SSL_VERIFY = False

def get_json(location):
    """
    Performs a GET using the passed URL location
    """

    r = requests.get(location, auth=(USERNAME, PASSWORD), verify=SSL_VERIFY)

    return r.json()

def main():
    # List all hosts available to the user
    hosts = get_json(SAT_API + "hosts/")

    # Pretty Print the returned JSON of Hosts
    print json.dumps(hosts, sort_keys=True, indent=4)

if __name__ == "__main__":

    main()
Copy to Clipboard Toggle word wrap

使用 Ruby 列出 Satellite 6 上的可用主机

本例使用 Ruby 脚本连接到 Satellite 6,身份验证并检索可用主机列表。

#!/usr/bin/env ruby
require 'json'
require 'rest-client'

url = 'https://localhost/api/v2/'
$username = 'admin'
$password = 'changeme'

def get_json(location)
    response = RestClient::Request.new(
        :method => :get,
        :url => location,
        :user => $username,
        :password => $password,
        :headers => { :accept => :json,
        :content_type => :json }
    ).execute
    results = JSON.parse(response.to_str)
end

hosts = get_json(url+"hosts/")
#puts JSON.pretty_generate(hosts)

puts "Hosts within Satellite are:"
hosts['results'].each do |name|
      puts name['name']
end

exit()
Copy to Clipboard Toggle word wrap

使用命令行列出 Satellite 5 上的可用系统

使用以下命令列出 Satellite 5 上的可用系统:

# spacecmd -u username -p password system_list
Copy to Clipboard Toggle word wrap

会话示例可能如下所示:

# spacecmd -u admin -p password system_list

INFO: Spacewalk Username: admin
INFO: Connected to https://localhost/rpc/api as admin
test_02.example.com
Copy to Clipboard Toggle word wrap

使用命令行列出 Satellite 6 上的可用主机

使用以下命令列出 Satellite 6 上的可用主机:

# hammer host list
Copy to Clipboard Toggle word wrap

会话示例可能如下所示:

# hammer host list

[Foreman] password for admin:
---|-----------------|-------------------|------------|--------------|------------------
ID | NAME            | OPERATING SYSTEM  | HOST GROUP | IP           | MAC
---|-----------------|-------------------|------------|--------------|------------------
1  | test.example.com | RHEL Server 6.5  |            | 10.34.34.235 | e4:1f:13:6b:ed:0c
Copy to Clipboard Toggle word wrap

4.1.2. 删除和创建用户

本节中的示例描述了查找、创建和删除用户的不同方法。

使用 Python 在 Satellite 5 上管理用户

本例使用 Python 脚本连接 Satellite 5 服务器并进行身份验证。然后,它会进入 搜索特定用户(),以在该用户存在时删除该用户,然后使用管理员权限重新创建该用户。

#!/usr/bin/python
import xmlrpclib

# Define Satellite location and login details
SATELLITE_URL = "http://localhost/rpc/api"
SATELLITE_LOGIN = "admin"
SATELLITE_PASSWORD = "password"

client = xmlrpclib.Server(SATELLITE_URL, verbose=0)

# Authenticate and get session key
key = client.auth.login(SATELLITE_LOGIN, SATELLITE_PASSWORD)

# Get list of users
list = client.user.list_users(key)
print "Existing users in Satellite:"
for user in list:
   print user.get('login')

# Look for user example and if found, delete the user
for user in list:
   if user.get('login') == 'example':
       deleteuser = client.user.delete(key, 'example')
       if deleteuser == 1:
           print "User example deleted"

# Create a user called example
createuser = client.user.create(key, 'example', 'password', 'Example', 'User', "root@localhost")
if createuser == 1:
    print "User example created"
    # Admin Org Admin role to the example user
    adminrole = client.user.addRole(key, 'example', 'org_admin')
    if adminrole == 1:
        print "Made example an Org Admin"

# Logout
client.auth.logout(key)
Copy to Clipboard Toggle word wrap

使用 Python 在 Satellite 6 上管理用户

本例执行与上例相同的任务。也就是说,它使用 Python 脚本连接到 Satellite 6 服务器并验证,搜索特定用户,删除该用户(如果存在),然后使用管理员权限重新创建它。

#!/usr/bin/python
import json
import requests

# Define Satellite location and login details
SAT_API = "https://localhost/api/v2/"
POST_HEADERS = {'content-type': 'application/json'}
USERNAME = "admin"
PASSWORD = "changeme"
SSL_VERIFY = False

def get_json(location):
    """
    Performs a GET using the passed URL location
    """
    r = requests.get(location, auth=(USERNAME, PASSWORD), verify=SSL_VERIFY)

    return r.json()

def post_json(location, json_data):
    """
    Performs a POST and passes the data to the URL location
    """
    result = requests.post(
        location,
        data=json_data,
        auth=(USERNAME, PASSWORD),
        verify=SSL_VERIFY,
        headers=POST_HEADERS)

    return result.json()

def delete_json(location):
    """
    Performs a DELETE and passes the id to the URL location
    """
    result = requests.delete(
        location,
        auth=(USERNAME, PASSWORD),
        verify=SSL_VERIFY)

    return result.json()

def main():
    # List all users within the Satellite
    users = get_json(SAT_API + "users/")

    #print json.dumps(users, indent=4)
    print "Users known are:"
    for login in users['results']:
        print login['login']

    # Look for user example and if found, delete the user
    for delete in users['results']:
        if delete['login'] == 'example':
            id = delete ['id']
            id = str(id)

            delete = delete_json(SAT_API + "/users/" + id)
            #print json.dumps(delete, indent=4)
            print "User example deleted"

    # Create a user called example as admin role
    createuser = post_json(SAT_API + "/users/", json.dumps({ "mail": "root@localhost", "firstname": "Example", "lastname": "User", "login": "example", "password": "redhat", "admin": 'true', "auth_source_id": 1 }))
    #print json.dumps(createuser, indent=4)
    print "Admin user example created"

if __name__ == "__main__":
    main()
Copy to Clipboard Toggle word wrap

使用 Ruby 在 Satellite 6 上管理用户

这个示例使用 Ruby 执行与上例相同的任务。

#!/usr/bin/env ruby
require 'json'
require 'rest-client'

url = 'https://localhost/api/v2/'
$username = 'admin'
$password = 'changeme'

def get_json(location)
    response = RestClient::Request.new(
        :method => :get,
        :url => location,
        :user => $username,
        :password => $password,
        :headers => { :accept => :json,
        :content_type => :json }
    ).execute
    results = JSON.parse(response.to_str)
end

def post_json(location, json_data)
    response = RestClient::Request.new(
        :method => :post,
        :url => location,
        :user => $username,
        :password => $password,
        :headers => { :accept => :json,
        :content_type => :json},
        :payload => json_data
    ).execute
    results = JSON.parse(response.to_str)
end

def delete_json(location)
    response = RestClient::Request.new(
        :method => :delete,
        :url => location,
        :user => $username,
        :password => $password,
        :headers => { :accept => :json,
        :content_type => :json }
    ).execute
    results = JSON.parse(response.to_str)
end

# List all users within the Satellite
users = get_json(url+"users/")

#puts JSON.pretty_generate(users)
puts "Users known are:"
users['results'].each do |name|
    puts name['login']
end

# Look for user example and if found, delete the user
users['results'].each do |name|
    if name['login'] == 'example'
        id = name['id']
        delete = delete_json(url+"users/"+id.to_s)
        #puts JSON.pretty_generate(delete)
        puts "User example deleted"
    end
end

# Create a user called example as admin role
data = JSON.generate({ :mail => "root@localhost", :firstname => "Example", :lastname => "User", :login => "example", :password => "password", :admin => 'true', :auth_source_id => 1})
createuser = post_json(url+"users/", data)

#puts JSON.pretty_generate(createuser)
puts "Admin user example created"

exit()
Copy to Clipboard Toggle word wrap

使用命令行在 Satellite 5 上管理用户

这个示例使用 spacecmd 命令执行与上例相同的任务。

# spacecmd -u admin -p password user_list
# spacecmd -u admin -p password user_delete example
# spacecmd -u admin -p password user_create
# spacecmd -u admin -p password user_addrole example org_admin
Copy to Clipboard Toggle word wrap

会话示例可能如下所示:

# spacecmd -u admin -p password user_list
INFO: Spacewalk Username: admin
INFO: Connected to https://localhost/rpc/api as admin
admin
example

# spacecmd -u admin -p password user_delete example
INFO: Spacewalk Username: admin
INFO: Connected to https://localhost/rpc/api as admin

Delete this user [y/N]: y

# spacecmd -u admin -p password user_list
INFO: Spacewalk Username: admin
INFO: Connected to https://localhost/rpc/api as admin
admin

# spacecmd -u admin -p password user_create
INFO: Spacewalk Username: admin
INFO: Connected to https://localhost/rpc/api as admin
Username: example
First Name: Example
Last Name: User
Email: root@localhost
PAM Authentication [y/N]: n
Password:
Repeat Password:

# spacecmd -u admin -p password user_list
INFO: Spacewalk Username: admin
INFO: Connected to https://localhost/rpc/api as admin
admin
example

# spacecmd -u admin -p password user_addrole example org_admin
INFO: Spacewalk Username: admin
INFO: Connected to https://localhost/rpc/api as admin

# spacecmd -u admin -p password user_details example
INFO: Spacewalk Username: admin
INFO: Connected to https://localhost/rpc/api as admin
Username:      example
First Name:    Example
Last Name:     User
Email Address: root@localhost
Organization:  MY ORG
Last Login:
Created:       8/19/14 8:42:52 AM EDT
Enabled:       True

Roles
-----
activation_key_admin
channel_admin
config_admin
monitoring_admin
org_admin
system_group_admin
Copy to Clipboard Toggle word wrap

使用命令行在 Satellite 6 上管理用户

本例使用 hammer 命令执行与上一示例相同的任务。

# hammer shell
> user list
> user delete --id 4 --login example
> user create --admin true --firstname Example --lastname User --login example --mail root@localhost --password redhat --auth-source-id 1
Copy to Clipboard Toggle word wrap

会话示例可能如下所示:

# hammer shell
[Foreman] password for admin:
Welcome to the hammer interactive shell
Type 'help' for usage information

hammer> user list
---|---------|--------------|---------------
ID | LOGIN   | NAME         | EMAIL
---|---------|--------------|---------------
4  | example | Example User | root@localhost
3  | admin   | Admin User   | root@localhost
---|---------|--------------|---------------

hammer> user delete --id 4 --login example
User deleted
hammer> user list
---|-------|------------|---------------
ID | LOGIN | NAME       | EMAIL
---|-------|------------|---------------
3  | admin | Admin User | root@localhost
---|-------|------------|---------------

hammer> user create --admin true --firstname Example --lastname User --login example --mail root@localhost --password redhat --auth-source-id 1
User created
hammer> user list
---|----------|--------------|---------------
ID | LOGIN    | NAME         | EMAIL
---|----------|--------------|---------------
3  | admin    | Admin User   | root@localhost
5  | example  | Example User | root@localhost
---|----------|--------------|---------------
hammer>
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat