Saltstack Official Linux Formula
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

98 行
2.4KB

  1. {%- from "linux/map.jinja" import network with context %}
  2. {%- if network.enabled %}
  3. {%- set host_dict = network.host %}
  4. {%- if network.mine_dns_records %}
  5. {%- for node_name, node_grains in salt['mine.get']('*', 'grains.items').items() %}
  6. {%- if node_grains.get('dns_records', []) is iterable %}
  7. {%- for record in node_grains.get('dns_records', []) %}
  8. {%- set record_key = node_name ~ '-' ~ loop.index %}
  9. {%- do host_dict.update({ record_key: {'address': record.address, 'names': record.names} }) %}
  10. {%- endfor %}
  11. {%- endif %}
  12. {%- endfor %}
  13. {%- endif %}
  14. {%- if network.get('purge_hosts', false) %}
  15. linux_hosts:
  16. file.managed:
  17. - name: /etc/hosts
  18. - source: salt://linux/files/hosts
  19. - template: jinja
  20. - defaults:
  21. host_dict: {{ host_dict|yaml }}
  22. {%- else %}
  23. {%- for name, host in host_dict.items() %}
  24. {%- if host.names is defined %}
  25. {%- set clearers = [] %}
  26. {%- for etc_addr, etc_names in salt.hosts.list_hosts().items() %}
  27. {%- set names_to_clear = [] %}
  28. {%- for host_name in host.names %}
  29. {%- if (host.address != etc_addr) and host_name in etc_names %}
  30. {%- do names_to_clear.append(host_name) %}
  31. {%- endif %}
  32. {%- endfor %}
  33. {%- if names_to_clear != [] %}
  34. {%- set clearer = "linux_host_" + name + "_" + etc_addr + "_clear" %}
  35. {%- do clearers.append(clearer) %}
  36. {{ clearer }}:
  37. host.absent:
  38. - ip: {{ etc_addr }}
  39. - names: {{ names_to_clear }}
  40. {%- endif %}
  41. {%- endfor %}
  42. linux_host_{{ name }}:
  43. host.present:
  44. - ip: {{ host.address }}
  45. - names: {{ host.names }}
  46. - require: {{ clearers }}
  47. {%- if host.address in grains.ipv4 and host.names|length > 1 %}
  48. {%- if host.names.1 in host.names.0 %}
  49. {%- set before = host.names.1 + " " + host.names.0 %}
  50. {%- set after = host.names.0 + " " + host.names.1 %}
  51. {%- elif host.names.0 in host.names.1 %}
  52. {%- set before = host.names.0 + " " + host.names.1 %}
  53. {%- set after = host.names.1 + " " + host.names.0 %}
  54. {%- endif %}
  55. linux_host_{{ name }}_order_fix:
  56. module.run:
  57. {%- if 'module.run' in salt['config.get']('use_superseded', default=[]) %}
  58. - file.replace:
  59. - path: /etc/hosts
  60. - pattern: {{ before }}
  61. - repl: {{ after }}
  62. {%- else %}
  63. - name: file.replace
  64. - path: /etc/hosts
  65. - pattern: {{ before }}
  66. - repl: {{ after }}
  67. {%- endif %}
  68. - watch:
  69. - host: linux_host_{{ name }}
  70. - onlyif:
  71. - grep -q "{{ before }}" /etc/hosts
  72. {%- endif %}
  73. {%- endif %}
  74. {%- endfor %}
  75. {%- endif %}
  76. {%- endif %}