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
1 changed files with 16 additions and 11 deletions

View File

@ -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()