removed syslog. Generalized external id concept. Now user can add or remove multiple external ids. Added documenation about fail_main parameter.

This commit is contained in:
Mark Hamilton 2015-07-14 05:17:38 -07:00
parent 647cac63f1
commit 90a831a828

View file

@ -3,6 +3,8 @@
# (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
@ -17,8 +19,6 @@
# #
# 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/>.
#
# Portions copyright @ 2015 VMware, Inc. All rights reserved.
# pylint: disable=C0111 # pylint: disable=C0111
@ -47,15 +47,20 @@ 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_id: external_ids:
version_added: 2.0
required: false required: false
default: None
description: description:
- bridge external-id - A dictionary of external-ids. Omitting this parameter is a No-op.
To clear all external-ids pass an empty value.
fail_mode: fail_mode:
version_added: 2.0
default: None
required: false required: false
choices : [secure, standalone] choices : [secure, standalone]
description: description:
- bridge fail-mode - Set bridge fail-mode. The default value (None) is a No-op.
''' '''
EXAMPLES = ''' EXAMPLES = '''
@ -63,12 +68,13 @@ EXAMPLES = '''
- openvswitch_bridge: bridge=br-int state=present - openvswitch_bridge: bridge=br-int state=present
# Create an integration bridge # Create an integration bridge
- openvswitch_bridge: bridge=br-int state=present external_id=br-int - openvswitch_bridge: bridge=br-int state=present fail_mode=secure
fail_mode=secure args:
external_ids:
bridge-id: "br-int"
''' '''
import syslog import syslog
import os
class OVSBridge(object): class OVSBridge(object):
@ -78,7 +84,6 @@ class OVSBridge(object):
self.bridge = module.params['bridge'] self.bridge = module.params['bridge']
self.state = module.params['state'] self.state = module.params['state']
self.timeout = module.params['timeout'] self.timeout = module.params['timeout']
self.external_id = module.params['external_id']
self.fail_mode = module.params['fail_mode'] self.fail_mode = module.params['fail_mode']
def _vsctl(self, command): def _vsctl(self, command):
@ -100,8 +105,6 @@ class OVSBridge(object):
rtc, _, err = self._vsctl(['add-br', self.bridge]) rtc, _, err = self._vsctl(['add-br', self.bridge])
if rtc != 0: if rtc != 0:
self.module.fail_json(msg=err) self.module.fail_json(msg=err)
if self.external_id:
self.set_external_id()
if self.fail_mode: if self.fail_mode:
self.set_fail_mode() self.set_fail_mode()
@ -118,12 +121,25 @@ class OVSBridge(object):
# pylint: disable=W0703 # pylint: disable=W0703
try: try:
if self.state == 'present' and self.exists(): if self.state == 'present' and self.exists():
if (self.external_id and
(self.external_id != self.get_external_id())):
changed = True
if (self.fail_mode and if (self.fail_mode and
(self.fail_mode != self.get_fail_mode())): (self.fail_mode != self.get_fail_mode())):
changed = True changed = True
##
# Check if external ids would change.
current_external_ids = self.get_external_ids()
items = self.module.params['external_ids'].items()
for (key, value) in items:
if ((key in current_external_ids) and
(value != current_external_ids[key])):
changed = True
##
# Check if external ids would be removed.
for (key, value) in current_external_ids.items():
if key not in self.module.params['external_ids']:
changed = True
elif self.state == 'absent' and self.exists(): elif self.state == 'absent' and self.exists():
changed = True changed = True
elif self.state == 'present' and not self.exists(): elif self.state == 'present' and not self.exists():
@ -145,15 +161,11 @@ class OVSBridge(object):
self.delete() self.delete()
changed = True changed = True
elif self.state == 'present': elif self.state == 'present':
if not self.exists(): if not self.exists():
self.add() self.add()
changed = True changed = True
if (self.external_id and
(self.external_id != self.get_external_id())):
self.set_external_id()
changed = True
current_fail_mode = self.get_fail_mode() current_fail_mode = self.get_fail_mode()
if self.fail_mode and (self.fail_mode != current_fail_mode): if self.fail_mode and (self.fail_mode != current_fail_mode):
syslog.syslog(syslog.LOG_NOTICE, syslog.syslog(syslog.LOG_NOTICE,
@ -162,31 +174,50 @@ class OVSBridge(object):
self.set_fail_mode() self.set_fail_mode()
changed = True changed = True
current_external_ids = self.get_external_ids()
##
# Change and add existing external ids.
items = self.module.params['external_ids'].items()
for (key, value) in items:
if (value != current_external_ids.get(key, None)):
changed = self.set_external_id(key, value) or changed
##
# Remove current external ids that are not passed in.
for (key, value) in current_external_ids.items():
if key not in self.module.params['external_ids']:
changed = self.set_external_id(key, None) or changed
except Exception, earg: except Exception, earg:
self.module.fail_json(msg=str(earg)) self.module.fail_json(msg=str(earg))
# pylint: enable=W0703 # pylint: enable=W0703
self.module.exit_json(changed=changed) self.module.exit_json(changed=changed)
def get_external_id(self): def get_external_ids(self):
""" Return the current external id. """ """ Return the bridge's external ids as a dict. """
value = ''
if self.exists(): if self.exists():
rtc, out, err = self._vsctl(['br-get-external-id', self.bridge]) rtc, out, err = self._vsctl(['br-get-external-id', self.bridge])
if rtc != 0: if rtc != 0:
self.module.fail_json(msg=err) self.module.fail_json(msg=err)
try: lines = out.split("\n")
(_, value) = out.split('=') lines = [item.split("=") for item in lines if (len(item) > 0)]
except ValueError: return {item[0]: item[1] for item in lines}
pass
return value.strip("\n")
def set_external_id(self): return {}
def set_external_id(self, key, value):
""" Set external id. """ """ Set external id. """
if self.exists(): if self.exists():
(rtc, _, err) = self._vsctl(['br-set-external-id', self.bridge, cmd = ['br-set-external-id', self.bridge, key]
'bridge-id', self.external_id]) if (value):
cmd += [value]
(rtc, _, err) = self._vsctl(cmd)
if rtc != 0: if rtc != 0:
self.module.fail_json(msg=err) self.module.fail_json(msg=err)
return True
return False
def get_fail_mode(self): def get_fail_mode(self):
""" Get failure mode. """ """ Get failure mode. """
@ -216,16 +247,12 @@ def main():
'bridge': {'required': True}, 'bridge': {'required': True},
'state': {'default': 'present', 'choices': ['present', 'absent']}, 'state': {'default': 'present', 'choices': ['present', 'absent']},
'timeout': {'default': 5, 'type': 'int'}, 'timeout': {'default': 5, 'type': 'int'},
'external_id': {'default': ''}, 'external_ids': {'default': None},
'fail_mode': {'default': ''}, 'fail_mode': {'default': None},
'syslogging': {'required': False, 'type': 'bool', 'default': True}
}, },
supports_check_mode=True, supports_check_mode=True,
) )
if (module.params["syslogging"]):
syslog.openlog('ansible-%s' % os.path.basename(__file__))
bridge = OVSBridge(module) bridge = OVSBridge(module)
if module.check_mode: if module.check_mode:
bridge.check() bridge.check()