Saltstack Official Linux Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
662B

  1. # -*- coding: utf-8 -*-
  2. '''
  3. Module for defining new filter for sorting
  4. host names/alias by FQDN first and alphabetically
  5. '''
  6. from jinja2 import Undefined
  7. def fqdn_sort_fn(n1, n2):
  8. l1 = n1.split('.')
  9. l2 = n2.split('.')
  10. if len(l1) > len(l2):
  11. return -1
  12. if len(l1) < len(l2):
  13. return 1
  14. for i1, i2 in zip(l1, l2):
  15. if i1 < i2:
  16. return -1
  17. if i1 > i2:
  18. return 1
  19. return 0
  20. def fqdn_sort_filter(iterable):
  21. if iterable is None or isinstance(iterable, Undefined):
  22. return iterable
  23. # Do effective custom sorting of iterable here
  24. return sorted(set(iterable), cmp=fqdn_sort_fn)