2015-03-31 22:37:07 +02:00
|
|
|
#!/usr/bin/python
|
2015-06-26 13:53:20 +02:00
|
|
|
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
|
|
|
|
# Author: Davide Guerri <davide.guerri@hp.com>
|
2015-03-31 22:37:07 +02:00
|
|
|
#
|
|
|
|
# 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
|
2015-06-26 13:53:20 +02:00
|
|
|
from shade import meta
|
|
|
|
|
2015-03-31 22:37:07 +02:00
|
|
|
HAS_SHADE = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_SHADE = False
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: os_floating_ip
|
|
|
|
version_added: "2.0"
|
|
|
|
short_description: Add/Remove floating IP from an instance
|
|
|
|
extends_documentation_fragment: openstack
|
|
|
|
description:
|
|
|
|
- Add or Remove a floating IP to an instance
|
|
|
|
options:
|
|
|
|
server:
|
|
|
|
description:
|
|
|
|
- The name or ID of the instance to which the IP address
|
|
|
|
should be assigned.
|
|
|
|
required: true
|
2015-06-26 13:53:20 +02:00
|
|
|
network:
|
2015-03-31 22:37:07 +02:00
|
|
|
description:
|
2015-06-26 13:53:20 +02:00
|
|
|
- The name or ID of a neutron external network or a nova pool name.
|
|
|
|
required: false
|
|
|
|
floating_ip_address:
|
|
|
|
description:
|
2016-01-14 19:22:15 +01:00
|
|
|
- A floating IP address to attach or to detach. Required only if I(state)
|
|
|
|
is absent. When I(state) is present can be used to specify a IP address
|
2015-06-26 13:53:20 +02:00
|
|
|
to attach.
|
|
|
|
required: false
|
|
|
|
reuse:
|
|
|
|
description:
|
2016-01-14 19:22:15 +01:00
|
|
|
- When I(state) is present, and I(floating_ip_address) is not present,
|
2015-06-26 13:53:20 +02:00
|
|
|
this parameter can be used to specify whether we should try to reuse
|
|
|
|
a floating IP address already allocated to the project.
|
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
fixed_address:
|
|
|
|
description:
|
|
|
|
- To which fixed IP of server the floating IP address should be
|
|
|
|
attached to.
|
|
|
|
required: false
|
|
|
|
wait:
|
2015-03-31 22:37:07 +02:00
|
|
|
description:
|
2015-06-26 13:53:20 +02:00
|
|
|
- When attaching a floating IP address, specify whether we should
|
|
|
|
wait for it to appear as attached.
|
2015-03-31 22:37:07 +02:00
|
|
|
required: false
|
2015-07-08 00:29:47 +02:00
|
|
|
default: false
|
2015-06-26 13:53:20 +02:00
|
|
|
timeout:
|
|
|
|
description:
|
|
|
|
- Time to wait for an IP address to appear as attached. See wait.
|
|
|
|
required: false
|
2015-07-08 00:29:47 +02:00
|
|
|
default: 60
|
2015-03-31 22:37:07 +02:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Should the resource be present or absent.
|
|
|
|
choices: [present, absent]
|
|
|
|
required: false
|
|
|
|
default: present
|
2016-01-14 19:22:15 +01:00
|
|
|
purge:
|
|
|
|
description:
|
|
|
|
- When I(state) is absent, indicates whether or not to delete the floating
|
|
|
|
IP completely, or only detach it from the server. Default is to detach only.
|
|
|
|
required: false
|
|
|
|
default: false
|
2016-01-26 05:43:46 +01:00
|
|
|
version_added: "2.1"
|
2015-03-31 22:37:07 +02:00
|
|
|
requirements: ["shade"]
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2015-06-26 13:53:20 +02:00
|
|
|
# Assign a floating IP to the fist interface of `cattle001` from an exiting
|
2015-06-26 15:53:48 +02:00
|
|
|
# external network or nova pool. A new floating IP from the first available
|
|
|
|
# external network is allocated to the project.
|
2015-03-31 22:37:07 +02:00
|
|
|
- os_floating_ip:
|
2015-06-26 13:53:20 +02:00
|
|
|
cloud: dguerri
|
|
|
|
server: cattle001
|
|
|
|
|
|
|
|
# Assign a new floating IP to the instance fixed ip `192.0.2.3` of
|
2015-06-26 15:53:48 +02:00
|
|
|
# `cattle001`. If a free floating IP is already allocated to the project, it is
|
|
|
|
# reused; if not, a new one is created.
|
2015-06-26 13:53:20 +02:00
|
|
|
- os_floating_ip:
|
|
|
|
cloud: dguerri
|
2015-03-31 22:37:07 +02:00
|
|
|
state: present
|
2015-06-26 15:53:48 +02:00
|
|
|
reuse: yes
|
2015-06-26 13:53:20 +02:00
|
|
|
server: cattle001
|
|
|
|
network: ext_net
|
|
|
|
fixed_address: 192.0.2.3
|
|
|
|
wait: true
|
|
|
|
timeout: 180
|
|
|
|
|
|
|
|
# Detach a floating IP address from a server
|
|
|
|
- os_floating_ip:
|
|
|
|
cloud: dguerri
|
|
|
|
state: absent
|
|
|
|
floating_ip_address: 203.0.113.2
|
|
|
|
server: cattle001
|
2015-03-31 22:37:07 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
2015-06-26 13:53:20 +02:00
|
|
|
def _get_floating_ip(cloud, floating_ip_address):
|
|
|
|
f_ips = cloud.search_floating_ips(
|
|
|
|
filters={'floating_ip_address': floating_ip_address})
|
|
|
|
if not f_ips:
|
2015-03-31 22:37:07 +02:00
|
|
|
return None
|
|
|
|
|
2015-06-26 13:53:20 +02:00
|
|
|
return f_ips[0]
|
2015-03-31 22:37:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = openstack_full_argument_spec(
|
2015-06-26 13:53:20 +02:00
|
|
|
server=dict(required=True),
|
|
|
|
state=dict(default='present', choices=['absent', 'present']),
|
2015-11-09 16:39:56 +01:00
|
|
|
network=dict(required=False, default=None),
|
|
|
|
floating_ip_address=dict(required=False, default=None),
|
2015-06-26 13:53:20 +02:00
|
|
|
reuse=dict(required=False, type='bool', default=False),
|
2015-11-09 16:39:56 +01:00
|
|
|
fixed_address=dict(required=False, default=None),
|
2015-06-26 13:53:20 +02:00
|
|
|
wait=dict(required=False, type='bool', default=False),
|
|
|
|
timeout=dict(required=False, type='int', default=60),
|
2016-01-14 19:22:15 +01:00
|
|
|
purge=dict(required=False, type='bool', default=False),
|
2015-03-31 22:37:07 +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')
|
|
|
|
|
2015-06-26 13:53:20 +02:00
|
|
|
server_name_or_id = module.params['server']
|
2015-03-31 22:37:07 +02:00
|
|
|
state = module.params['state']
|
2015-06-26 13:53:20 +02:00
|
|
|
network = module.params['network']
|
|
|
|
floating_ip_address = module.params['floating_ip_address']
|
|
|
|
reuse = module.params['reuse']
|
|
|
|
fixed_address = module.params['fixed_address']
|
|
|
|
wait = module.params['wait']
|
|
|
|
timeout = module.params['timeout']
|
2016-01-14 19:22:15 +01:00
|
|
|
purge = module.params['purge']
|
2015-03-31 22:37:07 +02:00
|
|
|
|
2015-06-26 13:53:20 +02:00
|
|
|
cloud = shade.openstack_cloud(**module.params)
|
2015-03-31 22:37:07 +02:00
|
|
|
|
2015-06-26 13:53:20 +02:00
|
|
|
try:
|
|
|
|
server = cloud.get_server(server_name_or_id)
|
|
|
|
if server is None:
|
|
|
|
module.fail_json(
|
|
|
|
msg="server {0} not found".format(server_name_or_id))
|
2015-03-31 22:37:07 +02:00
|
|
|
|
|
|
|
if state == 'present':
|
2015-12-10 02:11:10 +01:00
|
|
|
server = cloud.add_ips_to_server(
|
2015-12-13 18:25:34 +01:00
|
|
|
server=server, ips=floating_ip_address, ip_pool=network,
|
|
|
|
reuse=reuse, fixed_address=fixed_address, wait=wait,
|
|
|
|
timeout=timeout)
|
2015-11-09 16:39:56 +01:00
|
|
|
fip_address = cloud.get_server_public_ip(server)
|
2015-06-26 13:53:20 +02:00
|
|
|
# Update the floating IP status
|
2015-11-09 16:39:56 +01:00
|
|
|
f_ip = _get_floating_ip(cloud, fip_address)
|
2015-06-26 13:53:20 +02:00
|
|
|
module.exit_json(changed=True, floating_ip=f_ip)
|
2015-03-31 22:37:07 +02:00
|
|
|
|
|
|
|
elif state == 'absent':
|
2015-06-26 13:53:20 +02:00
|
|
|
if floating_ip_address is None:
|
|
|
|
module.fail_json(msg="floating_ip_address is required")
|
|
|
|
|
|
|
|
f_ip = _get_floating_ip(cloud, floating_ip_address)
|
|
|
|
|
2016-01-14 19:22:15 +01:00
|
|
|
if not f_ip:
|
|
|
|
# Nothing to detach
|
|
|
|
module.exit_json(changed=False)
|
|
|
|
|
2015-06-26 13:53:20 +02:00
|
|
|
cloud.detach_ip_from_server(
|
|
|
|
server_id=server['id'], floating_ip_id=f_ip['id'])
|
|
|
|
# Update the floating IP status
|
|
|
|
f_ip = cloud.get_floating_ip(id=f_ip['id'])
|
2016-01-14 19:22:15 +01:00
|
|
|
if purge:
|
|
|
|
cloud.delete_floating_ip(f_ip['id'])
|
|
|
|
module.exit_json(changed=True)
|
2015-06-26 13:53:20 +02:00
|
|
|
module.exit_json(changed=True, floating_ip=f_ip)
|
2015-03-31 22:37:07 +02:00
|
|
|
|
|
|
|
except shade.OpenStackCloudException as e:
|
2016-01-13 17:00:16 +01:00
|
|
|
module.fail_json(msg=str(e), extra_data=e.extra_data)
|
2015-06-26 13:53:20 +02:00
|
|
|
|
2015-03-31 22:37:07 +02:00
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.openstack import *
|
2015-06-26 13:53:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|