From 24375f5d226f41d52d1ba207fdb98437416bad7c Mon Sep 17 00:00:00 2001 From: Mark Hamilton Date: Thu, 16 Jul 2015 10:05:59 -0700 Subject: [PATCH] Changed syntax to support python2.4. Allow external_ids to be None. --- .../extras/network/openvswitch_bridge.py | 52 +++++++++++-------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/lib/ansible/modules/extras/network/openvswitch_bridge.py b/lib/ansible/modules/extras/network/openvswitch_bridge.py index 58ba8d740e..2396b1f2b4 100644 --- a/lib/ansible/modules/extras/network/openvswitch_bridge.py +++ b/lib/ansible/modules/extras/network/openvswitch_bridge.py @@ -128,17 +128,18 @@ class OVSBridge(object): ## # 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 + 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 self.module.params['external_ids']: - 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 elif self.state == 'absent' and self.exists(): changed = True @@ -178,16 +179,19 @@ class OVSBridge(object): ## # 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 + 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 self.module.params['external_ids']: - changed = self.set_external_id(key, None) or changed + ## + # 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 except Exception, earg: self.module.fail_json(msg=str(earg)) @@ -196,21 +200,23 @@ class OVSBridge(object): def get_external_ids(self): """ Return the bridge's external ids as a dict. """ + results = {} if self.exists(): rtc, out, err = self._vsctl(['br-get-external-id', self.bridge]) if rtc != 0: self.module.fail_json(msg=err) lines = out.split("\n") - lines = [item.split("=") for item in lines if (len(item) > 0)] - return {item[0]: item[1] for item in lines} + lines = [item.split("=") for item in lines if len(item) > 0] + for item in lines: + results[item[0]] = item[1] - return {} + return results def set_external_id(self, key, value): """ Set external id. """ if self.exists(): cmd = ['br-set-external-id', self.bridge, key] - if (value): + if value: cmd += [value] (rtc, _, err) = self._vsctl(cmd)