2015-03-31 23:05:43 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
|
|
|
|
# Copyright (c) 2013, Benno Joy <benno@ansible.com>
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2016-02-18 22:59:15 +01:00
|
|
|
from distutils.version import StrictVersion
|
|
|
|
|
|
|
|
|
2015-03-31 23:05:43 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: os_network
|
2015-09-25 20:58:47 +02:00
|
|
|
short_description: Creates/removes networks from OpenStack
|
2015-03-31 23:05:43 +02:00
|
|
|
extends_documentation_fragment: openstack
|
|
|
|
version_added: "2.0"
|
2015-06-15 20:41:22 +02:00
|
|
|
author: "Monty Taylor (@emonty)"
|
2015-03-31 23:05:43 +02:00
|
|
|
description:
|
2015-09-25 20:58:47 +02:00
|
|
|
- Add or remove network from OpenStack.
|
2015-03-31 23:05:43 +02:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Name to be assigned to the network.
|
|
|
|
required: true
|
|
|
|
shared:
|
|
|
|
description:
|
|
|
|
- Whether this network is shared or not.
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
admin_state_up:
|
|
|
|
description:
|
|
|
|
- Whether the state should be marked as up or down.
|
|
|
|
required: false
|
|
|
|
default: true
|
2015-09-25 20:58:47 +02:00
|
|
|
external:
|
|
|
|
description:
|
|
|
|
- Whether this network is externally accessible.
|
|
|
|
required: false
|
|
|
|
default: false
|
2015-03-31 23:05:43 +02:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicate desired state of the resource.
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
required: false
|
|
|
|
default: present
|
2016-02-18 22:59:15 +01:00
|
|
|
provider_physical_network:
|
|
|
|
description:
|
|
|
|
- The physical network where this network object is implemented.
|
|
|
|
required: false
|
|
|
|
default: None
|
2016-04-10 11:37:53 +02:00
|
|
|
version_added: "2.1"
|
2016-02-18 22:59:15 +01:00
|
|
|
provider_network_type:
|
|
|
|
description:
|
|
|
|
- The type of physical network that maps to this network resource.
|
|
|
|
choices: ['flat', 'vlan', 'vxlan', 'gre']
|
|
|
|
required: false
|
|
|
|
default: None
|
2016-04-10 11:37:53 +02:00
|
|
|
version_added: "2.1"
|
2016-02-18 22:59:15 +01:00
|
|
|
provider_segmentation_id:
|
|
|
|
description:
|
|
|
|
- An isolated segment on the physical network. The I(network_type)
|
|
|
|
attribute defines the segmentation model. For example, if the
|
|
|
|
I(network_type) value is vlan, this ID is a vlan identifier. If
|
|
|
|
the I(network_type) value is gre, this ID is a gre key.
|
|
|
|
required: false
|
|
|
|
default: None
|
2016-04-10 11:37:53 +02:00
|
|
|
version_added: "2.1"
|
2015-03-31 23:05:43 +02:00
|
|
|
requirements: ["shade"]
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2015-09-25 20:58:47 +02:00
|
|
|
# Create an externally accessible network named 'ext_network'.
|
2015-03-31 23:05:43 +02:00
|
|
|
- os_network:
|
2015-09-25 20:58:47 +02:00
|
|
|
cloud: mycloud
|
2015-08-28 14:34:05 +02:00
|
|
|
state: present
|
2015-09-25 20:58:47 +02:00
|
|
|
name: ext_network
|
|
|
|
external: true
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
network:
|
|
|
|
description: Dictionary describing the network.
|
|
|
|
returned: On success when I(state) is 'present'.
|
|
|
|
type: dictionary
|
|
|
|
contains:
|
|
|
|
id:
|
|
|
|
description: Network ID.
|
|
|
|
type: string
|
|
|
|
sample: "4bb4f9a5-3bd2-4562-bf6a-d17a6341bb56"
|
|
|
|
name:
|
|
|
|
description: Network name.
|
|
|
|
type: string
|
|
|
|
sample: "ext_network"
|
|
|
|
shared:
|
|
|
|
description: Indicates whether this network is shared across all tenants.
|
|
|
|
type: bool
|
|
|
|
sample: false
|
|
|
|
status:
|
|
|
|
description: Network status.
|
|
|
|
type: string
|
|
|
|
sample: "ACTIVE"
|
|
|
|
mtu:
|
|
|
|
description: The MTU of a network resource.
|
|
|
|
type: integer
|
|
|
|
sample: 0
|
|
|
|
admin_state_up:
|
|
|
|
description: The administrative state of the network.
|
|
|
|
type: bool
|
|
|
|
sample: true
|
|
|
|
port_security_enabled:
|
|
|
|
description: The port security status
|
|
|
|
type: bool
|
|
|
|
sample: true
|
|
|
|
router:external:
|
|
|
|
description: Indicates whether this network is externally accessible.
|
|
|
|
type: bool
|
|
|
|
sample: true
|
|
|
|
tenant_id:
|
|
|
|
description: The tenant ID.
|
|
|
|
type: string
|
|
|
|
sample: "06820f94b9f54b119636be2728d216fc"
|
|
|
|
subnets:
|
|
|
|
description: The associated subnets.
|
|
|
|
type: list
|
|
|
|
sample: []
|
2016-02-18 22:59:15 +01:00
|
|
|
"provider:physical_network":
|
|
|
|
description: The physical network where this network object is implemented.
|
|
|
|
type: string
|
|
|
|
sample: my_vlan_net
|
|
|
|
"provider:network_type":
|
|
|
|
description: The type of physical network that maps to this network resource.
|
|
|
|
type: string
|
|
|
|
sample: vlan
|
|
|
|
"provider:segmentation_id":
|
|
|
|
description: An isolated segment on the physical network.
|
|
|
|
type: string
|
|
|
|
sample: 101
|
2015-03-31 23:05:43 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = openstack_full_argument_spec(
|
|
|
|
name=dict(required=True),
|
|
|
|
shared=dict(default=False, type='bool'),
|
|
|
|
admin_state_up=dict(default=True, type='bool'),
|
2015-09-25 20:58:47 +02:00
|
|
|
external=dict(default=False, type='bool'),
|
2016-02-18 22:59:15 +01:00
|
|
|
provider_physical_network=dict(required=False),
|
|
|
|
provider_network_type=dict(required=False, default=None,
|
|
|
|
choices=['flat', 'vlan', 'vxlan', 'gre']),
|
|
|
|
provider_segmentation_id=dict(required=False),
|
2015-06-04 18:08:27 +02:00
|
|
|
state=dict(default='present', choices=['absent', 'present']),
|
2015-03-31 23:05:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
module_kwargs = openstack_module_kwargs()
|
|
|
|
module = AnsibleModule(argument_spec, **module_kwargs)
|
|
|
|
|
|
|
|
if not HAS_SHADE:
|
|
|
|
module.fail_json(msg='shade is required for this module')
|
|
|
|
|
|
|
|
state = module.params['state']
|
|
|
|
name = module.params['name']
|
|
|
|
shared = module.params['shared']
|
|
|
|
admin_state_up = module.params['admin_state_up']
|
2015-09-25 20:58:47 +02:00
|
|
|
external = module.params['external']
|
2016-02-18 22:59:15 +01:00
|
|
|
provider_physical_network = module.params['provider_physical_network']
|
|
|
|
provider_network_type = module.params['provider_network_type']
|
|
|
|
provider_segmentation_id = module.params['provider_segmentation_id']
|
2015-03-31 23:05:43 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
cloud = shade.openstack_cloud(**module.params)
|
|
|
|
net = cloud.get_network(name)
|
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
if not net:
|
2016-02-18 22:59:15 +01:00
|
|
|
provider = {}
|
|
|
|
if provider_physical_network:
|
|
|
|
provider['physical_network'] = provider_physical_network
|
|
|
|
if provider_network_type:
|
|
|
|
provider['network_type'] = provider_network_type
|
|
|
|
if provider_segmentation_id:
|
|
|
|
provider['segmentation_id'] = provider_segmentation_id
|
|
|
|
|
|
|
|
if provider and StrictVersion(shade.__version__) < StrictVersion('1.5.0'):
|
|
|
|
module.fail_json(msg="Shade >= 1.5.0 required to use provider options")
|
|
|
|
|
|
|
|
net = cloud.create_network(name, shared, admin_state_up,
|
|
|
|
external, provider)
|
2015-10-22 19:38:32 +02:00
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
changed = False
|
|
|
|
module.exit_json(changed=changed, network=net, id=net['id'])
|
2015-03-31 23:05:43 +02:00
|
|
|
|
|
|
|
elif state == 'absent':
|
|
|
|
if not net:
|
2015-06-17 11:24:08 +02:00
|
|
|
module.exit_json(changed=False)
|
2015-03-31 23:05:43 +02:00
|
|
|
else:
|
|
|
|
cloud.delete_network(name)
|
2015-06-17 11:24:08 +02:00
|
|
|
module.exit_json(changed=True)
|
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
|
|
|
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.openstack import *
|
2015-09-25 20:58:47 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|