Update dist-sort module for better effectiveness

The dist-sort module has been updated to ensure its execution
is effective, fast, and nonintrusive. There are cases where
the rounds made by the module take an excessive amount of time
to complete. This change ensures that the module is limiting its
rounds to a known index and the iterable is processed using a
generator.

Change-Id: I03d1f70cb6cd17f38ef37b4d8371a8982beca29e
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
This commit is contained in:
Kevin Carter 2016-07-19 23:43:49 -05:00
parent 991380305f
commit c7b7169c79
No known key found for this signature in database
GPG Key ID: 69FEFFC5E2D9273F

View File

@ -15,9 +15,11 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
DOCUMENTATION = """ DOCUMENTATION = """
--- ---
module: dist_sort module: dist_sort
@ -60,6 +62,7 @@ author:
- Sam Yaple - Sam Yaple
""" """
EXAMPLES = """ EXAMPLES = """
- dist_sort: - dist_sort:
value_to_lookup: "Hostname-in-ansible-group_name" value_to_lookup: "Hostname-in-ansible-group_name"
@ -106,16 +109,19 @@ class DistSort(object):
:returns: ``str`` :returns: ``str``
""" """
index = self.params['ref_list'].index(self.params['value_to_lookup']) try:
index += self.params['sort_modifier'] index = self.params['ref_list'].index(
src_list = self.params['src_list'].split( self.params['value_to_lookup']
self.params['delimiter'] )
) except ValueError:
index = 0
for _ in range(index % len(src_list)): src_list = self.params['src_list'].split(self.params['delimiter'])
index += self.params['sort_modifier']
for _ in xrange(index % len(src_list)):
src_list.append(src_list.pop(0)) src_list.append(src_list.pop(0))
else:
return self.params['delimiter'].join(src_list) return self.params['delimiter'].join(src_list)
def main(): def main():
@ -151,18 +157,17 @@ def main():
# This is done so that the failure can be parsed and does not cause # This is done so that the failure can be parsed and does not cause
# ansible to fail if a non-int is passed. # ansible to fail if a non-int is passed.
module.params['sort_modifier'] = int(module.params['sort_modifier']) module.params['sort_modifier'] = int(module.params['sort_modifier'])
_ds = DistSort(module=module) _ds = DistSort(module=module)
if _ds.return_data == module.params['src_list']: if _ds.return_data == module.params['src_list']:
_changed = False _changed = False
else: else:
_changed = True _changed = True
module.exit_json(changed=_changed, **{'sorted_list': _ds.return_data})
except Exception as exp: except Exception as exp:
resp = {'stderr': str(exp)} resp = {'stderr': str(exp)}
resp.update(module.params) resp.update(module.params)
module.fail_json(msg='Failed Process', **resp) module.fail_json(msg='Failed Process', **resp)
else:
module.exit_json(changed=_changed, **{'sorted_list': _ds.return_data})
if __name__ == '__main__': if __name__ == '__main__':
main() main()