Get Instance Status via Python API

asked 2017-07-03 05:55:33 -0600

FilipGontko gravatar image

Hello guys!

Please, is there a way to get a status of an instance via Python API but not using cmd in source code? I mean original Python API function. I was looking everywhere and I can not find it, but I am not sure wheter I am not searching good enough or it does not exist.

I created this:

def get_node_state(name):
# create a session
nova_client = Client_nova(session=get_session(), version=2)
# creates a list of servers
servers_list = nova_client.servers.list()
server_state = name
server_exists = False
# search the server in the list
for s in servers_list:
    if == server_state:
        server_exists = True
if not server_exists:
    print("server %s does not exist" % server_state)
    status = None
    # runs a command to find status
    status = os.popen("openstack server show -c status --format value %s" % name).read()
status = status.strip("\r\n")
return status

But I need to exchange the os.popen command.

Thank you!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2017-07-03 09:13:35 -0600

nuriel77 gravatar image

Have a look here: (novaclient Python API bindings)

And check the novaclient v2 library on github where you can inspect how you should construct your commands/queries, for example the list() function which can accept search options. (python-novaclient)

edit flag offensive delete link more

answered 2017-07-03 07:03:08 -0600

dbaxps gravatar image

Version of nova client might be higher, syntax a bit different. But core idea would work.

edit flag offensive delete link more

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower


Asked: 2017-07-03 05:55:33 -0600

Seen: 1,192 times

Last updated: Jul 03 '17