Ask Your Question

Python nova-client: get image id using name

asked 2016-04-04 23:54:44 -0600

itsme gravatar image

I am using python nova-client in my development environment.

I need to get the image id of image using the image-name as a input.

I can b e able to achieve the same in Shell command. Command for the same is as follows:

nova image-show cirros-0.3.4-x86_64-uec

It is displaying the result.

But now I need to achieve the using python script.

net = nt.images.list()

But here I am unable to get the result. I have used get instead of list.

It seems of no use.

Some on let me know for getting the same.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-04-05 05:39:38 -0600

dbaxps gravatar image

Code and configuration

[root@ip-192-169-142-127 test1(keystone_admin)]# ls -l
total 16
-rw-r--r--. 1 root root 508 Apr  5 13:32
-rw-r--r--. 1 root root 784 Apr  5 13:34 credentials.pyc
-rw-r--r--. 1 root root 220 Apr  5 13:33
-rw-r--r--. 1 root root 419 Apr  5 13:34 getImageID.pyc

[root@ip-192-169-142-127 test1(keystone_admin)]# cat

#!/usr/bin/env python
import os

def get_keystone_creds():
    d = {}
    d['username'] = os.environ['OS_USERNAME']
    d['password'] = os.environ['OS_PASSWORD']
    d['auth_url'] = os.environ['OS_AUTH_URL']
    d['tenant_name'] = os.environ['OS_TENANT_NAME']
    return d

def get_nova_creds():
    d = {}
    d['username'] = os.environ['OS_USERNAME']
    d['api_key'] = os.environ['OS_PASSWORD']
    d['auth_url'] = os.environ['OS_AUTH_URL']
    d['project_id'] = os.environ['OS_TENANT_NAME']
    return d

[root@ip-192-169-142-127 test1(keystone_admin)]# cat

import novaclient.v2.client as nvclient
from credentials import get_nova_creds
creds = get_nova_creds()
nova = nvclient.Client(**creds)
image = nova.images.find(name=name)
print (

Now run

[root@ip-192-169-142-127 test1(keystone_admin)]# python
[root@ip-192-169-142-127 test1(keystone_admin)]# glance image-list | grep af71fe64-699c-4605-908b-676f9d536fb1
| af71fe64-699c-4605-908b-676f9d536fb1 | rastasheep/ubuntu-sshd   |
edit flag offensive delete link more

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower


Asked: 2016-04-04 23:54:44 -0600

Seen: 1,177 times

Last updated: Apr 05 '16