blob: 14bd837a3aafb102e4f7ecf815f4a580c6110be1 [file] [log] [blame]
Filip Pytlounf5383a42015-10-06 16:28:32 +02001{%- from "linux/map.jinja" import network with context %}
2{%- if network.enabled %}
3
Ales Komarek417e8c52017-08-25 15:10:29 +02004{%- set host_dict = network.host %}
5
6{%- if network.mine_dns_records %}
7
8{%- for node_name, node_grains in salt['mine.get']('*', 'grains.items').iteritems() %}
9{%- if node_grains.get('dns_records', []) is iterable %}
10{%- for record in node_grains.get('dns_records', []) %}
11{%- set record_key = node_name ~ '-' ~ loop.index %}
12{%- do host_dict.update({ record_key: {'address': record.address, 'names': record.names} }) %}
13{%- endfor %}
14{%- endif %}
15{%- endfor %}
16
17{%- endif %}
18
Filip Pytloun86506fe2017-01-26 14:36:16 +010019{%- if network.get('purge_hosts', false) %}
20
21linux_hosts:
22 file.managed:
23 - name: /etc/hosts
24 - source: salt://linux/files/hosts
25 - template: jinja
Ales Komarek417e8c52017-08-25 15:10:29 +020026 - defaults:
27 host_dict: {{ host_dict|yaml }}
Filip Pytloun86506fe2017-01-26 14:36:16 +010028
29{%- else %}
30
Ales Komarek417e8c52017-08-25 15:10:29 +020031{%- for name, host in host_dict.iteritems() %}
Filip Pytlounf5383a42015-10-06 16:28:32 +020032
33{%- if host.names is defined %}
34
Dennis Dmitriev279521e2017-01-27 03:43:57 +020035{%- set clearers = [] %}
36{%- for etc_addr, etc_names in salt.hosts.list_hosts().iteritems() %}
37{%- set names_to_clear = [] %}
38{%- for host_name in host.names %}
39{%- if (host.address != etc_addr) and host_name in etc_names %}
40{%- do names_to_clear.append(host_name) %}
41{%- endif %}
42{%- endfor %}
43{%- if names_to_clear != [] %}
44{%- set clearer = "linux_host_" + name + "_" + etc_addr + "_clear" %}
45{%- do clearers.append(clearer) %}
46
47{{ clearer }}:
48 host.absent:
49 - ip: {{ etc_addr }}
50 - names: {{ names_to_clear }}
51
52{%- endif %}
53{%- endfor %}
54
Filip Pytlounf5383a42015-10-06 16:28:32 +020055linux_host_{{ name }}:
56 host.present:
57 - ip: {{ host.address }}
58 - names: {{ host.names }}
Dennis Dmitriev279521e2017-01-27 03:43:57 +020059 - require: {{ clearers }}
Filip Pytlounf5383a42015-10-06 16:28:32 +020060
Ales Komarekc8548ed2016-06-20 11:29:28 +020061{%- if host.address in grains.ipv4 and host.names|length > 1 %}
Ales Komarekc00acb32016-03-05 11:05:53 +010062
Ales Komarekc8548ed2016-06-20 11:29:28 +020063{%- if host.names.1 in host.names.0 %}
Ales Komarekc00acb32016-03-05 11:05:53 +010064{%- set before = host.names.1 + " " + host.names.0 %}
65{%- set after = host.names.0 + " " + host.names.1 %}
Ales Komarek878ea322016-03-07 09:58:27 +010066{%- elif host.names.0 in host.names.1 %}
Ales Komarekc00acb32016-03-05 11:05:53 +010067{%- set before = host.names.0 + " " + host.names.1 %}
68{%- set after = host.names.1 + " " + host.names.0 %}
69{%- endif %}
70
Ales Komarek878ea322016-03-07 09:58:27 +010071linux_host_{{ name }}_order_fix:
Ales Komarekc00acb32016-03-05 11:05:53 +010072 module.run:
73 - name: file.replace
74 - path: /etc/hosts
75 - pattern: {{ before }}
76 - repl: {{ after }}
77 - watch:
78 - host: linux_host_{{ name }}
Olivier Bourdon577fbf52016-11-09 14:53:02 +010079 - onlyif:
80 - grep -q "{{ before }}" /etc/hosts
Ales Komarekc00acb32016-03-05 11:05:53 +010081
82{%- endif %}
83
Filip Pytlounf5383a42015-10-06 16:28:32 +020084{%- endif %}
85
Filip Pytlounf5383a42015-10-06 16:28:32 +020086{%- endfor %}
87
Olivier Bourdon577fbf52016-11-09 14:53:02 +010088{%- endif %}
Filip Pytloun86506fe2017-01-26 14:36:16 +010089
90{%- endif %}