summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiroslav Suchý <msuchy@redhat.com>2015-03-23 16:04:11 +0000
committerMiroslav Suchý <msuchy@redhat.com>2015-03-23 16:04:30 +0000
commitb4be4fe3d294ee017c1e087396bfe89bf78f36c6 (patch)
tree3d0241d293eb95cce95d0226494b5d725bbf7e0a
parent98aafc387b9806faec245bccbeaddde36d32d967 (diff)
downloadansible-b4be4fe3d294ee017c1e087396bfe89bf78f36c6.tar.gz
ansible-b4be4fe3d294ee017c1e087396bfe89bf78f36c6.tar.xz
ansible-b4be4fe3d294ee017c1e087396bfe89bf78f36c6.zip
add image_id_to_name filter
-rw-r--r--filter_plugins/openstack.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/filter_plugins/openstack.py b/filter_plugins/openstack.py
index 69266b497..43073f5d6 100644
--- a/filter_plugins/openstack.py
+++ b/filter_plugins/openstack.py
@@ -1,7 +1,11 @@
from ansible import errors, runner
-import json
+from glanceclient import Client as GlanceClient
+from keystoneclient import session
+from keystoneclient.auth.identity import v2 as identity
from novaclient.v3.client import Client
-import novaclient.exceptions;
+import glanceclient.exc
+import json
+import novaclient.exceptions
def flavor_id_to_name(host_vars, user, password, tenant, auth_url):
nt = Client(user, password, tenant, auth_url, service_type="compute")
@@ -19,8 +23,21 @@ def flavor_name_to_id(host_vars, user, password, tenant, auth_url):
return i.id
raise errors.AnsibleFilterError('There is no flavor of id {0}'.format(host_vars))
+def image_id_to_name(host_vars, user, password, tenant, auth_url):
+ auth = identity.Password(auth_url=auth_url, username=user,
+ password=password, tenant_name=tenant)
+ sess = session.Session(auth=auth)
+ token = auth.get_token(sess)
+ endpoint = auth.get_endpoint(sess, service_name='glance', service_type='image')
+ glance = GlanceClient('2', endpoint=endpoint, token=token)
+ try:
+ return glance.images.get(host_vars).name
+ except glanceclient.exc.HTTPNotFound:
+ raise errors.AnsibleFilterError('There is no image of id {0}'.format(host_vars))
+
class FilterModule (object):
def filters(self):
return {"flavor_id_to_name": flavor_id_to_name,
"flavor_name_to_id": flavor_name_to_id,
+ "image_id_to_name": image_id_to_name,
}