2015-03-31 23:05:43 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
try:
|
|
|
|
import shade
|
|
|
|
HAS_SHADE = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_SHADE = False
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: os_router
|
2015-09-25 19:09:02 +02:00
|
|
|
short_description: Create or delete routers from OpenStack
|
2015-03-31 23:05:43 +02:00
|
|
|
extends_documentation_fragment: openstack
|
2015-09-14 22:20:18 +02:00
|
|
|
version_added: "2.0"
|
2015-10-02 15:41:18 +02:00
|
|
|
author: "David Shrewsbury (@Shrews)"
|
2015-03-31 23:05:43 +02:00
|
|
|
description:
|
2015-04-08 20:10:34 +02:00
|
|
|
- Create or Delete routers from OpenStack. Although Neutron allows
|
|
|
|
routers to share the same name, this module enforces name uniqueness
|
|
|
|
to be more user friendly.
|
2015-03-31 23:05:43 +02:00
|
|
|
options:
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicate desired state of the resource
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
default: present
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name to be give to the router
|
|
|
|
required: true
|
|
|
|
admin_state_up:
|
|
|
|
description:
|
2015-09-14 22:20:18 +02:00
|
|
|
- Desired admin state of the created or existing router.
|
2015-03-31 23:05:43 +02:00
|
|
|
required: false
|
|
|
|
default: true
|
2015-09-25 19:09:02 +02:00
|
|
|
enable_snat:
|
|
|
|
description:
|
|
|
|
- Enable Source NAT (SNAT) attribute.
|
|
|
|
required: false
|
|
|
|
default: true
|
|
|
|
network:
|
|
|
|
description:
|
|
|
|
- Unique name or ID of the external gateway network.
|
2016-04-01 22:58:30 +02:00
|
|
|
- required I(interfaces) or I(enable_snat) are provided,
|
2015-09-25 19:09:02 +02:00
|
|
|
type: string
|
2016-04-01 22:58:30 +02:00
|
|
|
required: false
|
2015-09-25 19:09:02 +02:00
|
|
|
false otherwise.
|
|
|
|
default: None
|
2015-10-19 21:53:15 +02:00
|
|
|
external_fixed_ips:
|
|
|
|
description:
|
|
|
|
- The IP address parameters for the external gateway network. Each
|
|
|
|
is a dictionary with the subnet name or ID (subnet) and the IP
|
|
|
|
address to assign on the subnet (ip). If no IP is specified,
|
|
|
|
one is automatically assigned from that subnet.
|
|
|
|
required: false
|
|
|
|
default: None
|
2015-09-25 19:09:02 +02:00
|
|
|
interfaces:
|
|
|
|
description:
|
2015-10-19 21:53:15 +02:00
|
|
|
- List of subnets to attach to the router internal interface.
|
2015-09-25 19:09:02 +02:00
|
|
|
required: false
|
|
|
|
default: None
|
2015-03-31 23:05:43 +02:00
|
|
|
requirements: ["shade"]
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2015-09-25 19:09:02 +02:00
|
|
|
# Create a simple router, not attached to a gateway or subnets.
|
|
|
|
- os_router:
|
|
|
|
cloud: mycloud
|
|
|
|
state: present
|
|
|
|
name: simple_router
|
|
|
|
|
2015-10-19 21:53:15 +02:00
|
|
|
# Creates a router attached to ext_network1 on an IPv4 subnet and one
|
|
|
|
# internal subnet interface.
|
2015-04-08 20:10:34 +02:00
|
|
|
- os_router:
|
2015-09-25 19:09:02 +02:00
|
|
|
cloud: mycloud
|
|
|
|
state: present
|
|
|
|
name: router1
|
|
|
|
network: ext_network1
|
2015-10-19 21:53:15 +02:00
|
|
|
external_fixed_ips:
|
|
|
|
- subnet: public-subnet
|
|
|
|
ip: 172.24.4.2
|
2015-09-25 19:09:02 +02:00
|
|
|
interfaces:
|
2015-10-19 21:53:15 +02:00
|
|
|
- private-subnet
|
2015-09-25 19:09:02 +02:00
|
|
|
|
2015-10-19 21:53:15 +02:00
|
|
|
# Update existing router1 external gateway to include the IPv6 subnet.
|
|
|
|
# Note that since 'interfaces' is not provided, any existing internal
|
|
|
|
# interfaces on an existing router will be left intact.
|
2015-09-25 19:09:02 +02:00
|
|
|
- os_router:
|
|
|
|
cloud: mycloud
|
|
|
|
state: present
|
|
|
|
name: router1
|
|
|
|
network: ext_network1
|
2015-10-19 21:53:15 +02:00
|
|
|
external_fixed_ips:
|
|
|
|
- subnet: public-subnet
|
|
|
|
ip: 172.24.4.2
|
|
|
|
- subnet: ipv6-public-subnet
|
|
|
|
ip: 2001:db8::3
|
2015-09-25 19:09:02 +02:00
|
|
|
|
|
|
|
# Delete router1
|
|
|
|
- os_router:
|
|
|
|
cloud: mycloud
|
|
|
|
state: absent
|
|
|
|
name: router1
|
2015-03-31 23:05:43 +02:00
|
|
|
'''
|
|
|
|
|
2015-09-14 22:20:18 +02:00
|
|
|
RETURN = '''
|
2015-09-25 19:09:02 +02:00
|
|
|
router:
|
|
|
|
description: Dictionary describing the router.
|
|
|
|
returned: On success when I(state) is 'present'
|
2015-10-02 18:21:29 +02:00
|
|
|
type: dictionary
|
2015-09-25 19:09:02 +02:00
|
|
|
contains:
|
|
|
|
id:
|
|
|
|
description: Router ID.
|
|
|
|
type: string
|
|
|
|
sample: "474acfe5-be34-494c-b339-50f06aa143e4"
|
|
|
|
name:
|
|
|
|
description: Router name.
|
|
|
|
type: string
|
|
|
|
sample: "router1"
|
|
|
|
admin_state_up:
|
|
|
|
description: Administrative state of the router.
|
|
|
|
type: boolean
|
|
|
|
sample: true
|
|
|
|
status:
|
|
|
|
description: The router status.
|
|
|
|
type: string
|
|
|
|
sample: "ACTIVE"
|
|
|
|
tenant_id:
|
|
|
|
description: The tenant ID.
|
|
|
|
type: string
|
|
|
|
sample: "861174b82b43463c9edc5202aadc60ef"
|
|
|
|
external_gateway_info:
|
|
|
|
description: The external gateway parameters.
|
|
|
|
type: dictionary
|
|
|
|
sample: {
|
|
|
|
"enable_snat": true,
|
|
|
|
"external_fixed_ips": [
|
|
|
|
{
|
|
|
|
"ip_address": "10.6.6.99",
|
|
|
|
"subnet_id": "4272cb52-a456-4c20-8f3c-c26024ecfa81"
|
|
|
|
}
|
2015-10-02 18:21:29 +02:00
|
|
|
]
|
2015-09-25 19:09:02 +02:00
|
|
|
}
|
|
|
|
routes:
|
|
|
|
description: The extra routes configuration for L3 router.
|
|
|
|
type: list
|
2015-09-14 22:20:18 +02:00
|
|
|
'''
|
|
|
|
|
2015-03-31 23:05:43 +02:00
|
|
|
|
2015-10-19 21:53:15 +02:00
|
|
|
def _needs_update(cloud, module, router, network, internal_subnet_ids):
|
2015-04-08 20:10:34 +02:00
|
|
|
"""Decide if the given router needs an update.
|
|
|
|
"""
|
2015-09-25 19:09:02 +02:00
|
|
|
if router['admin_state_up'] != module.params['admin_state_up']:
|
2015-04-08 20:10:34 +02:00
|
|
|
return True
|
2015-10-22 17:52:28 +02:00
|
|
|
if router['external_gateway_info']:
|
|
|
|
if router['external_gateway_info'].get('enable_snat', True) != module.params['enable_snat']:
|
|
|
|
return True
|
2015-09-25 19:09:02 +02:00
|
|
|
if network:
|
2015-10-22 17:52:28 +02:00
|
|
|
if not router['external_gateway_info']:
|
|
|
|
return True
|
|
|
|
elif router['external_gateway_info']['network_id'] != network['id']:
|
2015-09-25 19:09:02 +02:00
|
|
|
return True
|
|
|
|
|
2015-10-19 21:53:15 +02:00
|
|
|
# check external interfaces
|
|
|
|
if module.params['external_fixed_ips']:
|
|
|
|
for new_iface in module.params['external_fixed_ips']:
|
|
|
|
subnet = cloud.get_subnet(new_iface['subnet'])
|
|
|
|
exists = False
|
|
|
|
|
|
|
|
# compare the requested interface with existing, looking for an existing match
|
|
|
|
for existing_iface in router['external_gateway_info']['external_fixed_ips']:
|
|
|
|
if existing_iface['subnet_id'] == subnet['id']:
|
|
|
|
if 'ip' in new_iface:
|
|
|
|
if existing_iface['ip_address'] == new_iface['ip']:
|
|
|
|
# both subnet id and ip address match
|
|
|
|
exists = True
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# only the subnet was given, so ip doesn't matter
|
2015-09-25 19:09:02 +02:00
|
|
|
exists = True
|
|
|
|
break
|
|
|
|
|
2015-10-19 21:53:15 +02:00
|
|
|
# this interface isn't present on the existing router
|
|
|
|
if not exists:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# check internal interfaces
|
|
|
|
if module.params['interfaces']:
|
|
|
|
existing_subnet_ids = []
|
|
|
|
for port in cloud.list_router_interfaces(router, 'internal'):
|
|
|
|
if 'fixed_ips' in port:
|
|
|
|
for fixed_ip in port['fixed_ips']:
|
|
|
|
existing_subnet_ids.append(fixed_ip['subnet_id'])
|
|
|
|
|
|
|
|
if set(internal_subnet_ids) != set(existing_subnet_ids):
|
2015-09-25 19:09:02 +02:00
|
|
|
return True
|
|
|
|
|
2015-04-08 20:10:34 +02:00
|
|
|
return False
|
|
|
|
|
2015-10-19 21:53:15 +02:00
|
|
|
def _system_state_change(cloud, module, router, network, internal_ids):
|
2015-04-08 20:10:34 +02:00
|
|
|
"""Check if the system state would be changed."""
|
|
|
|
state = module.params['state']
|
|
|
|
if state == 'absent' and router:
|
|
|
|
return True
|
|
|
|
if state == 'present':
|
|
|
|
if not router:
|
|
|
|
return True
|
2015-10-19 21:53:15 +02:00
|
|
|
return _needs_update(cloud, module, router, network, internal_ids)
|
2015-04-08 20:10:34 +02:00
|
|
|
return False
|
2015-03-31 23:05:43 +02:00
|
|
|
|
2015-09-25 19:09:02 +02:00
|
|
|
def _build_kwargs(cloud, module, router, network):
|
|
|
|
kwargs = {
|
|
|
|
'admin_state_up': module.params['admin_state_up'],
|
|
|
|
}
|
|
|
|
|
|
|
|
if router:
|
|
|
|
kwargs['name_or_id'] = router['id']
|
|
|
|
else:
|
|
|
|
kwargs['name'] = module.params['name']
|
|
|
|
|
|
|
|
if network:
|
|
|
|
kwargs['ext_gateway_net_id'] = network['id']
|
|
|
|
# can't send enable_snat unless we have a network
|
|
|
|
kwargs['enable_snat'] = module.params['enable_snat']
|
|
|
|
|
2015-10-19 21:53:15 +02:00
|
|
|
if module.params['external_fixed_ips']:
|
2015-09-25 19:09:02 +02:00
|
|
|
kwargs['ext_fixed_ips'] = []
|
2015-10-19 21:53:15 +02:00
|
|
|
for iface in module.params['external_fixed_ips']:
|
2015-09-25 19:09:02 +02:00
|
|
|
subnet = cloud.get_subnet(iface['subnet'])
|
|
|
|
d = {'subnet_id': subnet['id']}
|
|
|
|
if 'ip' in iface:
|
|
|
|
d['ip_address'] = iface['ip']
|
|
|
|
kwargs['ext_fixed_ips'].append(d)
|
|
|
|
|
|
|
|
return kwargs
|
|
|
|
|
2015-10-19 21:53:15 +02:00
|
|
|
def _validate_subnets(module, cloud):
|
|
|
|
external_subnet_ids = []
|
|
|
|
internal_subnet_ids = []
|
|
|
|
if module.params['external_fixed_ips']:
|
|
|
|
for iface in module.params['external_fixed_ips']:
|
|
|
|
subnet = cloud.get_subnet(iface['subnet'])
|
|
|
|
if not subnet:
|
|
|
|
module.fail_json(msg='subnet %s not found' % iface['subnet'])
|
|
|
|
external_subnet_ids.append(subnet['id'])
|
|
|
|
|
|
|
|
if module.params['interfaces']:
|
|
|
|
for iface in module.params['interfaces']:
|
|
|
|
subnet = cloud.get_subnet(iface)
|
|
|
|
if not subnet:
|
|
|
|
module.fail_json(msg='subnet %s not found' % iface)
|
|
|
|
internal_subnet_ids.append(subnet['id'])
|
|
|
|
|
|
|
|
return (external_subnet_ids, internal_subnet_ids)
|
|
|
|
|
2015-03-31 23:05:43 +02:00
|
|
|
def main():
|
|
|
|
argument_spec = openstack_full_argument_spec(
|
2015-09-25 19:09:02 +02:00
|
|
|
state=dict(default='present', choices=['absent', 'present']),
|
2015-04-08 20:10:34 +02:00
|
|
|
name=dict(required=True),
|
|
|
|
admin_state_up=dict(type='bool', default=True),
|
2015-09-25 19:09:02 +02:00
|
|
|
enable_snat=dict(type='bool', default=True),
|
|
|
|
network=dict(default=None),
|
2015-10-19 21:53:15 +02:00
|
|
|
interfaces=dict(type='list', default=None),
|
|
|
|
external_fixed_ips=dict(type='list', default=None),
|
2015-03-31 23:05:43 +02:00
|
|
|
)
|
2015-04-08 20:10:34 +02:00
|
|
|
|
2015-03-31 23:05:43 +02:00
|
|
|
module_kwargs = openstack_module_kwargs()
|
2015-04-08 20:10:34 +02:00
|
|
|
module = AnsibleModule(argument_spec,
|
|
|
|
supports_check_mode=True,
|
|
|
|
**module_kwargs)
|
2015-03-31 23:05:43 +02:00
|
|
|
|
|
|
|
if not HAS_SHADE:
|
|
|
|
module.fail_json(msg='shade is required for this module')
|
|
|
|
|
2015-04-08 20:10:34 +02:00
|
|
|
state = module.params['state']
|
2015-09-25 19:09:02 +02:00
|
|
|
name = module.params['name']
|
|
|
|
network = module.params['network']
|
|
|
|
|
2015-10-19 21:53:15 +02:00
|
|
|
if module.params['external_fixed_ips'] and not network:
|
|
|
|
module.fail_json(msg='network is required when supplying external_fixed_ips')
|
2015-04-08 20:10:34 +02:00
|
|
|
|
2015-03-31 23:05:43 +02:00
|
|
|
try:
|
|
|
|
cloud = shade.openstack_cloud(**module.params)
|
2015-04-08 20:10:34 +02:00
|
|
|
router = cloud.get_router(name)
|
2015-03-31 23:05:43 +02:00
|
|
|
|
2015-09-25 19:09:02 +02:00
|
|
|
net = None
|
|
|
|
if network:
|
|
|
|
net = cloud.get_network(network)
|
|
|
|
if not net:
|
|
|
|
module.fail_json(msg='network %s not found' % network)
|
|
|
|
|
2015-10-19 21:53:15 +02:00
|
|
|
# Validate and cache the subnet IDs so we can avoid duplicate checks
|
|
|
|
# and expensive API calls.
|
|
|
|
external_ids, internal_ids = _validate_subnets(module, cloud)
|
|
|
|
|
2015-04-08 20:10:34 +02:00
|
|
|
if module.check_mode:
|
2015-09-25 19:09:02 +02:00
|
|
|
module.exit_json(
|
2015-10-19 21:53:15 +02:00
|
|
|
changed=_system_state_change(cloud, module, router, net, internal_ids)
|
2015-09-25 19:09:02 +02:00
|
|
|
)
|
2015-03-31 23:05:43 +02:00
|
|
|
|
2015-04-08 20:10:34 +02:00
|
|
|
if state == 'present':
|
2015-09-25 19:09:02 +02:00
|
|
|
changed = False
|
|
|
|
|
2015-04-08 20:10:34 +02:00
|
|
|
if not router:
|
2015-09-25 19:09:02 +02:00
|
|
|
kwargs = _build_kwargs(cloud, module, router, net)
|
|
|
|
router = cloud.create_router(**kwargs)
|
2015-10-19 21:53:15 +02:00
|
|
|
for internal_subnet_id in internal_ids:
|
|
|
|
cloud.add_router_interface(router, subnet_id=internal_subnet_id)
|
2015-09-25 19:09:02 +02:00
|
|
|
changed = True
|
2015-03-31 23:05:43 +02:00
|
|
|
else:
|
2015-10-19 21:53:15 +02:00
|
|
|
if _needs_update(cloud, module, router, net, internal_ids):
|
2015-09-25 19:09:02 +02:00
|
|
|
kwargs = _build_kwargs(cloud, module, router, net)
|
|
|
|
router = cloud.update_router(**kwargs)
|
2015-10-19 21:53:15 +02:00
|
|
|
|
|
|
|
# On a router update, if any internal interfaces were supplied,
|
|
|
|
# just detach all existing internal interfaces and attach the new.
|
|
|
|
if internal_ids:
|
|
|
|
ports = cloud.list_router_interfaces(router, 'internal')
|
|
|
|
for port in ports:
|
|
|
|
cloud.remove_router_interface(router, port_id=port['id'])
|
|
|
|
for internal_subnet_id in internal_ids:
|
|
|
|
cloud.add_router_interface(router, subnet_id=internal_subnet_id)
|
|
|
|
|
2015-09-25 19:09:02 +02:00
|
|
|
changed = True
|
|
|
|
|
2015-10-23 19:32:37 +02:00
|
|
|
module.exit_json(changed=changed,
|
|
|
|
router=router,
|
|
|
|
id=router['id'])
|
2015-03-31 23:05:43 +02:00
|
|
|
|
2015-04-08 20:10:34 +02:00
|
|
|
elif state == 'absent':
|
|
|
|
if not router:
|
2015-09-14 22:20:18 +02:00
|
|
|
module.exit_json(changed=False)
|
2015-03-31 23:05:43 +02:00
|
|
|
else:
|
2015-10-19 21:53:15 +02:00
|
|
|
# We need to detach all internal interfaces on a router before
|
|
|
|
# we will be allowed to delete it.
|
|
|
|
ports = cloud.list_router_interfaces(router, 'internal')
|
|
|
|
for port in ports:
|
|
|
|
cloud.remove_router_interface(router, port_id=port['id'])
|
2015-04-08 20:10:34 +02:00
|
|
|
cloud.delete_router(name)
|
2015-09-14 22:20:18 +02:00
|
|
|
module.exit_json(changed=True)
|
2015-04-08 20:10:34 +02:00
|
|
|
|
2015-03-31 23:05:43 +02:00
|
|
|
except shade.OpenStackCloudException as e:
|
2016-01-13 17:00:16 +01:00
|
|
|
module.fail_json(msg=str(e))
|
2015-03-31 23:05:43 +02:00
|
|
|
|
2015-09-14 22:20:18 +02:00
|
|
|
|
2015-03-31 23:05:43 +02:00
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.openstack import *
|
2015-09-14 22:20:18 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|