summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorin Hochstein <lorinh@gmail.com>2015-04-21 17:30:12 -0400
committerLorin Hochstein <lorinh@gmail.com>2015-04-21 17:30:12 -0400
commit01bb3c3d31ce53be576cc2f21ceed2af2495b5cc (patch)
treeb1397801d3cc16f50f68dea7546a99e2cc4d4d27
parentd46ec30eb268648172f818489446982916dc90e9 (diff)
parent0a1a8de0f1f45579a7d440bcd389a613307b3203 (diff)
downloadopenstack-ansible-modules-01bb3c3d31ce53be576cc2f21ceed2af2495b5cc.tar.gz
Merge pull request #44 from dguerri/master
Few enhancements for neutron_* modules
-rw-r--r--neutron_floating_ip24
-rw-r--r--neutron_network41
-rw-r--r--neutron_router38
-rw-r--r--neutron_router_gateway70
-rw-r--r--neutron_router_interface42
-rw-r--r--neutron_sec_group3
-rw-r--r--neutron_subnet61
7 files changed, 153 insertions, 126 deletions
diff --git a/neutron_floating_ip b/neutron_floating_ip
index 4c14102..fbece6c 100644
--- a/neutron_floating_ip
+++ b/neutron_floating_ip
@@ -89,10 +89,12 @@ EXAMPLES = '''
def _get_ksclient(module, kwargs):
try:
- kclient = ksclient.Client(username=kwargs.get('login_username'),
- password=kwargs.get('login_password'),
- tenant_name=kwargs.get('login_tenant_name'),
- auth_url=kwargs.get('auth_url'))
+ kclient = ksclient.Client(
+ username=module.params.get('login_username'),
+ password=module.params.get('login_password'),
+ tenant_name=module.params.get('login_tenant_name'),
+ auth_url=module.params.get('auth_url'),
+ region_name=module.params.get('region_name'))
except Exception as e:
module.fail_json(msg = "Error authenticating to the keystone: %s " % e.message)
global _os_keystone
@@ -136,7 +138,7 @@ def _get_server_state(module, nova):
except Exception as e:
module.fail_json(msg = "Error in getting the server list: %s" % e.message)
return server_info, server
-
+
def _get_port_info(neutron, module, instance_id):
if module.params['port_network_name'] is None:
kwargs = {
@@ -158,7 +160,7 @@ def _get_port_info(neutron, module, instance_id):
if not ports['ports']:
return None, None
return ports['ports'][0]['fixed_ips'][0]['ip_address'], ports['ports'][0]['id']
-
+
def _get_floating_ip(module, neutron, fixed_ip_address):
kwargs = {
'fixed_ip_address': fixed_ip_address
@@ -222,7 +224,7 @@ def _update_floating_ip(neutron, module, port_id, floating_ip_id):
def main():
-
+
module = AnsibleModule(
argument_spec = dict(
login_username = dict(default='admin'),
@@ -236,14 +238,15 @@ def main():
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')
+ module.params['login_tenant_name'], module.params['auth_url'], service_type='compute',
+ region_name=module.params.get('region_name'))
neutron = _get_neutron_client(module, module.params)
except Exception as e:
module.fail_json(msg="Error in authenticating to nova: %s" % e.message)
-
+
server_info, server_obj = _get_server_state(module, nova)
if not server_info:
module.fail_json(msg="The instance name provided cannot be found")
@@ -271,4 +274,3 @@ def main():
from ansible.module_utils.basic import *
main()
-
diff --git a/neutron_network b/neutron_network
index 6dee045..a9640d2 100644
--- a/neutron_network
+++ b/neutron_network
@@ -121,16 +121,18 @@ _os_tenant_id = None
def _get_ksclient(module, kwargs):
try:
- kclient = ksclient.Client(username=kwargs.get('login_username'),
- password=kwargs.get('login_password'),
- tenant_name=kwargs.get('login_tenant_name'),
- auth_url=kwargs.get('auth_url'))
+ kclient = ksclient.Client(
+ username=module.params.get('login_username'),
+ password=module.params.get('login_password'),
+ tenant_name=module.params.get('login_tenant_name'),
+ auth_url=module.params.get('auth_url'),
+ region_name=module.params.get('region_name'))
except Exception as e:
module.fail_json(msg = "Error authenticating to the keystone: %s" %e.message)
global _os_keystone
_os_keystone = kclient
return kclient
-
+
def _get_endpoint(module, ksclient):
try:
@@ -155,15 +157,18 @@ def _get_neutron_client(module, kwargs):
def _set_tenant_id(module):
global _os_tenant_id
- if not module.params['tenant_name']:
- tenant_name = module.params['login_tenant_name']
- else:
+ if module.params['tenant_name']:
+ # We need admin power in order retrieve the tenant_id of a given
+ # tenant name and to create/delete networks for a tenant that is not
+ # the one used to authenticate the user.
tenant_name = module.params['tenant_name']
-
- for tenant in _os_keystone.tenants.list():
- if tenant.name == tenant_name:
- _os_tenant_id = tenant.id
- break
+ for tenant in _os_keystone.tenants.list():
+ if tenant.name == tenant_name:
+ _os_tenant_id = tenant.id
+ break
+ else:
+ _os_tenant_id = _os_keystone.tenant_id
+
if not _os_tenant_id:
module.fail_json(msg = "The tenant id cannot be found, please check the paramters")
@@ -191,11 +196,15 @@ def _create_network(module, neutron):
'provider:network_type': module.params.get('provider_network_type'),
'provider:physical_network': module.params.get('provider_physical_network'),
'provider:segmentation_id': module.params.get('provider_segmentation_id'),
- 'router:external': module.params.get('router_external'),
'shared': module.params.get('shared'),
'admin_state_up': module.params.get('admin_state_up'),
}
+ # Older neutron versions wil reject explicitly router:external set
+ # to false
+ if module.params.get('router_external'):
+ network['router:external'] = True
+
if module.params['provider_network_type'] == 'local':
network.pop('provider:physical_network', None)
network.pop('provider:segmentation_id', None)
@@ -219,7 +228,7 @@ def _create_network(module, neutron):
except Exception as e:
module.fail_json(msg = "Error in creating network: %s" % e.message)
return net['network']['id']
-
+
def _delete_network(module, net_id, neutron):
try:
@@ -229,7 +238,7 @@ def _delete_network(module, net_id, neutron):
return True
def main():
-
+
module = AnsibleModule(
argument_spec = dict(
login_username = dict(default='admin'),
diff --git a/neutron_router b/neutron_router
index 56d384d..6baa65f 100644
--- a/neutron_router
+++ b/neutron_router
@@ -92,16 +92,18 @@ _os_tenant_id = None
def _get_ksclient(module, kwargs):
try:
- kclient = ksclient.Client(username=kwargs.get('login_username'),
- password=kwargs.get('login_password'),
- tenant_name=kwargs.get('login_tenant_name'),
- auth_url=kwargs.get('auth_url'))
+ kclient = ksclient.Client(
+ username=module.params.get('login_username'),
+ password=module.params.get('login_password'),
+ tenant_name=module.params.get('login_tenant_name'),
+ auth_url=module.params.get('auth_url'),
+ region_name=module.params.get('region_name'))
except Exception as e:
module.fail_json(msg = "Error authenticating to the keystone: %s " % e.message)
global _os_keystone
_os_keystone = kclient
return kclient
-
+
def _get_endpoint(module, ksclient):
try:
@@ -126,18 +128,20 @@ def _get_neutron_client(module, kwargs):
def _set_tenant_id(module):
global _os_tenant_id
- if not module.params['tenant_name']:
- login_tenant_name = module.params['login_tenant_name']
+ if module.params['tenant_name']:
+ # We need admin power in order retrieve the tenant_id of a given
+ # tenant name and to create/delete networks for a tenant that is not
+ # the one used to authenticate the user.
+ tenant_name = module.params['tenant_name']
+ for tenant in _os_keystone.tenants.list():
+ if tenant.name == tenant_name:
+ _os_tenant_id = tenant.id
+ break
else:
- login_tenant_name = module.params['tenant_name']
+ _os_tenant_id = _os_keystone.tenant_id
- for tenant in _os_keystone.tenants.list():
- if tenant.name == login_tenant_name:
- _os_tenant_id = tenant.id
- break
if not _os_tenant_id:
- module.fail_json(msg = "The tenant id cannot be found, please check the paramters")
-
+ module.fail_json(msg = "The tenant id cannot be found, please check the paramters")
def _get_router_id(module, neutron):
kwargs = {
@@ -170,7 +174,7 @@ def _delete_router(module, neutron, router_id):
except:
module.fail_json("Error in deleting the router")
return True
-
+
def main():
module = AnsibleModule(
argument_spec = dict(
@@ -185,7 +189,7 @@ def main():
admin_state_up = dict(type='bool', default=True),
),
)
-
+
neutron = _get_neutron_client(module, module.params)
_set_tenant_id(module)
@@ -204,7 +208,7 @@ def main():
else:
_delete_router(module, neutron, router_id)
module.exit_json(changed=True, result="deleted")
-
+
# this is magic, see lib/ansible/module.params['common.py
from ansible.module_utils.basic import *
main()
diff --git a/neutron_router_gateway b/neutron_router_gateway
index 93235b8..3a9b7ce 100644
--- a/neutron_router_gateway
+++ b/neutron_router_gateway
@@ -82,16 +82,18 @@ EXAMPLES = '''
_os_keystone = None
def _get_ksclient(module, kwargs):
try:
- kclient = ksclient.Client(username=kwargs.get('login_username'),
- password=kwargs.get('login_password'),
- tenant_name=kwargs.get('login_tenant_name'),
- auth_url=kwargs.get('auth_url'))
+ kclient = ksclient.Client(
+ username=module.params.get('login_username'),
+ password=module.params.get('login_password'),
+ tenant_name=module.params.get('login_tenant_name'),
+ auth_url=module.params.get('auth_url'),
+ region_name=module.params.get('region_name'))
except Exception as e:
module.fail_json(msg = "Error authenticating to the keystone: %s " % e.message)
global _os_keystone
_os_keystone = kclient
return kclient
-
+
def _get_endpoint(module, ksclient):
try:
@@ -114,7 +116,7 @@ def _get_neutron_client(module, kwargs):
module.fail_json(msg = "Error in connecting to neutron: %s " % e.message)
return neutron
-def _get_router_id(module, neutron):
+def _get_router(module, neutron):
kwargs = {
'name': module.params['router_name'],
}
@@ -124,7 +126,7 @@ def _get_router_id(module, neutron):
module.fail_json(msg = "Error in getting the router list: %s " % e.message)
if not routers['routers']:
return None
- return routers['routers'][0]['id']
+ return routers['routers'][0]
def _get_net_id(neutron, module):
kwargs = {
@@ -139,19 +141,6 @@ def _get_net_id(neutron, module):
return None
return networks['networks'][0]['id']
-def _get_port_id(neutron, module, router_id, network_id):
- kwargs = {
- 'device_id': router_id,
- 'network_id': network_id,
- }
- try:
- ports = neutron.list_ports(**kwargs)
- except Exception as e:
- module.fail_json( msg = "Error in listing ports: %s" % e.message)
- if not ports['ports']:
- return None
- return ports['ports'][0]['id']
-
def _add_gateway_router(neutron, module, router_id, network_id):
kwargs = {
'network_id': network_id
@@ -161,16 +150,16 @@ def _add_gateway_router(neutron, module, router_id, network_id):
except Exception as e:
module.fail_json(msg = "Error in adding gateway to router: %s" % e.message)
return True
-
+
def _remove_gateway_router(neutron, module, router_id):
try:
neutron.remove_gateway_router(router_id)
except Exception as e:
module.fail_json(msg = "Error in removing gateway to router: %s" % e.message)
return True
-
+
def main():
-
+
module = AnsibleModule(
argument_spec = dict(
login_username = dict(default='admin'),
@@ -183,33 +172,36 @@ def main():
state = dict(default='present', choices=['absent', 'present']),
),
)
-
+
neutron = _get_neutron_client(module, module.params)
- router_id = _get_router_id(module, neutron)
+ router = _get_router(module, neutron)
- if not router_id:
+ if not router:
module.fail_json(msg="failed to get the router id, please check the router name")
network_id = _get_net_id(neutron, module)
if not network_id:
module.fail_json(msg="failed to get the network id, please check the network name and make sure it is external")
-
+
if module.params['state'] == 'present':
- port_id = _get_port_id(neutron, module, router_id, network_id)
- if not port_id:
- _add_gateway_router(neutron, module, router_id, network_id)
- module.exit_json(changed=True, result="created")
- module.exit_json(changed=False, result="success")
+ if router.get('external_gateway_info') is None:
+ _add_gateway_router(neutron, module, router['id'], network_id)
+ module.exit_json(changed=True, updated=False, result="created")
+ else:
+ if router['external_gateway_info']['network_id'] == network_id:
+ module.exit_json(changed=False, updated=False, result="success")
+ else:
+ _remove_gateway_router(neutron, module, router['id'])
+ _add_gateway_router(neutron, module, router['id'], network_id)
+ module.exit_json(changed=True, updated=True, result="created")
if module.params['state'] == 'absent':
- port_id = _get_port_id(neutron, module, router_id, network_id)
- if not port_id:
- module.exit_json(changed=False, result="Success")
- _remove_gateway_router(neutron, module, router_id)
- module.exit_json(changed=True, result="Deleted")
+ if router.get('external_gateway_info') is None:
+ module.exit_json(changed=False, updated=False, result="success")
+ else:
+ _remove_gateway_router(neutron, module, router['id'])
+ module.exit_json(changed=True, updated=False, result="deleted")
# this is magic, see lib/ansible/module.params['common.py
from ansible.module_utils.basic import *
main()
-
-
diff --git a/neutron_router_interface b/neutron_router_interface
index 8d57725..a3060ff 100644
--- a/neutron_router_interface
+++ b/neutron_router_interface
@@ -93,16 +93,18 @@ _os_tenant_id = None
def _get_ksclient(module, kwargs):
try:
- kclient = ksclient.Client(username=kwargs.get('login_username'),
- password=kwargs.get('login_password'),
- tenant_name=kwargs.get('login_tenant_name'),
- auth_url=kwargs.get('auth_url'))
+ kclient = ksclient.Client(
+ username=module.params.get('login_username'),
+ password=module.params.get('login_password'),
+ tenant_name=module.params.get('login_tenant_name'),
+ auth_url=module.params.get('auth_url'),
+ region_name=module.params.get('region_name'))
except Exception as e:
module.fail_json(msg = "Error authenticating to the keystone: %s " % e.message)
global _os_keystone
_os_keystone = kclient
return kclient
-
+
def _get_endpoint(module, ksclient):
try:
@@ -127,19 +129,21 @@ def _get_neutron_client(module, kwargs):
def _set_tenant_id(module):
global _os_tenant_id
- if not module.params['tenant_name']:
- login_tenant_name = module.params['login_tenant_name']
+ if module.params['tenant_name']:
+ # We need admin power in order retrieve the tenant_id of a given
+ # tenant name and to create/delete networks for a tenant that is not
+ # the one used to authenticate the user.
+ tenant_name = module.params['tenant_name']
+ for tenant in _os_keystone.tenants.list():
+ if tenant.name == tenant_name:
+ _os_tenant_id = tenant.id
+ break
else:
- login_tenant_name = module.params['tenant_name']
+ _os_tenant_id = _os_keystone.tenant_id
- for tenant in _os_keystone.tenants.list():
- if tenant.name == login_tenant_name:
- _os_tenant_id = tenant.id
- break
if not _os_tenant_id:
module.fail_json(msg = "The tenant id cannot be found, please check the paramters")
-
def _get_router_id(module, neutron):
kwargs = {
'name': module.params['router_name'],
@@ -166,7 +170,7 @@ def _get_subnet_id(module, neutron):
if not subnets['subnets']:
return None
return subnets['subnets'][0]['id']
-
+
def _get_port_id(neutron, module, router_id, subnet_id):
kwargs = {
'tenant_id': _os_tenant_id,
@@ -193,7 +197,7 @@ def _add_interface_router(neutron, module, router_id, subnet_id):
except Exception as e:
module.fail_json(msg = "Error in adding interface to router: %s" % e.message)
return True
-
+
def _remove_interface_router(neutron, module, router_id, subnet_id):
kwargs = {
'subnet_id': subnet_id
@@ -203,7 +207,7 @@ def _remove_interface_router(neutron, module, router_id, subnet_id):
except Exception as e:
module.fail_json(msg="Error in removing interface from router: %s" % e.message)
return True
-
+
def main():
module = AnsibleModule(
argument_spec = dict(
@@ -218,7 +222,7 @@ def main():
state = dict(default='present', choices=['absent', 'present']),
),
)
-
+
neutron = _get_neutron_client(module, module.params)
_set_tenant_id(module)
@@ -229,7 +233,7 @@ def main():
subnet_id = _get_subnet_id(module, neutron)
if not subnet_id:
module.fail_json(msg="failed to get the subnet id, please check the subnet name")
-
+
if module.params['state'] == 'present':
port_id = _get_port_id(neutron, module, router_id, subnet_id)
if not port_id:
@@ -243,7 +247,7 @@ def main():
module.exit_json(changed = False, result = "Success")
_remove_interface_router(neutron, module, router_id, subnet_id)
module.exit_json(changed=True, result="Deleted")
-
+
# this is magic, see lib/ansible/module.params['common.py
from ansible.module_utils.basic import *
main()
diff --git a/neutron_sec_group b/neutron_sec_group
index 0d882c9..a5b8a6d 100644
--- a/neutron_sec_group
+++ b/neutron_sec_group
@@ -310,7 +310,8 @@ def _update_sg_rules(module, network_client, sg, wanted_rules, tenant_id):
clean_new_rule[key] = None
continue
value = clean_new_rule[key]
- if isinstance(value, (str, unicode)) and value.isdigit():
+ if isinstance(value, (str, unicode)) and value.isdigit() and \
+ key != 'tenant_id':
clean_new_rule[key] = int(value)
if cmp(clean_old_rule, clean_new_rule) == 0:
matched_id = old_id
diff --git a/neutron_subnet b/neutron_subnet
index 8cb622b..afdcef8 100644
--- a/neutron_subnet
+++ b/neutron_subnet
@@ -85,6 +85,11 @@ options:
- Whether DHCP should be enabled for this subnet.
required: false
default: true
+ no_gateway:
+ description:
+ - If "true", no gateway will be created for this subnet
+ required: false
+ default: false
gateway_ip:
description:
- The ip that would be assigned to the gateway for this subnet
@@ -126,16 +131,18 @@ _os_network_id = None
def _get_ksclient(module, kwargs):
try:
- kclient = ksclient.Client(username=kwargs.get('login_username'),
- password=kwargs.get('login_password'),
- tenant_name=kwargs.get('login_tenant_name'),
- auth_url=kwargs.get('auth_url'))
+ kclient = ksclient.Client(
+ username=module.params.get('login_username'),
+ password=module.params.get('login_password'),
+ tenant_name=module.params.get('login_tenant_name'),
+ auth_url=module.params.get('auth_url'),
+ region_name=module.params.get('region_name'))
except Exception as e:
module.fail_json(msg = "Error authenticating to the keystone: %s" %e.message)
global _os_keystone
_os_keystone = kclient
return kclient
-
+
def _get_endpoint(module, ksclient):
try:
@@ -160,17 +167,20 @@ def _get_neutron_client(module, kwargs):
def _set_tenant_id(module):
global _os_tenant_id
- if not module.params['tenant_name']:
- tenant_name = module.params['login_tenant_name']
- else:
+ if module.params['tenant_name']:
+ # We need admin power in order retrieve the tenant_id of a given
+ # tenant name and to create/delete networks for a tenant that is not
+ # the one used to authenticate the user.
tenant_name = module.params['tenant_name']
+ for tenant in _os_keystone.tenants.list():
+ if tenant.name == tenant_name:
+ _os_tenant_id = tenant.id
+ break
+ else:
+ _os_tenant_id = _os_keystone.tenant_id
- for tenant in _os_keystone.tenants.list():
- if tenant.name == tenant_name:
- _os_tenant_id = tenant.id
- break
if not _os_tenant_id:
- module.fail_json(msg = "The tenant id cannot be found, please check the paramters")
+ module.fail_json(msg = "The tenant id cannot be found, please check the paramters")
def _get_net_id(neutron, module):
kwargs = {
@@ -180,7 +190,7 @@ def _get_net_id(neutron, module):
try:
networks = neutron.list_networks(**kwargs)
except Exception as e:
- module.fail_json("Error in listing Neutron networks: %s" % e.message)
+ module.fail_json(msg = "Error in listing Neutron networks: %s" % e.message)
if not networks['networks']:
return None
return networks['networks'][0]['id']
@@ -226,8 +236,12 @@ def _create_subnet(module, neutron):
}
]
subnet.update({'allocation_pools': allocation_pools})
- if not module.params['gateway_ip']:
- subnet.pop('gateway_ip')
+ # "subnet['gateway_ip'] = None" means: "no gateway"
+ # no gateway_ip in body means: "automatic gateway"
+ if module.params['no_gateway']:
+ subnet['gateway_ip'] = None
+ elif module.params['gateway_ip'] is not None:
+ subnet['gateway_ip'] = module.params['gateway_ip']
if module.params['dns_nameservers']:
subnet['dns_nameservers'] = module.params['dns_nameservers'].split(',')
else:
@@ -239,18 +253,18 @@ def _create_subnet(module, neutron):
except Exception, e:
module.fail_json(msg = "Failure in creating subnet: %s" % e.message)
return new_subnet['subnet']['id']
-
-
+
+
def _delete_subnet(module, neutron, subnet_id):
try:
neutron.delete_subnet(subnet_id)
except Exception as e:
module.fail_json( msg = "Error in deleting subnet: %s" % e.message)
return True
-
-
+
+
def main():
-
+
module = AnsibleModule(
argument_spec = dict(
login_username = dict(default='admin'),
@@ -264,7 +278,8 @@ def main():
tenant_name = dict(default=None),
state = dict(default='present', choices=['absent', 'present']),
ip_version = dict(default='4', choices=['4', '6']),
- enable_dhcp = dict(default='true', choices=BOOLEANS),
+ enable_dhcp = dict(default=True, type='bool'),
+ no_gateway = dict(default=False, type='bool'),
gateway_ip = dict(default=None),
dns_nameservers = dict(default=None),
allocation_pool_start = dict(default=None),
@@ -288,7 +303,7 @@ def main():
else:
_delete_subnet(module, neutron, subnet_id)
module.exit_json(changed = True, result = "deleted")
-
+
# this is magic, see lib/ansible/module.params['common.py
from ansible.module_utils.basic import *
main()