|
|
|
@ -126,117 +126,117 @@ requirements: ["novaclient"]
|
|
|
|
|
def _delete_server(module, nova):
|
|
|
|
|
name = None
|
|
|
|
|
try:
|
|
|
|
|
server = nova.servers.list({'name': module.params['name']}).pop()
|
|
|
|
|
nova.servers.delete(server)
|
|
|
|
|
server = nova.servers.list({'name': module.params['name']}).pop()
|
|
|
|
|
nova.servers.delete(server)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
module.fail_json( msg = "Error in deleting vm: %s" % e.message)
|
|
|
|
|
module.fail_json( msg = "Error in deleting vm: %s" % e.message)
|
|
|
|
|
if module.params['wait'] == 'no':
|
|
|
|
|
module.exit_json(changed = True, result = "deleted")
|
|
|
|
|
module.exit_json(changed = True, result = "deleted")
|
|
|
|
|
expire = time.time() + module.params['wait_for']
|
|
|
|
|
while time.time() < expire:
|
|
|
|
|
name = nova.servers.list({'name': module.params['name']})
|
|
|
|
|
if not name:
|
|
|
|
|
module.exit_json(changed = True, result = "deleted")
|
|
|
|
|
time.sleep(5)
|
|
|
|
|
name = nova.servers.list({'name': module.params['name']})
|
|
|
|
|
if not name:
|
|
|
|
|
module.exit_json(changed = True, result = "deleted")
|
|
|
|
|
time.sleep(5)
|
|
|
|
|
module.fail_json(msg = "Timed out waiting for server to get deleted, please check manually")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _create_server(module, nova):
|
|
|
|
|
bootargs = [module.params['name'], module.params['image_id'], module.params['flavor_id']]
|
|
|
|
|
bootkwargs = {
|
|
|
|
|
'nics' : module.params['nics'],
|
|
|
|
|
'meta' : module.params['meta'],
|
|
|
|
|
'key_name': module.params['key_name'],
|
|
|
|
|
'security_groups': module.params['security_groups'].split(','),
|
|
|
|
|
'nics' : module.params['nics'],
|
|
|
|
|
'meta' : module.params['meta'],
|
|
|
|
|
'key_name': module.params['key_name'],
|
|
|
|
|
'security_groups': module.params['security_groups'].split(','),
|
|
|
|
|
}
|
|
|
|
|
if not module.params['key_name']:
|
|
|
|
|
del bootkwargs['key_name']
|
|
|
|
|
del bootkwargs['key_name']
|
|
|
|
|
try:
|
|
|
|
|
server = nova.servers.create(*bootargs, **bootkwargs )
|
|
|
|
|
server = nova.servers.get(server.id)
|
|
|
|
|
server = nova.servers.create(*bootargs, **bootkwargs )
|
|
|
|
|
server = nova.servers.get(server.id)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
module.fail_json( msg = "Error in creating instance: %s " % e.message)
|
|
|
|
|
module.fail_json( msg = "Error in creating instance: %s " % e.message)
|
|
|
|
|
if module.params['wait'] == 'yes':
|
|
|
|
|
expire = time.time() + module.params['wait_for']
|
|
|
|
|
while time.time() < expire:
|
|
|
|
|
try:
|
|
|
|
|
expire = time.time() + module.params['wait_for']
|
|
|
|
|
while time.time() < expire:
|
|
|
|
|
try:
|
|
|
|
|
server = nova.servers.get(server.id)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
module.fail_json( msg = "Error in getting info from instance: %s " % e.message)
|
|
|
|
|
if server.status == 'ACTIVE':
|
|
|
|
|
private = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'fixed']
|
|
|
|
|
public = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'floating']
|
|
|
|
|
module.exit_json(changed = True, id = server.id, private_ip=''.join(private), public_ip=''.join(public), status = server.status, info = server._info)
|
|
|
|
|
if server.status == 'ERROR':
|
|
|
|
|
module.fail_json(msg = "Error in creating the server, please check logs")
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
|
|
module.fail_json(msg = "Timeout waiting for the server to come up.. Please check manually")
|
|
|
|
|
if server.status == 'ACTIVE':
|
|
|
|
|
private = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'fixed']
|
|
|
|
|
public = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'floating']
|
|
|
|
|
module.exit_json(changed = True, id = server.id, private_ip=''.join(private), public_ip=''.join(public), status = server.status, info = server._info)
|
|
|
|
|
if server.status == 'ERROR':
|
|
|
|
|
module.fail_json(msg = "Error in creating the server, please check logs")
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
|
|
module.fail_json(msg = "Timeout waiting for the server to come up.. Please check manually")
|
|
|
|
|
if server.status == 'ERROR':
|
|
|
|
|
module.fail_json(msg = "Error in creating the server.. Please check manually")
|
|
|
|
|
module.fail_json(msg = "Error in creating the server.. Please check manually")
|
|
|
|
|
private = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'fixed']
|
|
|
|
|
public = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'floating']
|
|
|
|
|
module.exit_json(changed = True, id = info['id'], private_ip=''.join(private), public_ip=''.join(public), status = server.status, info = server._info)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_server_state(module, nova):
|
|
|
|
|
server = None
|
|
|
|
|
try:
|
|
|
|
|
servers = nova.servers.list({'name': module.params['name']})
|
|
|
|
|
if servers:
|
|
|
|
|
server = servers.pop()
|
|
|
|
|
servers = nova.servers.list({'name': module.params['name']})
|
|
|
|
|
if servers:
|
|
|
|
|
server = servers.pop()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
module.fail_json(msg = "Error in getting the server list: %s" % e.message)
|
|
|
|
|
module.fail_json(msg = "Error in getting the server list: %s" % e.message)
|
|
|
|
|
if server and module.params['state'] == 'present':
|
|
|
|
|
if server.status != 'ACTIVE':
|
|
|
|
|
module.fail_json( msg="The VM is available but not Active. state:" + server.status)
|
|
|
|
|
private = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'fixed']
|
|
|
|
|
if server.status != 'ACTIVE':
|
|
|
|
|
module.fail_json( msg="The VM is available but not Active. state:" + server.status)
|
|
|
|
|
private = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'fixed']
|
|
|
|
|
public = [ x['addr'] for x in getattr(server, 'addresses').itervalues().next() if x['OS-EXT-IPS:type'] == 'floating']
|
|
|
|
|
module.exit_json(changed = False, id = server.id, public_ip = ''.join(public), private_ip = ''.join(private), info = server._info)
|
|
|
|
|
module.exit_json(changed = False, id = server.id, public_ip = ''.join(public), private_ip = ''.join(private), info = server._info)
|
|
|
|
|
if server and module.params['state'] == 'absent':
|
|
|
|
|
return True
|
|
|
|
|
return True
|
|
|
|
|
if module.params['state'] == 'absent':
|
|
|
|
|
module.exit_json(changed = False, result = "not present")
|
|
|
|
|
module.exit_json(changed = False, result = "not present")
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
|
argument_spec = dict(
|
|
|
|
|
login_username = dict(default='admin'),
|
|
|
|
|
login_password = dict(required=True),
|
|
|
|
|
login_tenant_name = dict(required='True'),
|
|
|
|
|
auth_url = dict(default='http://127.0.0.1:35357/v2.0/'),
|
|
|
|
|
region_name = dict(default=None),
|
|
|
|
|
name = dict(required=True),
|
|
|
|
|
image_id = dict(required=True),
|
|
|
|
|
flavor_id = dict(default=1),
|
|
|
|
|
key_name = dict(default=None),
|
|
|
|
|
security_groups = dict(default='default'),
|
|
|
|
|
nics = dict(default=None),
|
|
|
|
|
meta = dict(default=None),
|
|
|
|
|
wait = dict(default='yes', choices=['yes', 'no']),
|
|
|
|
|
wait_for = dict(default=120),
|
|
|
|
|
state = dict(default='present', choices=['absent', 'present'])
|
|
|
|
|
argument_spec = dict(
|
|
|
|
|
login_username = dict(default='admin'),
|
|
|
|
|
login_password = dict(required=True),
|
|
|
|
|
login_tenant_name = dict(required='True'),
|
|
|
|
|
auth_url = dict(default='http://127.0.0.1:35357/v2.0/'),
|
|
|
|
|
region_name = dict(default=None),
|
|
|
|
|
name = dict(required=True),
|
|
|
|
|
image_id = dict(required=True),
|
|
|
|
|
flavor_id = dict(default=1),
|
|
|
|
|
key_name = dict(default=None),
|
|
|
|
|
security_groups = dict(default='default'),
|
|
|
|
|
nics = dict(default=None),
|
|
|
|
|
meta = dict(default=None),
|
|
|
|
|
wait = dict(default='yes', choices=['yes', 'no']),
|
|
|
|
|
wait_for = dict(default=120),
|
|
|
|
|
state = dict(default='present', choices=['absent', 'present'])
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
nova = nova_client.Client( module.params['login_username'],
|
|
|
|
|
module.params['login_password'],
|
|
|
|
|
module.params['login_tenant_name'],
|
|
|
|
|
module.params['auth_url'],
|
|
|
|
|
service_type='compute')
|
|
|
|
|
nova = nova_client.Client( module.params['login_username'],
|
|
|
|
|
module.params['login_password'],
|
|
|
|
|
module.params['login_tenant_name'],
|
|
|
|
|
module.params['auth_url'],
|
|
|
|
|
service_type='compute')
|
|
|
|
|
except Exception as e:
|
|
|
|
|
module.fail_json( msg = "Error in authenticating to nova: %s" % e.message)
|
|
|
|
|
module.fail_json( msg = "Error in authenticating to nova: %s" % e.message)
|
|
|
|
|
if module.params['state'] == 'present':
|
|
|
|
|
_get_server_state(module, nova)
|
|
|
|
|
_create_server(module, nova)
|
|
|
|
|
_get_server_state(module, nova)
|
|
|
|
|
_create_server(module, nova)
|
|
|
|
|
if module.params['state'] == 'absent':
|
|
|
|
|
_get_server_state(module, nova)
|
|
|
|
|
_delete_server(module, nova)
|
|
|
|
|
|
|
|
|
|
_get_server_state(module, nova)
|
|
|
|
|
_delete_server(module, nova)
|
|
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module.params['common.py
|
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
|
main()
|
|
|
|
|