From 1b54f37dbddc12eb6099b3c1018b6c09d47e3f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Such=C3=BD?= Date: Mon, 23 Mar 2015 12:36:51 +0000 Subject: [PATCH] add filters for openstack --- README | 2 ++ filter_plugins/openstack.py | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 filter_plugins/openstack.py diff --git a/README b/README index e3b97b5fa7..bce9c8d951 100644 --- a/README +++ b/README @@ -20,6 +20,8 @@ tasks - snippets of tasks that should be included in plays roles - specific roles to be use in playbooks. Each role has it's own files/templates/vars +filter_plugins - Jinja filters + == Paths == public path for everything is: diff --git a/filter_plugins/openstack.py b/filter_plugins/openstack.py new file mode 100644 index 0000000000..69266b4971 --- /dev/null +++ b/filter_plugins/openstack.py @@ -0,0 +1,26 @@ +from ansible import errors, runner +import json +from novaclient.v3.client import Client +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") + try: + flavor = nt.flavors.get(host_vars) + except novaclient.exceptions.NotFound: + raise errors.AnsibleFilterError('There is no flavor of name {0}'.format(host_vars)) + return flavor.name + + +def flavor_name_to_id(host_vars, user, password, tenant, auth_url): + nt = Client(user, password, tenant, auth_url, service_type="compute") + for i in nt.flavors.list(): + if i.name == host_vars: + return i.id + raise errors.AnsibleFilterError('There is no flavor 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, + }