#!/usr/bin/python #coding: utf-8 -*- # (c) 2015, Christian Zunker # # This module is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This software is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this software. If not, see . try: from novaclient import client from keystoneclient.v2_0 import client as ksclient except ImportError: print("failed=True msg='novaclient and keystone client are required'") DOCUMENTATION = ''' --- module: nova_aggregate short_description: Manage OpenStack host aggregates description: - Create host aggregates with OpenStack Nova service requirements: [ python-novaclient ] options: login_username: description: - user name to authenticate against Identity service required: True aliases: [username] login_password: description: - password to authenticate against Identity service aliases: [password] required: True login_tenant_name: description: - tenant name of the login user aliases: [tenant_name] required: True auth_url: description: - The keystone URL for authentication required: false default: 'http://127.0.0.1:35357/v2.0/' region_name: description: - Name of the region required: False default: None name: description: - Descriptive name of the aggregate required: True availability_zone: description: - Availability Zone of the aggregate required: False hosts: description: - Hosts asigned to the aggregate required: False metadata: description: - Metadata for the aggregate, used as reference inside flavors required: False state: description: - Create or delete aggregate required: False choices: ['present', 'absent'] default: 'present' ''' EXAMPLES = ''' - nova_aggregate: login_username: admin login_password: 1234 login_tenant_name: admin name: medium ''' def authenticate(module, auth_url, username, password, tenant_name, region): """ Return a Nova client object. """ try: keystone = ksclient.Client(auth_url=auth_url, username=username, password=password, tenant_name=tenant_name, region=region) except Exception as e: module.fail_json( msg = "Could not authenticate with Keystone: {}".format( e.message)) try: nova = client.Client('2', keystone.username, keystone.password, keystone.tenant_name, keystone.auth_url) except Exception as e: module.fail_json(msg = "Could not get Nova client: {}".format( e.message)) return nova def get_aggregates(nova, name, id=None): if not id: aggregates = [x for x in nova.aggregates.list() if x.name == name] else: aggregates = [x for x in nova.aggregates.list() if x.name == name and x.id == str(id)] return aggregates def create_aggregate(module, nova, name, availability_zone, hosts, metadata, id): aggregate_name = name + "-" + availability_zone aggregates = get_aggregates(nova, aggregate_name, id) if len(aggregates) >0: present_aggregate = aggregates[0] changed = False for host in hosts: if not host in present_aggregate.hosts: nova.aggregates.add_host(present_aggregate, host) changed = True if metadata != present_aggregate.metadata: nova.aggregates.set_metadata(present_aggregate, metadata) changed = True return changed, aggregates[0].id try: aggregate = nova.aggregates.create(name=aggregate_name, availability_zone=availability_zone) for host in hosts: nova.aggregates.add_host(aggregate, host) nova.aggregates.set_metadata(aggregate, metadata) except Exception as e: module.fail_json(msg = "Could not create aggregate: {0}".format(e.message)) return True, aggregate.id def delete_aggregate(module, nova, name): aggregates = get_aggregates(nova, name) if len(aggregates) == 0: return False for aggregate in aggregates: try: nova.aggregates.delete(aggregate); except Exception as e: module.fail_json(msg = "Could not delete aggregate {0}: {1}".format(name, e.message)) return True def main(): module = AnsibleModule( argument_spec = dict( login_username = dict(default='admin', aliases=["username"]), login_password = dict(required=True, aliases=["password"]), login_tenant_name = dict(required='True', aliases=["tenant_name"]), auth_url = dict(default='http://127.0.0.1:35357/v2.0/'), region_name = dict(default=None), name = dict(required=True), id = dict(default=None), availability_zone = dict(required=False), hosts = dict(required=False), metadata = dict(required=False, default=None), state = dict(default='present'), ) ) auth_url = module.params['auth_url'] region = module.params['region_name'] username = module.params['login_username'] password = module.params['login_password'] tenant_name = module.params['login_tenant_name'] name = module.params['name'] availability_zone = module.params['availability_zone'] hosts = module.params['hosts'] metadata = module.params['metadata'] id = module.params['id'] state = module.params['state'] nova = authenticate(module, auth_url, username, password, tenant_name, region) if state == 'present': changed, id = create_aggregate(module, nova, name, availability_zone, hosts, metadata, id) module.exit_json(changed=changed, name=name, id=id) elif state == 'absent': changed = delete_aggregate(module, nova, name) module.exit_json(changed=changed, name=name) else: raise ValueError("Code should never reach here") # this is magic, see lib/ansible/module_common.py #<> if __name__ == '__main__': main()