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.

74 lines
1.7KB

  1. {%- from "linux/map.jinja" import network with context %}
  2. {%- if network.enabled %}
  3. {%- if network.get('purge_hosts', false) %}
  4. linux_hosts:
  5. file.managed:
  6. - name: /etc/hosts
  7. - source: salt://linux/files/hosts
  8. - template: jinja
  9. {%- else %}
  10. {%- for name, host in network.host.iteritems() %}
  11. {%- if host.names is defined %}
  12. {%- set clearers = [] %}
  13. {%- for etc_addr, etc_names in salt.hosts.list_hosts().iteritems() %}
  14. {%- set names_to_clear = [] %}
  15. {%- for host_name in host.names %}
  16. {%- if (host.address != etc_addr) and host_name in etc_names %}
  17. {%- do names_to_clear.append(host_name) %}
  18. {%- endif %}
  19. {%- endfor %}
  20. {%- if names_to_clear != [] %}
  21. {%- set clearer = "linux_host_" + name + "_" + etc_addr + "_clear" %}
  22. {%- do clearers.append(clearer) %}
  23. {{ clearer }}:
  24. host.absent:
  25. - ip: {{ etc_addr }}
  26. - names: {{ names_to_clear }}
  27. {%- endif %}
  28. {%- endfor %}
  29. linux_host_{{ name }}:
  30. host.present:
  31. - ip: {{ host.address }}
  32. - names: {{ host.names }}
  33. - require: {{ clearers }}
  34. {%- if host.address in grains.ipv4 and host.names|length > 1 %}
  35. {%- if host.names.1 in host.names.0 %}
  36. {%- set before = host.names.1 + " " + host.names.0 %}
  37. {%- set after = host.names.0 + " " + host.names.1 %}
  38. {%- elif host.names.0 in host.names.1 %}
  39. {%- set before = host.names.0 + " " + host.names.1 %}
  40. {%- set after = host.names.1 + " " + host.names.0 %}
  41. {%- endif %}
  42. linux_host_{{ name }}_order_fix:
  43. module.run:
  44. - name: file.replace
  45. - path: /etc/hosts
  46. - pattern: {{ before }}
  47. - repl: {{ after }}
  48. - watch:
  49. - host: linux_host_{{ name }}
  50. - onlyif:
  51. - grep -q "{{ before }}" /etc/hosts
  52. {%- endif %}
  53. {%- endif %}
  54. {%- endfor %}
  55. {%- endif %}
  56. {%- endif %}