Bladeren bron

Fix for sorting hostnames and aliases properly

Change-Id: I7e9267036d473b68c5344ed50304030e6985f27c
pull/122/head
Olivier Bourdon 7 jaren geleden
bovenliggende
commit
99c9bcbd63
2 gewijzigde bestanden met toevoegingen van 33 en 1 verwijderingen
  1. +27
    -0
      _modules/linux_hosts.py
  2. +6
    -1
      linux/files/hosts

+ 27
- 0
_modules/linux_hosts.py Bestand weergeven

@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
'''
Module for defining new filter for sorting
host names/alias by FQDN first and alphabetically
'''

from jinja2 import Undefined

def fqdn_sort_fn(n1, n2):
l1 = n1.split('.')
l2 = n2.split('.')
if len(l1) > len(l2):
return -1
if len(l1) < len(l2):
return 1
for i1, i2 in zip(l1, l2):
if i1 < i2:
return -1
if i1 > i2:
return 1
return 0

def fqdn_sort_filter(iterable):
if iterable is None or isinstance(iterable, Undefined):
return iterable
# Do effective custom sorting of iterable here
return sorted(iterable, cmp=fqdn_sort_fn)

+ 6
- 1
linux/files/hosts Bestand weergeven

@@ -40,5 +40,10 @@
{%- do hosts.update({host.address: host.names}) -%}
{%- endfor %}
{% for address, entries in hosts|dictsort %}
{{ address }} {{ entries|join(' ') }}
{%- if 'linux_hosts.fqdn_sort_filter' in salt.keys() %}
{%- set sorted_entries = salt['linux_hosts.fqdn_sort_filter'](entries) -%}
{%- else %}
{%- set sorted_entries = entries -%}
{%- endif %}
{{ address }} {{ sorted_entries|join(' ') }}
{%- endfor %}

Laden…
Annuleren
Opslaan