2014-01-15 15:52:17 +01:00
|
|
|
#!/usr/bin/python
|
2013-09-19 01:28:41 +02:00
|
|
|
#coding: utf-8 -*-
|
|
|
|
|
2015-07-09 09:36:30 +02:00
|
|
|
# pylint: disable=C0111
|
|
|
|
|
2014-06-20 02:02:02 +02:00
|
|
|
# (c) 2013, David Stygstra <david.stygstra@gmail.com>
|
|
|
|
#
|
2015-07-14 10:49:16 +02:00
|
|
|
# Portions copyright @ 2015 VMware, Inc.
|
|
|
|
#
|
2014-06-20 02:02:02 +02:00
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
2013-09-19 01:28:41 +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/>.
|
2015-07-09 09:36:30 +02:00
|
|
|
|
2013-09-19 01:28:41 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: openvswitch_port
|
2013-11-19 00:55:49 +01:00
|
|
|
version_added: 1.4
|
2015-06-16 20:32:39 +02:00
|
|
|
author: "David Stygstra (@stygstra)"
|
2013-09-19 01:28:41 +02:00
|
|
|
short_description: Manage Open vSwitch ports
|
|
|
|
requirements: [ ovs-vsctl ]
|
|
|
|
description:
|
|
|
|
- Manage Open vSwitch ports
|
|
|
|
options:
|
|
|
|
bridge:
|
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
- Name of bridge to manage
|
|
|
|
port:
|
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
- Name of port to manage on the bridge
|
|
|
|
state:
|
|
|
|
required: false
|
|
|
|
default: "present"
|
|
|
|
choices: [ present, absent ]
|
|
|
|
description:
|
|
|
|
- Whether the port should exist
|
|
|
|
timeout:
|
|
|
|
required: false
|
|
|
|
default: 5
|
|
|
|
description:
|
|
|
|
- How long to wait for ovs-vswitchd to respond
|
2015-07-14 10:49:16 +02:00
|
|
|
external_ids:
|
|
|
|
version_added: 2.0
|
2015-07-09 09:36:30 +02:00
|
|
|
required: false
|
2015-07-14 10:49:16 +02:00
|
|
|
default: {}
|
2015-07-09 09:36:30 +02:00
|
|
|
description:
|
2015-07-14 10:49:16 +02:00
|
|
|
- Dictionary of external_ids applied to a port.
|
2015-07-09 09:36:30 +02:00
|
|
|
set:
|
2015-07-14 10:49:16 +02:00
|
|
|
version_added: 2.0
|
2015-07-09 09:36:30 +02:00
|
|
|
required: false
|
2015-07-14 10:49:16 +02:00
|
|
|
default: None
|
2015-07-09 09:36:30 +02:00
|
|
|
description:
|
2015-07-14 10:49:16 +02:00
|
|
|
- Set a single property on a port.
|
2013-09-19 01:28:41 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Creates port eth2 on bridge br-ex
|
|
|
|
- openvswitch_port: bridge=br-ex port=eth2 state=present
|
2015-07-09 09:36:30 +02:00
|
|
|
|
|
|
|
# Creates port eth6 and set ofport equal to 6.
|
|
|
|
- openvswitch_port: bridge=bridge-loop port=eth6 state=present
|
2016-03-22 12:40:50 +01:00
|
|
|
set="Interface eth6 ofport_request=6"
|
2015-07-09 09:36:30 +02:00
|
|
|
|
|
|
|
# Assign interface id server1-vifeth6 and mac address 52:54:00:30:6d:11
|
2015-07-14 10:49:16 +02:00
|
|
|
# to port vifeth6 and setup port to be managed by a controller.
|
2015-07-09 09:36:30 +02:00
|
|
|
- openvswitch_port: bridge=br-int port=vifeth6 state=present
|
|
|
|
args:
|
|
|
|
external_ids:
|
2015-07-14 10:49:16 +02:00
|
|
|
iface-id: "{{inventory_hostname}}-vifeth6"
|
2015-07-09 09:36:30 +02:00
|
|
|
attached-mac: "52:54:00:30:6d:11"
|
2015-07-14 10:49:16 +02:00
|
|
|
vm-id: "{{inventory_hostname}}"
|
|
|
|
iface-status: "active"
|
2013-09-19 01:28:41 +02:00
|
|
|
'''
|
|
|
|
|
2015-07-09 09:36:30 +02:00
|
|
|
# pylint: disable=W0703
|
|
|
|
|
|
|
|
|
|
|
|
def truncate_before(value, srch):
|
|
|
|
""" Return content of str before the srch parameters. """
|
|
|
|
|
|
|
|
before_index = value.find(srch)
|
|
|
|
if (before_index >= 0):
|
|
|
|
return value[:before_index]
|
|
|
|
else:
|
|
|
|
return value
|
|
|
|
|
2013-09-19 01:28:41 +02:00
|
|
|
|
2015-10-01 06:29:57 +02:00
|
|
|
def _set_to_get(set_cmd, module):
|
2015-07-09 09:36:30 +02:00
|
|
|
""" Convert set command to get command and set value.
|
|
|
|
return tuple (get command, set value)
|
|
|
|
"""
|
|
|
|
|
|
|
|
##
|
|
|
|
# If set has option: then we want to truncate just before that.
|
|
|
|
set_cmd = truncate_before(set_cmd, " option:")
|
|
|
|
get_cmd = set_cmd.split(" ")
|
|
|
|
(key, value) = get_cmd[-1].split("=")
|
2015-10-01 06:29:57 +02:00
|
|
|
module.log("get commands %s " % key)
|
2015-07-09 09:36:30 +02:00
|
|
|
return (["--", "get"] + get_cmd[:-1] + [key], value)
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=R0902
|
2013-09-19 01:28:41 +02:00
|
|
|
class OVSPort(object):
|
2015-07-09 09:36:30 +02:00
|
|
|
""" Interface to OVS port. """
|
2013-09-19 01:28:41 +02:00
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
self.bridge = module.params['bridge']
|
|
|
|
self.port = module.params['port']
|
|
|
|
self.state = module.params['state']
|
|
|
|
self.timeout = module.params['timeout']
|
2015-07-09 09:36:30 +02:00
|
|
|
self.set_opt = module.params.get('set', None)
|
|
|
|
|
2015-07-14 10:49:16 +02:00
|
|
|
def _vsctl(self, command, check_rc=True):
|
2013-09-19 01:28:41 +02:00
|
|
|
'''Run ovs-vsctl command'''
|
2015-07-09 09:36:30 +02:00
|
|
|
|
|
|
|
cmd = ['ovs-vsctl', '-t', str(self.timeout)] + command
|
2015-07-14 10:49:16 +02:00
|
|
|
return self.module.run_command(cmd, check_rc=check_rc)
|
2013-09-19 01:28:41 +02:00
|
|
|
|
|
|
|
def exists(self):
|
|
|
|
'''Check if the port already exists'''
|
2015-07-09 09:36:30 +02:00
|
|
|
|
|
|
|
(rtc, out, err) = self._vsctl(['list-ports', self.bridge])
|
|
|
|
|
|
|
|
if rtc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
|
|
|
|
2013-09-19 01:28:41 +02:00
|
|
|
return any(port.rstrip() == self.port for port in out.split('\n'))
|
|
|
|
|
2015-07-09 09:36:30 +02:00
|
|
|
def set(self, set_opt):
|
|
|
|
""" Set attributes on a port. """
|
2015-11-06 04:16:41 +01:00
|
|
|
self.module.log("set called %s" % set_opt)
|
2015-07-09 09:36:30 +02:00
|
|
|
if (not set_opt):
|
|
|
|
return False
|
|
|
|
|
2015-10-01 06:29:57 +02:00
|
|
|
(get_cmd, set_value) = _set_to_get(set_opt, self.module)
|
2015-07-14 10:49:16 +02:00
|
|
|
(rtc, out, err) = self._vsctl(get_cmd, False)
|
2015-07-09 09:36:30 +02:00
|
|
|
if rtc != 0:
|
2015-07-14 10:49:16 +02:00
|
|
|
##
|
|
|
|
# ovs-vsctl -t 5 -- get Interface port external_ids:key
|
|
|
|
# returns failure if key does not exist.
|
|
|
|
out = None
|
|
|
|
else:
|
|
|
|
out = out.strip("\n")
|
|
|
|
out = out.strip('"')
|
2015-07-09 09:36:30 +02:00
|
|
|
|
|
|
|
if (out == set_value):
|
|
|
|
return False
|
|
|
|
|
|
|
|
(rtc, out, err) = self._vsctl(["--", "set"] + set_opt.split(" "))
|
|
|
|
if rtc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
|
|
|
|
2015-07-14 10:49:16 +02:00
|
|
|
return True
|
2015-07-09 09:36:30 +02:00
|
|
|
|
2013-09-19 01:28:41 +02:00
|
|
|
def add(self):
|
|
|
|
'''Add the port'''
|
2015-07-09 09:36:30 +02:00
|
|
|
cmd = ['add-port', self.bridge, self.port]
|
|
|
|
if self.set and self.set_opt:
|
|
|
|
cmd += ["--", "set"]
|
|
|
|
cmd += self.set_opt.split(" ")
|
|
|
|
|
|
|
|
(rtc, _, err) = self._vsctl(cmd)
|
|
|
|
if rtc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
|
|
|
|
2015-07-14 10:49:16 +02:00
|
|
|
return True
|
2013-09-19 01:28:41 +02:00
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
'''Remove the port'''
|
2015-07-09 09:36:30 +02:00
|
|
|
(rtc, _, err) = self._vsctl(['del-port', self.bridge, self.port])
|
|
|
|
if rtc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
2013-09-19 01:28:41 +02:00
|
|
|
|
|
|
|
def check(self):
|
|
|
|
'''Run check mode'''
|
|
|
|
try:
|
|
|
|
if self.state == 'absent' and self.exists():
|
|
|
|
changed = True
|
|
|
|
elif self.state == 'present' and not self.exists():
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
changed = False
|
2015-07-09 09:36:30 +02:00
|
|
|
except Exception, earg:
|
|
|
|
self.module.fail_json(msg=str(earg))
|
2013-09-19 01:28:41 +02:00
|
|
|
self.module.exit_json(changed=changed)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
'''Make the necessary changes'''
|
2013-10-13 18:35:53 +02:00
|
|
|
changed = False
|
2013-09-19 01:28:41 +02:00
|
|
|
try:
|
|
|
|
if self.state == 'absent':
|
|
|
|
if self.exists():
|
|
|
|
self.delete()
|
2013-10-13 18:35:53 +02:00
|
|
|
changed = True
|
2013-09-19 01:28:41 +02:00
|
|
|
elif self.state == 'present':
|
2015-07-09 09:36:30 +02:00
|
|
|
##
|
|
|
|
# Add any missing ports.
|
|
|
|
if (not self.exists()):
|
2013-09-19 01:28:41 +02:00
|
|
|
self.add()
|
2013-10-13 18:35:53 +02:00
|
|
|
changed = True
|
2015-07-09 09:36:30 +02:00
|
|
|
|
|
|
|
##
|
|
|
|
# If the -- set changed check here and make changes
|
|
|
|
# but this only makes sense when state=present.
|
|
|
|
if (not changed):
|
|
|
|
changed = self.set(self.set_opt) or changed
|
|
|
|
items = self.module.params['external_ids'].items()
|
|
|
|
for (key, value) in items:
|
|
|
|
value = value.replace('"', '')
|
|
|
|
fmt_opt = "Interface %s external_ids:%s=%s"
|
|
|
|
external_id = fmt_opt % (self.port, key, value)
|
|
|
|
changed = self.set(external_id) or changed
|
|
|
|
##
|
|
|
|
except Exception, earg:
|
|
|
|
self.module.fail_json(msg=str(earg))
|
2013-10-13 18:35:53 +02:00
|
|
|
self.module.exit_json(changed=changed)
|
2013-09-19 01:28:41 +02:00
|
|
|
|
|
|
|
|
2015-07-09 09:36:30 +02:00
|
|
|
# pylint: disable=E0602
|
2013-09-19 01:28:41 +02:00
|
|
|
def main():
|
2015-07-09 09:36:30 +02:00
|
|
|
""" Entry point. """
|
2013-09-19 01:28:41 +02:00
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec={
|
|
|
|
'bridge': {'required': True},
|
|
|
|
'port': {'required': True},
|
|
|
|
'state': {'default': 'present', 'choices': ['present', 'absent']},
|
2015-07-09 09:36:30 +02:00
|
|
|
'timeout': {'default': 5, 'type': 'int'},
|
2015-07-14 10:49:16 +02:00
|
|
|
'set': {'required': False, 'default': None},
|
2015-07-09 09:36:30 +02:00
|
|
|
'external_ids': {'default': {}, 'required': False},
|
2013-09-19 01:28:41 +02:00
|
|
|
},
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
port = OVSPort(module)
|
|
|
|
if module.check_mode:
|
|
|
|
port.check()
|
|
|
|
else:
|
|
|
|
port.run()
|
|
|
|
|
|
|
|
|
2015-07-09 09:36:30 +02:00
|
|
|
# pylint: disable=W0614
|
|
|
|
# pylint: disable=W0401
|
|
|
|
# pylint: disable=W0622
|
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2013-09-19 01:28:41 +02:00
|
|
|
main()
|