2014-01-15 15:52:17 +01:00
|
|
|
#!/usr/bin/python
|
2013-09-19 01:28:41 +02:00
|
|
|
#coding: utf-8 -*-
|
|
|
|
|
2014-06-20 02:02:02 +02:00
|
|
|
# (c) 2013, David Stygstra <david.stygstra@gmail.com>
|
|
|
|
#
|
2015-07-14 14:17:38 +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 05:45:19 +02:00
|
|
|
|
|
|
|
# pylint: disable=C0111
|
2013-09-19 01:28:41 +02:00
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: openvswitch_bridge
|
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 bridges
|
|
|
|
requirements: [ ovs-vsctl ]
|
|
|
|
description:
|
|
|
|
- Manage Open vSwitch bridges
|
|
|
|
options:
|
|
|
|
bridge:
|
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
- Name of bridge to manage
|
|
|
|
state:
|
|
|
|
required: false
|
|
|
|
default: "present"
|
|
|
|
choices: [ present, absent ]
|
|
|
|
description:
|
|
|
|
- Whether the bridge should exist
|
|
|
|
timeout:
|
|
|
|
required: false
|
|
|
|
default: 5
|
|
|
|
description:
|
|
|
|
- How long to wait for ovs-vswitchd to respond
|
2015-07-14 14:17:38 +02:00
|
|
|
external_ids:
|
|
|
|
version_added: 2.0
|
2015-07-09 05:45:19 +02:00
|
|
|
required: false
|
2015-07-14 14:17:38 +02:00
|
|
|
default: None
|
2015-07-09 05:45:19 +02:00
|
|
|
description:
|
2015-07-14 14:17:38 +02:00
|
|
|
- A dictionary of external-ids. Omitting this parameter is a No-op.
|
|
|
|
To clear all external-ids pass an empty value.
|
2015-07-09 05:45:19 +02:00
|
|
|
fail_mode:
|
2015-07-14 14:17:38 +02:00
|
|
|
version_added: 2.0
|
|
|
|
default: None
|
2015-07-09 05:45:19 +02:00
|
|
|
required: false
|
|
|
|
choices : [secure, standalone]
|
|
|
|
description:
|
2015-07-14 14:17:38 +02:00
|
|
|
- Set bridge fail-mode. The default value (None) is a No-op.
|
2013-09-19 01:28:41 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Create a bridge named br-int
|
|
|
|
- openvswitch_bridge: bridge=br-int state=present
|
2015-07-09 05:45:19 +02:00
|
|
|
|
|
|
|
# Create an integration bridge
|
2015-07-14 14:17:38 +02:00
|
|
|
- openvswitch_bridge: bridge=br-int state=present fail_mode=secure
|
|
|
|
args:
|
|
|
|
external_ids:
|
|
|
|
bridge-id: "br-int"
|
2013-09-19 01:28:41 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
class OVSBridge(object):
|
2015-07-09 05:45:19 +02:00
|
|
|
""" Interface to ovs-vsctl. """
|
2013-09-19 01:28:41 +02:00
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
self.bridge = module.params['bridge']
|
|
|
|
self.state = module.params['state']
|
|
|
|
self.timeout = module.params['timeout']
|
2015-07-09 05:45:19 +02:00
|
|
|
self.fail_mode = module.params['fail_mode']
|
2013-09-19 01:28:41 +02:00
|
|
|
|
|
|
|
def _vsctl(self, command):
|
|
|
|
'''Run ovs-vsctl command'''
|
2015-07-09 05:45:19 +02:00
|
|
|
return self.module.run_command(['ovs-vsctl', '-t',
|
|
|
|
str(self.timeout)] + command)
|
2013-09-19 01:28:41 +02:00
|
|
|
|
|
|
|
def exists(self):
|
|
|
|
'''Check if the bridge already exists'''
|
2015-07-09 05:45:19 +02:00
|
|
|
rtc, _, err = self._vsctl(['br-exists', self.bridge])
|
|
|
|
if rtc == 0: # See ovs-vsctl(8) for status codes
|
2013-09-19 01:28:41 +02:00
|
|
|
return True
|
2015-07-09 05:45:19 +02:00
|
|
|
if rtc == 2:
|
2013-09-19 01:28:41 +02:00
|
|
|
return False
|
2015-07-09 05:45:19 +02:00
|
|
|
self.module.fail_json(msg=err)
|
2013-09-19 01:28:41 +02:00
|
|
|
|
|
|
|
def add(self):
|
|
|
|
'''Create the bridge'''
|
2015-07-09 05:45:19 +02:00
|
|
|
rtc, _, err = self._vsctl(['add-br', self.bridge])
|
|
|
|
if rtc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
|
|
|
if self.fail_mode:
|
|
|
|
self.set_fail_mode()
|
2013-09-19 01:28:41 +02:00
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
'''Delete the bridge'''
|
2015-07-09 05:45:19 +02:00
|
|
|
rtc, _, err = self._vsctl(['del-br', self.bridge])
|
|
|
|
if rtc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
2013-09-19 01:28:41 +02:00
|
|
|
|
|
|
|
def check(self):
|
|
|
|
'''Run check mode'''
|
2015-07-09 05:45:19 +02:00
|
|
|
changed = False
|
|
|
|
|
|
|
|
# pylint: disable=W0703
|
2013-09-19 01:28:41 +02:00
|
|
|
try:
|
2015-07-09 05:45:19 +02:00
|
|
|
if self.state == 'present' and self.exists():
|
|
|
|
if (self.fail_mode and
|
|
|
|
(self.fail_mode != self.get_fail_mode())):
|
|
|
|
changed = True
|
2015-07-14 14:17:38 +02:00
|
|
|
|
|
|
|
##
|
|
|
|
# Check if external ids would change.
|
|
|
|
current_external_ids = self.get_external_ids()
|
2015-07-16 19:05:59 +02:00
|
|
|
exp_external_ids = self.module.params['external_ids']
|
|
|
|
if exp_external_ids is not None:
|
|
|
|
for (key, value) in exp_external_ids:
|
|
|
|
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 exp_external_ids:
|
|
|
|
changed = True
|
2015-07-14 14:17:38 +02:00
|
|
|
|
2015-07-09 05:45:19 +02:00
|
|
|
elif self.state == 'absent' and self.exists():
|
2013-09-19 01:28:41 +02:00
|
|
|
changed = True
|
|
|
|
elif self.state == 'present' and not self.exists():
|
|
|
|
changed = True
|
2015-07-09 05:45:19 +02:00
|
|
|
except Exception, earg:
|
|
|
|
self.module.fail_json(msg=str(earg))
|
|
|
|
|
|
|
|
# pylint: enable=W0703
|
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
|
2015-07-09 05:45:19 +02:00
|
|
|
# pylint: disable=W0703
|
|
|
|
|
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-14 14:17:38 +02:00
|
|
|
|
2013-09-19 01:28:41 +02:00
|
|
|
if not self.exists():
|
|
|
|
self.add()
|
2013-10-13 18:35:53 +02:00
|
|
|
changed = True
|
2013-09-19 01:28:41 +02:00
|
|
|
|
2015-07-09 05:45:19 +02:00
|
|
|
current_fail_mode = self.get_fail_mode()
|
|
|
|
if self.fail_mode and (self.fail_mode != current_fail_mode):
|
|
|
|
syslog.syslog(syslog.LOG_NOTICE,
|
|
|
|
"changing fail mode %s to %s" %
|
|
|
|
(current_fail_mode, self.fail_mode))
|
|
|
|
self.set_fail_mode()
|
|
|
|
changed = True
|
2013-09-19 01:28:41 +02:00
|
|
|
|
2015-07-14 14:17:38 +02:00
|
|
|
current_external_ids = self.get_external_ids()
|
|
|
|
|
|
|
|
##
|
|
|
|
# Change and add existing external ids.
|
2015-07-16 19:05:59 +02:00
|
|
|
exp_external_ids = self.module.params['external_ids']
|
|
|
|
if exp_external_ids is not None:
|
|
|
|
for (key, value) in exp_external_ids.items():
|
|
|
|
if ((value != current_external_ids.get(key, None)) and
|
|
|
|
self.set_external_id(key, value)):
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
##
|
|
|
|
# Remove current external ids that are not passed in.
|
|
|
|
for (key, value) in current_external_ids.items():
|
|
|
|
if ((key not in exp_external_ids) and
|
|
|
|
self.set_external_id(key, None)):
|
|
|
|
changed = True
|
2015-07-14 14:17:38 +02:00
|
|
|
|
2015-07-09 05:45:19 +02:00
|
|
|
except Exception, earg:
|
|
|
|
self.module.fail_json(msg=str(earg))
|
|
|
|
# pylint: enable=W0703
|
|
|
|
self.module.exit_json(changed=changed)
|
|
|
|
|
2015-07-14 14:17:38 +02:00
|
|
|
def get_external_ids(self):
|
|
|
|
""" Return the bridge's external ids as a dict. """
|
2015-07-16 19:05:59 +02:00
|
|
|
results = {}
|
2015-07-09 05:45:19 +02:00
|
|
|
if self.exists():
|
|
|
|
rtc, out, err = self._vsctl(['br-get-external-id', self.bridge])
|
|
|
|
if rtc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
2015-07-14 14:17:38 +02:00
|
|
|
lines = out.split("\n")
|
2015-07-16 19:05:59 +02:00
|
|
|
lines = [item.split("=") for item in lines if len(item) > 0]
|
|
|
|
for item in lines:
|
|
|
|
results[item[0]] = item[1]
|
2015-07-09 05:45:19 +02:00
|
|
|
|
2015-07-16 19:05:59 +02:00
|
|
|
return results
|
2015-07-14 14:17:38 +02:00
|
|
|
|
|
|
|
def set_external_id(self, key, value):
|
2015-07-09 05:45:19 +02:00
|
|
|
""" Set external id. """
|
|
|
|
if self.exists():
|
2015-07-14 14:17:38 +02:00
|
|
|
cmd = ['br-set-external-id', self.bridge, key]
|
2015-07-16 19:05:59 +02:00
|
|
|
if value:
|
2015-07-14 14:17:38 +02:00
|
|
|
cmd += [value]
|
|
|
|
|
|
|
|
(rtc, _, err) = self._vsctl(cmd)
|
2015-07-09 05:45:19 +02:00
|
|
|
if rtc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
2015-07-14 14:17:38 +02:00
|
|
|
return True
|
|
|
|
return False
|
2015-07-09 05:45:19 +02:00
|
|
|
|
|
|
|
def get_fail_mode(self):
|
|
|
|
""" Get failure mode. """
|
|
|
|
value = ''
|
|
|
|
if self.exists():
|
|
|
|
rtc, out, err = self._vsctl(['get-fail-mode', self.bridge])
|
|
|
|
if rtc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
|
|
|
value = out.strip("\n")
|
|
|
|
return value
|
|
|
|
|
|
|
|
def set_fail_mode(self):
|
|
|
|
""" Set failure mode. """
|
|
|
|
|
|
|
|
if self.exists():
|
|
|
|
(rtc, _, err) = self._vsctl(['set-fail-mode', self.bridge,
|
|
|
|
self.fail_mode])
|
|
|
|
if rtc != 0:
|
|
|
|
self.module.fail_json(msg=err)
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=E0602
|
2013-09-19 01:28:41 +02:00
|
|
|
def main():
|
2015-07-09 05:45:19 +02:00
|
|
|
""" Entry point. """
|
2013-09-19 01:28:41 +02:00
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec={
|
|
|
|
'bridge': {'required': True},
|
|
|
|
'state': {'default': 'present', 'choices': ['present', 'absent']},
|
2015-07-09 05:45:19 +02:00
|
|
|
'timeout': {'default': 5, 'type': 'int'},
|
2015-07-14 14:17:38 +02:00
|
|
|
'external_ids': {'default': None},
|
|
|
|
'fail_mode': {'default': None},
|
2013-09-19 01:28:41 +02:00
|
|
|
},
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
2015-07-09 05:45:19 +02:00
|
|
|
bridge = OVSBridge(module)
|
2013-09-19 01:28:41 +02:00
|
|
|
if module.check_mode:
|
2015-07-09 05:45:19 +02:00
|
|
|
bridge.check()
|
2013-09-19 01:28:41 +02:00
|
|
|
else:
|
2015-07-09 05:45:19 +02:00
|
|
|
bridge.run()
|
2013-09-19 01:28:41 +02:00
|
|
|
|
2015-07-09 05:45:19 +02:00
|
|
|
# pylint: disable=W0614
|
|
|
|
# pylint: disable=W0401
|
|
|
|
# pylint: disable=W0622
|
2013-09-19 01:28:41 +02:00
|
|
|
|
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()
|