Merge pull request #702 from markleehamilton/openvswitch-port-v3
Added support to assign attached mac address interface id and port options.
This commit is contained in:
commit
d5dd5f461f
1 changed files with 140 additions and 17 deletions
|
@ -1,8 +1,12 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
#coding: utf-8 -*-
|
#coding: utf-8 -*-
|
||||||
|
|
||||||
|
# pylint: disable=C0111
|
||||||
|
|
||||||
# (c) 2013, David Stygstra <david.stygstra@gmail.com>
|
# (c) 2013, David Stygstra <david.stygstra@gmail.com>
|
||||||
#
|
#
|
||||||
|
# Portions copyright @ 2015 VMware, Inc.
|
||||||
|
#
|
||||||
# This file is part of Ansible
|
# This file is part of Ansible
|
||||||
#
|
#
|
||||||
# This module is free software: you can redistribute it and/or modify
|
# This module is free software: you can redistribute it and/or modify
|
||||||
|
@ -18,6 +22,8 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this software. If not, see <http://www.gnu.org/licenses/>.
|
# along with this software. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import syslog
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
module: openvswitch_port
|
module: openvswitch_port
|
||||||
|
@ -47,44 +53,138 @@ options:
|
||||||
default: 5
|
default: 5
|
||||||
description:
|
description:
|
||||||
- How long to wait for ovs-vswitchd to respond
|
- How long to wait for ovs-vswitchd to respond
|
||||||
|
external_ids:
|
||||||
|
version_added: 2.0
|
||||||
|
required: false
|
||||||
|
default: {}
|
||||||
|
description:
|
||||||
|
- Dictionary of external_ids applied to a port.
|
||||||
|
set:
|
||||||
|
version_added: 2.0
|
||||||
|
required: false
|
||||||
|
default: None
|
||||||
|
description:
|
||||||
|
- Set a single property on a port.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
# Creates port eth2 on bridge br-ex
|
# Creates port eth2 on bridge br-ex
|
||||||
- openvswitch_port: bridge=br-ex port=eth2 state=present
|
- openvswitch_port: bridge=br-ex port=eth2 state=present
|
||||||
|
|
||||||
|
# Creates port eth6 and set ofport equal to 6.
|
||||||
|
- openvswitch_port: bridge=bridge-loop port=eth6 state=present
|
||||||
|
set Interface eth6 ofport_request=6
|
||||||
|
|
||||||
|
# Assign interface id server1-vifeth6 and mac address 52:54:00:30:6d:11
|
||||||
|
# to port vifeth6 and setup port to be managed by a controller.
|
||||||
|
- openvswitch_port: bridge=br-int port=vifeth6 state=present
|
||||||
|
args:
|
||||||
|
external_ids:
|
||||||
|
iface-id: "{{inventory_hostname}}-vifeth6"
|
||||||
|
attached-mac: "52:54:00:30:6d:11"
|
||||||
|
vm-id: "{{inventory_hostname}}"
|
||||||
|
iface-status: "active"
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
|
||||||
|
def _set_to_get(set_cmd):
|
||||||
|
""" 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("=")
|
||||||
|
syslog.syslog(syslog.LOG_NOTICE, "get commands %s " % key)
|
||||||
|
return (["--", "get"] + get_cmd[:-1] + [key], value)
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=R0902
|
||||||
class OVSPort(object):
|
class OVSPort(object):
|
||||||
|
""" Interface to OVS port. """
|
||||||
def __init__(self, module):
|
def __init__(self, module):
|
||||||
self.module = module
|
self.module = module
|
||||||
self.bridge = module.params['bridge']
|
self.bridge = module.params['bridge']
|
||||||
self.port = module.params['port']
|
self.port = module.params['port']
|
||||||
self.state = module.params['state']
|
self.state = module.params['state']
|
||||||
self.timeout = module.params['timeout']
|
self.timeout = module.params['timeout']
|
||||||
|
self.set_opt = module.params.get('set', None)
|
||||||
|
|
||||||
def _vsctl(self, command):
|
def _vsctl(self, command, check_rc=True):
|
||||||
'''Run ovs-vsctl command'''
|
'''Run ovs-vsctl command'''
|
||||||
return self.module.run_command(['ovs-vsctl', '-t', str(self.timeout)] + command)
|
|
||||||
|
cmd = ['ovs-vsctl', '-t', str(self.timeout)] + command
|
||||||
|
syslog.syslog(syslog.LOG_NOTICE, " ".join(cmd))
|
||||||
|
return self.module.run_command(cmd, check_rc=check_rc)
|
||||||
|
|
||||||
def exists(self):
|
def exists(self):
|
||||||
'''Check if the port already exists'''
|
'''Check if the port already exists'''
|
||||||
rc, out, err = self._vsctl(['list-ports', self.bridge])
|
|
||||||
if rc != 0:
|
(rtc, out, err) = self._vsctl(['list-ports', self.bridge])
|
||||||
raise Exception(err)
|
|
||||||
|
if rtc != 0:
|
||||||
|
self.module.fail_json(msg=err)
|
||||||
|
|
||||||
return any(port.rstrip() == self.port for port in out.split('\n'))
|
return any(port.rstrip() == self.port for port in out.split('\n'))
|
||||||
|
|
||||||
|
def set(self, set_opt):
|
||||||
|
""" Set attributes on a port. """
|
||||||
|
syslog.syslog(syslog.LOG_NOTICE, "set called %s" % set_opt)
|
||||||
|
if (not set_opt):
|
||||||
|
return False
|
||||||
|
|
||||||
|
(get_cmd, set_value) = _set_to_get(set_opt)
|
||||||
|
(rtc, out, err) = self._vsctl(get_cmd, False)
|
||||||
|
if rtc != 0:
|
||||||
|
##
|
||||||
|
# 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('"')
|
||||||
|
|
||||||
|
if (out == set_value):
|
||||||
|
return False
|
||||||
|
|
||||||
|
(rtc, out, err) = self._vsctl(["--", "set"] + set_opt.split(" "))
|
||||||
|
if rtc != 0:
|
||||||
|
self.module.fail_json(msg=err)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
def add(self):
|
def add(self):
|
||||||
'''Add the port'''
|
'''Add the port'''
|
||||||
rc, _, err = self._vsctl(['add-port', self.bridge, self.port])
|
cmd = ['add-port', self.bridge, self.port]
|
||||||
if rc != 0:
|
if self.set and self.set_opt:
|
||||||
raise Exception(err)
|
cmd += ["--", "set"]
|
||||||
|
cmd += self.set_opt.split(" ")
|
||||||
|
|
||||||
|
(rtc, _, err) = self._vsctl(cmd)
|
||||||
|
if rtc != 0:
|
||||||
|
self.module.fail_json(msg=err)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
def delete(self):
|
def delete(self):
|
||||||
'''Remove the port'''
|
'''Remove the port'''
|
||||||
rc, _, err = self._vsctl(['del-port', self.bridge, self.port])
|
(rtc, _, err) = self._vsctl(['del-port', self.bridge, self.port])
|
||||||
if rc != 0:
|
if rtc != 0:
|
||||||
raise Exception(err)
|
self.module.fail_json(msg=err)
|
||||||
|
|
||||||
def check(self):
|
def check(self):
|
||||||
'''Run check mode'''
|
'''Run check mode'''
|
||||||
|
@ -95,8 +195,8 @@ class OVSPort(object):
|
||||||
changed = True
|
changed = True
|
||||||
else:
|
else:
|
||||||
changed = False
|
changed = False
|
||||||
except Exception, e:
|
except Exception, earg:
|
||||||
self.module.fail_json(msg=str(e))
|
self.module.fail_json(msg=str(earg))
|
||||||
self.module.exit_json(changed=changed)
|
self.module.exit_json(changed=changed)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
@ -108,21 +208,40 @@ class OVSPort(object):
|
||||||
self.delete()
|
self.delete()
|
||||||
changed = True
|
changed = True
|
||||||
elif self.state == 'present':
|
elif self.state == 'present':
|
||||||
if not self.exists():
|
##
|
||||||
|
# Add any missing ports.
|
||||||
|
if (not self.exists()):
|
||||||
self.add()
|
self.add()
|
||||||
changed = True
|
changed = True
|
||||||
except Exception, e:
|
|
||||||
self.module.fail_json(msg=str(e))
|
##
|
||||||
|
# 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))
|
||||||
self.module.exit_json(changed=changed)
|
self.module.exit_json(changed=changed)
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=E0602
|
||||||
def main():
|
def main():
|
||||||
|
""" Entry point. """
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec={
|
argument_spec={
|
||||||
'bridge': {'required': True},
|
'bridge': {'required': True},
|
||||||
'port': {'required': True},
|
'port': {'required': True},
|
||||||
'state': {'default': 'present', 'choices': ['present', 'absent']},
|
'state': {'default': 'present', 'choices': ['present', 'absent']},
|
||||||
'timeout': {'default': 5, 'type': 'int'}
|
'timeout': {'default': 5, 'type': 'int'},
|
||||||
|
'set': {'required': False, 'default': None},
|
||||||
|
'external_ids': {'default': {}, 'required': False},
|
||||||
},
|
},
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
)
|
)
|
||||||
|
@ -134,6 +253,10 @@ def main():
|
||||||
port.run()
|
port.run()
|
||||||
|
|
||||||
|
|
||||||
|
# pylint: disable=W0614
|
||||||
|
# pylint: disable=W0401
|
||||||
|
# pylint: disable=W0622
|
||||||
|
|
||||||
# import module snippets
|
# import module snippets
|
||||||
from ansible.module_utils.basic import *
|
from ansible.module_utils.basic import *
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue