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.

53 lines
1.1KB

  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. linux_host_{{ name }}:
  13. host.present:
  14. - ip: {{ host.address }}
  15. - names: {{ host.names }}
  16. {%- if host.address in grains.ipv4 and host.names|length > 1 %}
  17. {%- if host.names.1 in host.names.0 %}
  18. {%- set before = host.names.1 + " " + host.names.0 %}
  19. {%- set after = host.names.0 + " " + host.names.1 %}
  20. {%- elif host.names.0 in host.names.1 %}
  21. {%- set before = host.names.0 + " " + host.names.1 %}
  22. {%- set after = host.names.1 + " " + host.names.0 %}
  23. {%- endif %}
  24. linux_host_{{ name }}_order_fix:
  25. module.run:
  26. - name: file.replace
  27. - path: /etc/hosts
  28. - pattern: {{ before }}
  29. - repl: {{ after }}
  30. - watch:
  31. - host: linux_host_{{ name }}
  32. - onlyif:
  33. - grep -q "{{ before }}" /etc/hosts
  34. {%- endif %}
  35. {%- endif %}
  36. {%- endfor %}
  37. {%- endif %}
  38. {%- endif %}