From c7b7169c7926347a7d0afb27ca3e1e3d6088dbb6 Mon Sep 17 00:00:00 2001 From: Kevin Carter Date: Tue, 19 Jul 2016 23:43:49 -0500 Subject: [PATCH] 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 --- library/dist_sort | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/library/dist_sort b/library/dist_sort index d4d1039..d7a94c0 100644 --- a/library/dist_sort +++ b/library/dist_sort @@ -15,9 +15,11 @@ # See the License for the specific language governing permissions and # limitations under the License. + # import module snippets from ansible.module_utils.basic import * + DOCUMENTATION = """ --- module: dist_sort @@ -60,6 +62,7 @@ author: - Sam Yaple """ + EXAMPLES = """ - dist_sort: value_to_lookup: "Hostname-in-ansible-group_name" @@ -106,16 +109,19 @@ class DistSort(object): :returns: ``str`` """ - index = self.params['ref_list'].index(self.params['value_to_lookup']) - index += self.params['sort_modifier'] - src_list = self.params['src_list'].split( - self.params['delimiter'] - ) + try: + index = self.params['ref_list'].index( + self.params['value_to_lookup'] + ) + 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)) - else: - return self.params['delimiter'].join(src_list) + + return self.params['delimiter'].join(src_list) def main(): @@ -151,18 +157,17 @@ def main(): # This is done so that the failure can be parsed and does not cause # ansible to fail if a non-int is passed. module.params['sort_modifier'] = int(module.params['sort_modifier']) - _ds = DistSort(module=module) if _ds.return_data == module.params['src_list']: _changed = False else: _changed = True - - module.exit_json(changed=_changed, **{'sorted_list': _ds.return_data}) except Exception as exp: resp = {'stderr': str(exp)} resp.update(module.params) module.fail_json(msg='Failed Process', **resp) + else: + module.exit_json(changed=_changed, **{'sorted_list': _ds.return_data}) if __name__ == '__main__': main()