mirror of https://github.com/ansible/ansible.git
fixing merge conflict
commit
2e74b17fad
@ -0,0 +1,158 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
|
||||
#
|
||||
# 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_image_facts
|
||||
short_description: Retrieve facts about an image within OpenStack.
|
||||
version_added: "2.0"
|
||||
author: "Davide Agnello (@dagnello)"
|
||||
description:
|
||||
- Retrieve facts about a image image from OpenStack.
|
||||
notes:
|
||||
- Facts are placed in the C(openstack) variable.
|
||||
requirements:
|
||||
- "python >= 2.6"
|
||||
- "shade"
|
||||
options:
|
||||
image:
|
||||
description:
|
||||
- Name or ID of the image
|
||||
required: true
|
||||
extends_documentation_fragment: openstack
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Gather facts about a previously created image named image1
|
||||
- os_image_facts:
|
||||
auth:
|
||||
auth_url: https://your_api_url.com:9000/v2.0
|
||||
username: user
|
||||
password: password
|
||||
project_name: someproject
|
||||
image: image1
|
||||
- debug: var=openstack
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
openstack_image:
|
||||
description: has all the openstack facts about the image
|
||||
returned: always, but can be null
|
||||
type: complex
|
||||
contains:
|
||||
id:
|
||||
description: Unique UUID.
|
||||
returned: success
|
||||
type: string
|
||||
name:
|
||||
description: Name given to the image.
|
||||
returned: success
|
||||
type: string
|
||||
status:
|
||||
description: Image status.
|
||||
returned: success
|
||||
type: string
|
||||
created_at:
|
||||
description: Image created at timestamp.
|
||||
returned: success
|
||||
type: string
|
||||
deleted:
|
||||
description: Image deleted flag.
|
||||
returned: success
|
||||
type: boolean
|
||||
container_format:
|
||||
description: Container format of the image.
|
||||
returned: success
|
||||
type: string
|
||||
min_ram:
|
||||
description: Min amount of RAM required for this image.
|
||||
returned: success
|
||||
type: int
|
||||
disk_format:
|
||||
description: Disk format of the image.
|
||||
returned: success
|
||||
type: string
|
||||
updated_at:
|
||||
description: Image updated at timestamp.
|
||||
returned: success
|
||||
type: string
|
||||
properties:
|
||||
description: Additional properties associated with the image.
|
||||
returned: success
|
||||
type: dict
|
||||
min_disk:
|
||||
description: Min amount of disk space required for this image.
|
||||
returned: success
|
||||
type: int
|
||||
protected:
|
||||
description: Image protected flag.
|
||||
returned: success
|
||||
type: boolean
|
||||
checksum:
|
||||
description: Checksum for the image.
|
||||
returned: success
|
||||
type: string
|
||||
owner:
|
||||
description: Owner for the image.
|
||||
returned: success
|
||||
type: string
|
||||
is_public:
|
||||
description: Is plubic flag of the image.
|
||||
returned: success
|
||||
type: boolean
|
||||
deleted_at:
|
||||
description: Image deleted at timestamp.
|
||||
returned: success
|
||||
type: string
|
||||
size:
|
||||
description: Size of the image.
|
||||
returned: success
|
||||
type: int
|
||||
'''
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
argument_spec = openstack_full_argument_spec(
|
||||
image=dict(required=True),
|
||||
)
|
||||
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')
|
||||
|
||||
try:
|
||||
cloud = shade.openstack_cloud(**module.params)
|
||||
image = cloud.get_image(module.params['image'])
|
||||
module.exit_json(changed=False, ansible_facts=dict(
|
||||
openstack_image=image))
|
||||
|
||||
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 *
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
@ -0,0 +1,141 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
|
||||
#
|
||||
# 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_networks_facts
|
||||
short_description: Retrieve facts about one or more OpenStack networks.
|
||||
version_added: "2.0"
|
||||
author: "Davide Agnello (@dagnello)"
|
||||
description:
|
||||
- Retrieve facts about one or more networks from OpenStack.
|
||||
requirements:
|
||||
- "python >= 2.6"
|
||||
- "shade"
|
||||
options:
|
||||
network:
|
||||
description:
|
||||
- Name or ID of the Network
|
||||
required: false
|
||||
filters:
|
||||
description:
|
||||
- A dictionary of meta data to use for further filtering. Elements of
|
||||
this dictionary may be additional dictionaries.
|
||||
required: false
|
||||
extends_documentation_fragment: openstack
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Gather facts about previously created networks
|
||||
- os_networks_facts:
|
||||
auth:
|
||||
auth_url: https://your_api_url.com:9000/v2.0
|
||||
username: user
|
||||
password: password
|
||||
project_name: someproject
|
||||
- debug: var=openstack_networks
|
||||
|
||||
# Gather facts about a previously created network by name
|
||||
- os_networks_facts:
|
||||
auth:
|
||||
auth_url: https://your_api_url.com:9000/v2.0
|
||||
username: user
|
||||
password: password
|
||||
project_name: someproject
|
||||
name: network1
|
||||
- debug: var=openstack_networks
|
||||
|
||||
# Gather facts about a previously created network with filter (note: name and
|
||||
filters parameters are Not mutually exclusive)
|
||||
- os_networks_facts:
|
||||
auth:
|
||||
auth_url: https://your_api_url.com:9000/v2.0
|
||||
username: user
|
||||
password: password
|
||||
project_name: someproject
|
||||
filters:
|
||||
tenant_id: 55e2ce24b2a245b09f181bf025724cbe
|
||||
subnets:
|
||||
- 057d4bdf-6d4d-4728-bb0f-5ac45a6f7400
|
||||
- 443d4dc0-91d4-4998-b21c-357d10433483
|
||||
- debug: var=openstack_networks
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
openstack_networks:
|
||||
description: has all the openstack facts about the networks
|
||||
returned: always, but can be null
|
||||
type: complex
|
||||
contains:
|
||||
id:
|
||||
description: Unique UUID.
|
||||
returned: success
|
||||
type: string
|
||||
name:
|
||||
description: Name given to the network.
|
||||
returned: success
|
||||
type: string
|
||||
status:
|
||||
description: Network status.
|
||||
returned: success
|
||||
type: string
|
||||
subnets:
|
||||
description: Subnet(s) included in this network.
|
||||
returned: success
|
||||
type: list of strings
|
||||
tenant_id:
|
||||
description: Tenant id associated with this network.
|
||||
returned: success
|
||||
type: string
|
||||
shared:
|
||||
description: Network shared flag.
|
||||
returned: success
|
||||
type: boolean
|
||||
'''
|
||||
|
||||
def main():
|
||||
|
||||
argument_spec = openstack_full_argument_spec(
|
||||
name=dict(required=False, default=None),
|
||||
filters=dict(required=False, default=None)
|
||||
)
|
||||
module = AnsibleModule(argument_spec)
|
||||
|
||||
if not HAS_SHADE:
|
||||
module.fail_json(msg='shade is required for this module')
|
||||
|
||||
try:
|
||||
cloud = shade.openstack_cloud(**module.params)
|
||||
networks = cloud.search_networks(module.params['name'],
|
||||
module.params['filters'])
|
||||
module.exit_json(changed=False, ansible_facts=dict(
|
||||
openstack_networks=networks))
|
||||
|
||||
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 *
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -0,0 +1,395 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
|
||||
#
|
||||
# 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_port
|
||||
short_description: Add/Update/Delete ports from an OpenStack cloud.
|
||||
extends_documentation_fragment: openstack
|
||||
author: "Davide Agnello (@dagnello)"
|
||||
version_added: "2.0"
|
||||
description:
|
||||
- Add, Update or Remove ports from an OpenStack cloud. A state=present,
|
||||
will ensure the port is created or updated if required.
|
||||
options:
|
||||
network:
|
||||
description:
|
||||
- Network ID or name this port belongs to.
|
||||
required: true
|
||||
name:
|
||||
description:
|
||||
- Name that has to be given to the port.
|
||||
required: false
|
||||
default: None
|
||||
fixed_ips:
|
||||
description:
|
||||
- Desired IP and/or subnet for this port. Subnet is referenced by
|
||||
subnet_id and IP is referenced by ip_address.
|
||||
required: false
|
||||
default: None
|
||||
admin_state_up:
|
||||
description:
|
||||
- Sets admin state.
|
||||
required: false
|
||||
default: None
|
||||
mac_address:
|
||||
description:
|
||||
- MAC address of this port.
|
||||
required: false
|
||||
default: None
|
||||
security_groups:
|
||||
description:
|
||||
- Security group(s) ID(s) or name(s) associated with the port (comma
|
||||
separated for multiple security groups - no spaces between comma(s)
|
||||
or YAML list).
|
||||
required: false
|
||||
default: None
|
||||
no_security_groups:
|
||||
description:
|
||||
- Do not associate a security group with this port.
|
||||
required: false
|
||||
default: False
|
||||
allowed_address_pairs:
|
||||
description:
|
||||
- Allowed address pairs list. Allowed address pairs are supported with
|
||||
dictionary structure.
|
||||
e.g. allowed_address_pairs:
|
||||
- ip_address: 10.1.0.12
|
||||
mac_address: ab:cd:ef:12:34:56
|
||||
- ip_address: ...
|
||||
required: false
|
||||
default: None
|
||||
extra_dhcp_opt:
|
||||
description:
|
||||
- Extra dhcp options to be assigned to this port. Extra options are
|
||||
supported with dictionary structure.
|
||||
e.g. extra_dhcp_opt:
|
||||
- opt_name: opt name1
|
||||
opt_value: value1
|
||||
- opt_name: ...
|
||||
required: false
|
||||
default: None
|
||||
device_owner:
|
||||
description:
|
||||
- The ID of the entity that uses this port.
|
||||
required: false
|
||||
default: None
|
||||
device_id:
|
||||
description:
|
||||
- Device ID of device using this port.
|
||||
required: false
|
||||
default: None
|
||||
state:
|
||||
description:
|
||||
- Should the resource be present or absent.
|
||||
choices: [present, absent]
|
||||
default: present
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Create a port
|
||||
- os_port:
|
||||
state: present
|
||||
auth:
|
||||
auth_url: https://region-b.geo-1.identity.hpcloudsvc.com:35357/v2.0/
|
||||
username: admin
|
||||
password: admin
|
||||
project_name: admin
|
||||
name: port1
|
||||
network: foo
|
||||
|
||||
# Create a port with a static IP
|
||||
- os_port:
|
||||
state: present
|
||||
auth:
|
||||
auth_url: https://region-b.geo-1.identity.hpcloudsvc.com:35357/v2.0/
|
||||
username: admin
|
||||
password: admin
|
||||
project_name: admin
|
||||
name: port1
|
||||
network: foo
|
||||
fixed_ips:
|
||||
- ip_address: 10.1.0.21
|
||||
|
||||
# Create a port with No security groups
|
||||
- os_port:
|
||||
state: present
|
||||
auth:
|
||||
auth_url: https://region-b.geo-1.identity.hpcloudsvc.com:35357/v2.0/
|
||||
username: admin
|
||||
password: admin
|
||||
project_name: admin
|
||||
name: port1
|
||||
network: foo
|
||||
no_security_groups: True
|
||||
|
||||
# Update the existing 'port1' port with multiple security groups (version 1)
|
||||
- os_port:
|
||||
state: present
|
||||
auth:
|
||||
auth_url: https://region-b.geo-1.identity.hpcloudsvc.com:35357/v2.0/d
|
||||
username: admin
|
||||
password: admin
|
||||
project_name: admin
|
||||
name: port1
|
||||
security_groups: 1496e8c7-4918-482a-9172-f4f00fc4a3a5,057d4bdf-6d4d-472...
|
||||
|
||||
# Update the existing 'port1' port with multiple security groups (version 2)
|
||||
- os_port:
|
||||
state: present
|
||||
auth:
|
||||
auth_url: https://region-b.geo-1.identity.hpcloudsvc.com:35357/v2.0/d
|
||||
username: admin
|
||||
password: admin
|
||||
project_name: admin
|
||||
name: port1
|
||||
security_groups:
|
||||
- 1496e8c7-4918-482a-9172-f4f00fc4a3a5
|
||||
- 057d4bdf-6d4d-472...
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
id:
|
||||
description: Unique UUID.
|
||||
returned: success
|
||||
type: string
|
||||
name:
|
||||
description: Name given to the port.
|
||||
returned: success
|
||||
type: string
|
||||
network_id:
|
||||
description: Network ID this port belongs in.
|
||||
returned: success
|
||||
type: string
|
||||
security_groups:
|
||||
description: Security group(s) associated with this port.
|
||||
returned: success
|
||||
type: list of strings
|
||||
status:
|
||||
description: Port's status.
|
||||
returned: success
|
||||
type: string
|
||||
fixed_ips:
|
||||
description: Fixed ip(s) associated with this port.
|
||||
returned: success
|
||||
type: list of dicts
|
||||
tenant_id:
|
||||
description: Tenant id associated with this port.
|
||||
returned: success
|
||||
type: string
|
||||
allowed_address_pairs:
|
||||
description: Allowed address pairs with this port.
|
||||
returned: success
|
||||
type: list of dicts
|
||||
admin_state_up:
|
||||
description: Admin state up flag for this port.
|
||||
returned: success
|
||||
type: bool
|
||||
'''
|
||||
|
||||
|
||||
def _needs_update(module, port, cloud):
|
||||
"""Check for differences in the updatable values.
|
||||
|
||||
NOTE: We don't currently allow name updates.
|
||||
"""
|
||||
compare_simple = ['admin_state_up',
|
||||
'mac_address',
|
||||
'device_owner',
|
||||
'device_id']
|
||||
compare_dict = ['allowed_address_pairs',
|
||||
'extra_dhcp_opt']
|
||||
compare_comma_separated_list = ['security_groups']
|
||||
|
||||
for key in compare_simple:
|
||||
if module.params[key] is not None and module.params[key] != port[key]:
|
||||
return True
|
||||
for key in compare_dict:
|
||||
if module.params[key] is not None and cmp(module.params[key],
|
||||
port[key]) != 0:
|
||||
return True
|
||||
for key in compare_comma_separated_list:
|
||||
if module.params[key] is not None and (set(module.params[key]) !=
|
||||
set(port[key])):
|
||||
return True
|
||||
|
||||
# NOTE: if port was created or updated with 'no_security_groups=True',
|
||||
# subsequent updates without 'no_security_groups' flag or
|
||||
# 'no_security_groups=False' and no specified 'security_groups', will not
|
||||
# result in an update to the port where the default security group is
|
||||
# applied.
|
||||
if module.params['no_security_groups'] and port['security_groups'] != []:
|
||||
return True
|
||||
|
||||
if module.params['fixed_ips'] is not None:
|
||||
for item in module.params['fixed_ips']:
|
||||
if 'ip_address' in item:
|
||||
# if ip_address in request does not match any in existing port,
|
||||
# update is required.
|
||||
if not any(match['ip_address'] == item['ip_address']
|
||||
for match in port['fixed_ips']):
|
||||
return True
|
||||
if 'subnet_id' in item:
|
||||
return True
|
||||
for item in port['fixed_ips']:
|
||||
# if ip_address in existing port does not match any in request,
|
||||
# update is required.
|
||||
if not any(match.get('ip_address') == item['ip_address']
|
||||
for match in module.params['fixed_ips']):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def _system_state_change(module, port, cloud):
|
||||
state = module.params['state']
|
||||
if state == 'present':
|
||||
if not port:
|
||||
return True
|
||||
return _needs_update(module, port, cloud)
|
||||
if state == 'absent' and port:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _compose_port_args(module, cloud):
|
||||
port_kwargs = {}
|
||||
optional_parameters = ['name',
|
||||
'fixed_ips',
|
||||
'admin_state_up',
|
||||
'mac_address',
|
||||
'security_groups',
|
||||
'allowed_address_pairs',
|
||||
'extra_dhcp_opt',
|
||||
'device_owner',
|
||||
'device_id']
|
||||
for optional_param in optional_parameters:
|
||||
if module.params[optional_param] is not None:
|
||||
port_kwargs[optional_param] = module.params[optional_param]
|
||||
|
||||
if module.params['no_security_groups']:
|
||||
port_kwargs['security_groups'] = []
|
||||
|
||||
return port_kwargs
|
||||
|
||||
|
||||
def get_security_group_id(module, cloud, security_group_name_or_id):
|
||||
security_group = cloud.get_security_group(security_group_name_or_id)
|
||||
if not security_group:
|
||||
module.fail_json(msg="Security group: %s, was not found"
|
||||
% security_group_name_or_id)
|
||||
return security_group['id']
|
||||
|
||||
|
||||
def main():
|
||||
argument_spec = openstack_full_argument_spec(
|
||||
network=dict(required=False),
|
||||
name=dict(required=False),
|
||||
fixed_ips=dict(default=None),
|
||||
admin_state_up=dict(default=None),
|
||||
mac_address=dict(default=None),
|
||||
security_groups=dict(default=None),
|
||||
no_security_groups=dict(default=False, type='bool'),
|
||||
allowed_address_pairs=dict(default=None),
|
||||
extra_dhcp_opt=dict(default=None),
|
||||
device_owner=dict(default=None),
|
||||
device_id=dict(default=None),
|
||||
state=dict(default='present', choices=['absent', 'present']),
|
||||
)
|
||||
|
||||
module_kwargs = openstack_module_kwargs(
|
||||
mutually_exclusive=[
|
||||
['no_security_groups', 'security_groups'],
|
||||
]
|
||||
)
|
||||
|
||||
module = AnsibleModule(argument_spec,
|
||||
supports_check_mode=True,
|
||||
**module_kwargs)
|
||||
|
||||
if not HAS_SHADE:
|
||||
module.fail_json(msg='shade is required for this module')
|
||||
name = module.params['name']
|
||||
state = module.params['state']
|
||||
|
||||
try:
|
||||
cloud = shade.openstack_cloud(**module.params)
|
||||
if module.params['security_groups']:
|
||||
if type(module.params['security_groups']) == str:
|
||||
module.params['security_groups'] = module.params[
|
||||
'security_groups'].split(',')
|
||||
# translate security_groups to UUID's if names where provided
|
||||
module.params['security_groups'] = map(
|
||||
lambda v: get_security_group_id(module, cloud, v),
|
||||
module.params['security_groups'])
|
||||
|
||||
port = None
|
||||
network_id = None
|
||||
if name:
|
||||
port = cloud.get_port(name)
|
||||
|
||||
if module.check_mode:
|
||||
module.exit_json(changed=_system_state_change(module, port, cloud))
|
||||
|
||||
changed = False
|
||||
if state == 'present':
|
||||
if not port:
|
||||
network = module.params['network']
|
||||
if not network:
|
||||
module.fail_json(
|
||||
msg="Parameter 'network' is required in Port Create"
|
||||
)
|
||||
port_kwargs = _compose_port_args(module, cloud)
|
||||
network_object = cloud.get_network(network)
|
||||
|
||||
if network_object:
|
||||
network_id = network_object['id']
|
||||
else:
|
||||
module.fail_json(
|
||||
msg="Specified network was not found."
|
||||
)
|
||||
|
||||
port = cloud.create_port(network_id, **port_kwargs)
|
||||
changed = True
|
||||
else:
|
||||
if _needs_update(module, port, cloud):
|
||||
port_kwargs = _compose_port_args(module, cloud)
|
||||
port = cloud.update_port(port['id'], **port_kwargs)
|
||||
changed = True
|
||||
module.exit_json(changed=changed, id=port['id'], port=port)
|
||||
|
||||
if state == 'absent':
|
||||
if port:
|
||||
cloud.delete_port(port['id'])
|
||||
changed = True
|
||||
module.exit_json(changed=changed)
|
||||
|
||||
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 *
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -0,0 +1,299 @@
|
||||
#!/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
|
||||
short_description: Create or delete routers from OpenStack
|
||||
extends_documentation_fragment: openstack
|
||||
version_added: "2.0"
|
||||
author: "David Shrewsbury (@Shrews)"
|
||||
description:
|
||||
- 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.
|
||||
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:
|
||||
- Desired admin state of the created or existing router.
|
||||
required: false
|
||||
default: true
|
||||
enable_snat:
|
||||
description:
|
||||
- Enable Source NAT (SNAT) attribute.
|
||||
required: false
|
||||
default: true
|
||||
network:
|
||||
description:
|
||||
- Unique name or ID of the external gateway network.
|
||||
type: string
|
||||
required: true when I(interfaces) or I(enable_snat) are provided,
|
||||
false otherwise.
|
||||
default: None
|
||||
interfaces:
|
||||
description:
|
||||
- List of subnets to attach to the router. Each is a dictionary with
|
||||
the subnet name or ID (subnet) and the IP address to assign on that
|
||||
subnet (ip). If no IP is specified, one is automatically assigned from
|
||||
that subnet.
|
||||
required: false
|
||||
default: None
|
||||
requirements: ["shade"]
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Create a simple router, not attached to a gateway or subnets.
|
||||
- os_router:
|
||||
cloud: mycloud
|
||||
state: present
|
||||
name: simple_router
|
||||
|
||||
# Creates a router attached to ext_network1 and one subnet interface.
|
||||
# An IP address from subnet1's IP range will automatically be assigned
|
||||
# to that interface.
|
||||
- os_router:
|
||||
cloud: mycloud
|
||||
state: present
|
||||
name: router1
|
||||
network: ext_network1
|
||||
interfaces:
|
||||
- subnet: subnet1
|
||||
|
||||
# Update existing router1 to include subnet2 (10.5.5.0/24), specifying
|
||||
# the IP address within subnet2's IP range we'd like for that interface.
|
||||
- os_router:
|
||||
cloud: mycloud
|
||||
state: present
|
||||
name: router1
|
||||
network: ext_network1
|
||||
interfaces:
|
||||
- subnet: subnet1
|
||||
- subnet: subnet2
|
||||
ip: 10.5.5.1
|
||||
|
||||
# Delete router1
|
||||
- os_router:
|
||||
cloud: mycloud
|
||||
state: absent
|
||||
name: router1
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
router:
|
||||
description: Dictionary describing the router.
|
||||
returned: On success when I(state) is 'present'
|
||||
type: dictionary
|
||||
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"
|
||||
}
|
||||
]
|
||||
}
|
||||
routes:
|
||||
description: The extra routes configuration for L3 router.
|
||||
type: list
|
||||
'''
|
||||
|
||||
|
||||
def _needs_update(cloud, module, router, network):
|
||||
"""Decide if the given router needs an update.
|
||||
"""
|
||||
if router['admin_state_up'] != module.params['admin_state_up']:
|
||||
return True
|
||||
if router['external_gateway_info']['enable_snat'] != module.params['enable_snat']:
|
||||
return True
|
||||
if network:
|
||||
if router['external_gateway_info']['network_id'] != network['id']:
|
||||
return True
|
||||
|
||||
# check subnet interfaces
|
||||
for new_iface in module.params['interfaces']:
|
||||
subnet = cloud.get_subnet(new_iface['subnet'])
|
||||
if not subnet:
|
||||
module.fail_json(msg='subnet %s not found' % 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
|
||||
exists = True
|
||||
break
|
||||
|
||||
# this interface isn't present on the existing router
|
||||
if not exists:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def _system_state_change(cloud, module, router, network):
|
||||
"""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
|
||||
return _needs_update(cloud, module, router, network)
|
||||
return False
|
||||
|
||||
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']
|
||||
|
||||
if module.params['interfaces']:
|
||||
kwargs['ext_fixed_ips'] = []
|
||||
for iface in module.params['interfaces']:
|
||||
subnet = cloud.get_subnet(iface['subnet'])
|
||||
if not subnet:
|
||||
module.fail_json(msg='subnet %s not found' % iface['subnet'])
|
||||
d = {'subnet_id': subnet['id']}
|
||||
if 'ip' in iface:
|
||||
d['ip_address'] = iface['ip']
|
||||
kwargs['ext_fixed_ips'].append(d)
|
||||
|
||||
return kwargs
|
||||
|
||||
def main():
|
||||
argument_spec = openstack_full_argument_spec(
|
||||
state=dict(default='present', choices=['absent', 'present']),
|
||||
name=dict(required=True),
|
||||
admin_state_up=dict(type='bool', default=True),
|
||||
enable_snat=dict(type='bool', default=True),
|
||||
network=dict(default=None),
|
||||
interfaces=dict(type='list', default=None)
|
||||
)
|
||||
|
||||
module_kwargs = openstack_module_kwargs()
|
||||
module = AnsibleModule(argument_spec,
|
||||
supports_check_mode=True,
|
||||
**module_kwargs)
|
||||
|
||||
if not HAS_SHADE:
|
||||
module.fail_json(msg='shade is required for this module')
|
||||
|
||||
state = module.params['state']
|
||||
name = module.params['name']
|
||||
network = module.params['network']
|
||||
|
||||
if module.params['interfaces'] and not network:
|
||||
module.fail_json(msg='network is required when supplying interfaces')
|
||||
|
||||
try:
|
||||
cloud = shade.openstack_cloud(**module.params)
|
||||
router = cloud.get_router(name)
|
||||
|
||||
net = None
|
||||
if network:
|
||||
net = cloud.get_network(network)
|
||||
if not net:
|
||||
module.fail_json(msg='network %s not found' % network)
|
||||
|
||||
if module.check_mode:
|
||||
module.exit_json(
|
||||
changed=_system_state_change(cloud, module, router, net)
|
||||
)
|
||||
|
||||
if state == 'present':
|
||||
changed = False
|
||||
|
||||
if not router:
|
||||
kwargs = _build_kwargs(cloud, module, router, net)
|
||||
router = cloud.create_router(**kwargs)
|
||||
changed = True
|
||||
else:
|
||||
if _needs_update(cloud, module, router, net):
|
||||
kwargs = _build_kwargs(cloud, module, router, net)
|
||||
router = cloud.update_router(**kwargs)
|
||||
changed = True
|
||||
|
||||
module.exit_json(changed=changed, router=router)
|
||||
|
||||
elif state == 'absent':
|
||||
if not router:
|
||||
module.exit_json(changed=False)
|
||||
else:
|
||||
cloud.delete_router(name)
|
||||
module.exit_json(changed=True)
|
||||
|
||||
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 *
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -0,0 +1,152 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
|
||||
#
|
||||
# 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_subnets_facts
|
||||
short_description: Retrieve facts about one or more OpenStack subnets.
|
||||
version_added: "2.0"
|
||||
author: "Davide Agnello (@dagnello)"
|
||||
description:
|
||||
- Retrieve facts about one or more subnets from OpenStack.
|
||||
requirements:
|
||||
- "python >= 2.6"
|
||||
- "shade"
|
||||
options:
|
||||
subnet:
|
||||
description:
|
||||
- Name or ID of the subnet
|
||||
required: false
|
||||
filters:
|
||||
description:
|
||||
- A dictionary of meta data to use for further filtering. Elements of
|
||||
this dictionary may be additional dictionaries.
|
||||
required: false
|
||||
extends_documentation_fragment: openstack
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# Gather facts about previously created subnets
|
||||
- os_subnets_facts:
|
||||
auth:
|
||||
auth_url: https://your_api_url.com:9000/v2.0
|
||||
username: user
|
||||
password: password
|
||||
project_name: someproject
|
||||
- debug: var=openstack_subnets
|
||||
|
||||
# Gather facts about a previously created subnet by name
|
||||
- os_subnets_facts:
|
||||
auth:
|
||||
auth_url: https://your_api_url.com:9000/v2.0
|
||||
username: user
|
||||
password: password
|
||||
project_name: someproject
|
||||
name: subnet1
|
||||
- debug: var=openstack_subnets
|
||||
|
||||
# Gather facts about a previously created subnet with filter (note: name and
|
||||
filters parameters are Not mutually exclusive)
|
||||
- os_subnets_facts:
|
||||
auth:
|
||||
auth_url: https://your_api_url.com:9000/v2.0
|
||||
username: user
|
||||
password: password
|
||||
project_name: someproject
|
||||
filters:
|
||||
tenant_id: 55e2ce24b2a245b09f181bf025724cbe
|
||||
- debug: var=openstack_subnets
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
This module registers subnet details in facts named: openstack_subnets. If a
|
||||
subnet name/id and or filter does not result in a subnet found, an empty list
|
||||
is set in openstack_subnets.
|
||||
id:
|
||||
description: Unique UUID.
|
||||
returned: success
|
||||
type: string
|
||||
name:
|
||||
description: Name given to the subnet.
|
||||
returned: success
|
||||
type: string
|
||||
network_id:
|
||||
description: Network ID this subnet belongs in.
|
||||
returned: success
|
||||
type: string
|
||||
cidr:
|
||||
description: Subnet's CIDR.
|
||||
returned: success
|
||||
type: string
|
||||
gateway_ip:
|
||||
description: Subnet's gateway ip.
|
||||
returned: success
|
||||
type: string
|
||||
enable_dhcp:
|
||||
description: DHCP enable flag for this subnet.
|
||||
returned: success
|
||||
type: bool
|
||||
ip_version:
|
||||
description: IP version for this subnet.
|
||||
returned: success
|
||||
type: int
|
||||
tenant_id:
|
||||
description: Tenant id associated with this subnet.
|
||||
returned: success
|
||||
type: string
|
||||
dns_nameservers:
|
||||
description: DNS name servers for this subnet.
|
||||
returned: success
|
||||
type: list of strings
|
||||
allocation_pools:
|
||||
description: Allocation pools associated with this subnet.
|
||||
returned: success
|
||||
type: list of dicts
|
||||
'''
|
||||
|
||||
def main():
|
||||
|
||||
argument_spec = openstack_full_argument_spec(
|
||||
name=dict(required=False, default=None),
|
||||
filters=dict(required=False, default=None)
|
||||
)
|
||||
module = AnsibleModule(argument_spec)
|
||||
|
||||
if not HAS_SHADE:
|
||||
module.fail_json(msg='shade is required for this module')
|
||||
|
||||
try:
|
||||
cloud = shade.openstack_cloud(**module.params)
|
||||
subnets = cloud.search_subnets(module.params['name'],
|
||||
module.params['filters'])
|
||||
module.exit_json(changed=False, ansible_facts=dict(
|
||||
openstack_subnets=subnets))
|
||||
|
||||
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 *
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue