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
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: os_network
|
|
|
|
short_description: Creates/Removes networks from OpenStack
|
|
|
|
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:
|
|
|
|
- Add or Remove network from OpenStack.
|
|
|
|
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
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicate desired state of the resource.
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
required: false
|
|
|
|
default: present
|
|
|
|
requirements: ["shade"]
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- os_network:
|
2015-08-28 14:34:05 +02:00
|
|
|
name: t1network
|
|
|
|
state: present
|
|
|
|
auth:
|
|
|
|
auth_url: https://your_api_url.com:9000/v2.0
|
|
|
|
username: user
|
|
|
|
password: password
|
|
|
|
project_name: someproject
|
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-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']
|
|
|
|
|
|
|
|
try:
|
|
|
|
cloud = shade.openstack_cloud(**module.params)
|
|
|
|
net = cloud.get_network(name)
|
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
if not net:
|
|
|
|
net = cloud.create_network(name, shared, admin_state_up)
|
2015-06-17 11:24:08 +02:00
|
|
|
module.exit_json(changed=False, 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:
|
|
|
|
module.fail_json(msg=e.message)
|
|
|
|
|
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.openstack import *
|
|
|
|
main()
|