Ask Your Question
0

openstacksdk list all project instance

asked 2018-09-07 12:07:39 -0600

mohammed saif gravatar image

updated 2018-09-07 18:31:56 -0600

How can i list all projects instance using native openstacksdk api client as of now i can list only single project instances

this is my code.

# !/usr/bin/env python
from openstack import connection
from os import getenv
import sys
sys.path.append('/home/openstack/.pwd')
import kypass
import json

conn = connection.Connection(auth_url="http://horizon.com:5000/v3/",
                             project_name="WS",username=kypass.user,
                             password=kypass.password,
                             user_domain_id="3d2f7b6dfa4148a3aad8f7",
                             project_domain_id="3d2f7b6dfa4148a3aa61bf418f7")
host_names = []
for server in conn.compute.servers():
    host = server.name
    if host.startswith('rw'):
        if 'production' in  server.metadata and str(server.metadata['production']).lower() == "yes" and 'ACTIVE' in server.status:
            host_names.append(host)

rws_host_names = {
    "all": {
        "hosts": host_names,
        "vars": {}
    },
    "_meta": {
        "hostvars": {}
    }
}
print(json.dumps(rws_host_names,indent=4,sort_keys=False))
edit retag flag offensive close merge delete

Comments

Try to format your code so that it can be read.

As a normal user, you can only list the servers owned by your project. As an admin user, you can list all projects, but you need to specify the all_tenants option, and I haven't tried it with openstacksdk.

Bernd Bausch gravatar imageBernd Bausch ( 2018-09-07 18:34:33 -0600 )edit

You can pass query parameters to the servers() method. all_tenant is documented in the API reference.

Bernd Bausch gravatar imageBernd Bausch ( 2018-09-07 18:35:44 -0600 )edit

Thnx for your input. Ya i know admin can list out all project am not sure how to use this with openstacksdk.

mohammed saif gravatar imagemohammed saif ( 2018-09-08 11:32:13 -0600 )edit

I would try logging on as an admin user instead of kypass.user/WS, and adding all_tenants=true to the compute.servers() call.

Bernd Bausch gravatar imageBernd Bausch ( 2018-09-08 17:58:07 -0600 )edit

1 answer

Sort by ┬╗ oldest newest most voted
1

answered 2018-09-09 23:55:35 -0600

updated 2018-09-10 00:01:30 -0600

I was successful with this code:

...
conn = connection.Connection(auth_url="http://192.168.1.222:5000/v3/",
                             project_name="admin",username="admin",
                             password="pw",
                             user_domain_id="default",
                             project_domain_id="default")

host_names = []
for server in conn.compute.servers(all_tenants=True):
...

Note that earlier versions of the OpenStack SDK suffer from a bug that makes it ignore the all_tenants option. If that is the case for you, add the string "all_tenants" to the _query_parameters definition in ..../site-packages/openstack/compute/v2/server.py.

See also the newest version of the openstacksdk.

edit flag offensive delete link more

Comments

Thank you sir, Its work for me as well.

mohammed saif gravatar imagemohammed saif ( 2018-09-10 03:46:29 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

Stats

Asked: 2018-09-07 12:07:39 -0600

Seen: 83 times

Last updated: Sep 10