如何使用python从openstack获取服务器列表

时间:2014-11-03 12:49:12

标签: openstack-nova

我是openstack和python的新手

我试图从openstack获取服务器列表..这是代码

credentials.py

  import os
  def get_nova_credentials_v2():
  d = {}
  d['version'] = '2'
  d['username'] = os.environ['billing']
  d['password'] = os.environ['mypassword']
  d['auth_url'] = os.environ['my auth url']
  d['project_id'] = os.environ['projectname']
  return d

instance.py

  from credentials import get_nova_credentials_v2
  from novaclient.client import Client

  credentials = get_nova_credentials_v2()
  nova_client = Client(**credentials)

  print(nova_client.servers.list())

我收到错误

    C:\Python27\python.exe C:/Users/ICEClenovos/PycharmProjects/billing/instance.py
     Traceback (most recent call last):
     File "C:/Users/ICEClenovos/PycharmProjects/billing/instance.py", line 5, in <module>
     credentials = get_nova_credentials_v2()
     File "C:\Users\ICEClenovos\PycharmProjects\billing\credentials.py", line 7, in
     get_nova_credentials_v2    
     d['username'] = os.environ['billing']
     File "C:\Python27\lib\os.py", line 423, in __getitem__
     return self.data[key.upper()]
 KeyError: 'BILLING'

  Process finished with exit code 1

不知道我做错了什么..请帮忙

1 个答案:

答案 0 :(得分:0)

如果您尝试从Openstack获取服务器详细信息,则不需要获取Biling详细信息。获取你的&#34; stackrc / openrc&#34;文件。

我们用nova对象调用servers.list()。类似地,您可以使用实例详细信息,并获取有关实例here

的更多信息
  from novaclient import client as novaclient

nova = novaclient.Client(version='2.0',username='xxxx',api_key='xxxx',project_id='xxxx',auth_url='http://192.168.12.3:5000/v2.0/',insecure='True')

for server in nova.servers.list():
    print server.id, server.name