使用openstack的PythonSDK获取主机和镜像数据.md
概述
众所周知,openstack是由Python开发,因此专门给Python提供了SDK,非常方便。
安装
在使用之前,需要使用pip安装openstacksdk
pip install openstacksdk
配置
OpenstackSDK可以直接在连接时传入认证数据,也可以用配置文件提供认证数据,我们这里使用的是配置文件方式,在Python文件同目录下准备clouds.yaml文件,内容如下
clouds:
test_cloud:
region_name: RegionOne
auth:
project_domain_name: default
user_domain_name: default
project_name: admin
username: admin
password: WoShiMiMaYa
auth_url: http://10.255.255.250:5000/v3
clouds.yaml文件的默认查找顺序是:
本目录(Python运行文件的目录)
~/.config/openstack/目录
/etc/openstack/目录
代码
然后我们可以来编写Python文件了,示例如下
import openstack
#定义连接函数,从配置文件加载连接信息
def create_connection_from_config():
return openstack.connect(cloud='test_cloud')
#创建连接
conn = create_connection_from_config()
#列出所有主机
for x in conn.compute.servers():
print(x)
#列出所有镜像
for image in conn.compute.images():
print(dict(image)['name'])
输出结果
是不是很简单?有了SDK就是这么简单,原数据输出如下
openstack.compute.v2.server.ServerDetail(OS-EXT-STS:task_state=None, addresses={'LB-API-NET': [{'OS-EXT-IPS-MAC:mac_addr': 'fa:16:3e:3d:c9:d1', 'version': 4, 'addr': '10.127.99.14', 'OS-EXT-IPS:type': 'fixed'}], 'my_stack': [{'OS-EXT-IPS-MAC:mac_addr': 'fa:16:3e:cc:b9:72', 'version': 4, 'addr': '10.127.64.7', 'OS-EXT-IPS:type': 'fixed'}]}, links=[{'href': 'http://10.127.74.200:8774/v2.1/75072095d14348eab4a247b6ec9e7dc1/servers/be9e5cb4-7f96-411a-b512-f5314cac6433', 'rel': 'self'}, {'href': 'http://10.127.74.200:8774/75072095d14348eab4a247b6ec9e7dc1/servers/be9e5cb4-7f96-411a-b512-f5314cac6433', 'rel': 'bookmark'}], image={'id': '8ae1528f-8e83-4bf7-a0c5-92911be91aa3', 'links': [{'href': 'http://10.127.74.200:8774/75072095d14348eab4a247b6ec9e7dc1/images/8ae1528f-8e83-4bf7-a0c5-92911be91aa3', 'rel': 'bookmark'}]}, OS-EXT-STS:vm_state=active, OS-EXT-SRV-ATTR:instance_name=instance-00000391, OS-SRV-USG:launched_at=2018-03-13T04:10:49.000000, flavor={'id': '83d80d5a-474c-44aa-8155-040d26872af6', 'links': [{'href': 'http://10.127.74.200:8774/75072095d14348eab4a247b6ec9e7dc1/flavors/83d80d5a-474c-44aa-8155-040d26872af6', 'rel': 'bookmark'}]}, id=be9e5cb4-7f96-411a-b512-f5314cac6433, security_groups=[{'name': 'lb-d0c0cee4-c145-4b8e-9f41-8cd152593aa1'}, {'name': 'octavia'}], user_id=c121fb9a98dd445394ba44fe65e49539, OS-DCF:diskConfig=MANUAL, accessIPv4=, accessIPv6=, progress=0, OS-EXT-STS:power_state=1, OS-EXT-AZ:availability_zone=nova, metadata={}, status=ACTIVE, updated=2018-03-13T04:10:49Z, hostId=daf800bd79434b6dd4e712ce2e31ccf6376db7b1a08728e4535d835b, OS-SRV-USG:terminated_at=None, key_name=octavia_ssh_key, OS-EXT-SRV-ATTR:hypervisor_hostname=compute04, name=amphora-e3e31ba0-4e62-4e4f-9df1-a4af538082d5, created=2017-12-20T09:24:26Z, tenant_id=75072095d14348eab4a247b6ec9e7dc1, os-extended-volumes:volumes_attached=[], config_drive=True)
这个格式我也没搞明白是啥,直接强制转换成dict字典,然后取出响应的值即可,如下
{'LB-API-NET': [{'OS-EXT-IPS-MAC:mac_addr': 'fa:16:3e:3d:c9:d1', 'version': 4, 'addr': '10.127.99.14', 'OS-EXT-IPS:type': 'fixed'}], 'IPCPU-OL': [{'OS-EXT-IPS-MAC:mac_addr': 'fa:16:3e:cc:b9:72', 'version': 4, 'addr': '10.127.64.7', 'OS-EXT-IPS:type': 'fixed'}]}
镜像这里不贴输出结果了,按照类似方式处理。
参考资料
https://docs.openstack.org/openstacksdk/latest/user/guides/connect_from_config.html
https://docs.openstack.org/openstacksdk/latest/user/guides/compute.html