Skip to content

Commit 9fc487f

Browse files
authored
Merge pull request #29 from aaannz/host_under_subnet
Allow hosts to be specified for subnet as well
2 parents 7843cee + e8bb413 commit 9fc487f

File tree

4 files changed

+90
-72
lines changed

4 files changed

+90
-72
lines changed

dhcpd/files/dhcpd.conf

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ failover peer "{{ failover_peer }}" {
193193
}
194194
{%- endfor %}
195195

196-
{%- set intendation='' %}
196+
{%- set indentation='' %}
197197
{%- for key, config in salt['pillar.get']('dhcpd:keys', {}).items() %}
198198
key {{ key }} {
199199
{%- if 'algorithm' in config %}
@@ -221,47 +221,7 @@ zone {{ zone }} {
221221
{%- endfor %}
222222

223223
{% for host, config in salt['pillar.get']('dhcpd:hosts', {}).items() %}
224-
{%- if 'comment' in config %}
225-
{%- for line in config.comment.splitlines() %}
226-
# {{ line }}
227-
{%- endfor %}
228-
{%- endif %}
229-
host {{ host }} {
230-
{%- if 'allow' in config %}
231-
{%- if config.allow is iterable and config.allow is not string %}
232-
{%- for item in config.allow %}allow {{ item }};{%- endfor %}
233-
{%- else %}allow {{ config.allow }};{%- endif %}
234-
{%- endif %}
235-
{%- if 'deny' in config %}
236-
{%- if config.deny is iterable and config.deny is not string %}
237-
{%- for item in config.deny %}deny {{ item }};{%- endfor %}
238-
{%- else %}deny {{ config.deny }};{%- endif %}
239-
{%- endif %}
240-
{%- if 'hardware' in config %}
241-
hardware {{ config.hardware }};
242-
{%- endif %}
243-
{%- if 'fixed_address' in config %}
244-
fixed-address {{ config.fixed_address }};
245-
{%- endif %}
246-
{%- if 'filename' in config %}
247-
filename "{{ config.filename }}";
248-
{%- endif %}
249-
{%- if 'next_server' in config %}
250-
next-server {{ config.next_server }};
251-
{%- endif %}
252-
{%- if 'server_name' in config %}
253-
server-name "{{ config.server_name }}";
254-
{%- endif %}
255-
{%- if 'host_name' in config %}
256-
option host-name "{{ config.host_name }}";
257-
{%- endif %}
258-
{%- for option in customized.keys() %}
259-
{%- if option in config %}
260-
{%- if customized[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %}
261-
option {{ option|replace('_', '-') }} {{ quote }}{{ config.get(option) }}{{ quote }};
262-
{%- endif %}
263-
{%- endfor %}
264-
}
224+
{%- include 'dhcpd/files/host.jinja' with context %}
265225
{% endfor %}
266226
{%- for shared_network, config in salt['pillar.get']('dhcpd:shared_networks', {}).items() %}
267227
{%- if 'comment' in config %}
@@ -270,7 +230,7 @@ host {{ host }} {
270230
{%- endfor %}
271231
{%- endif %}
272232
shared-network {{ shared_network }} {
273-
{%- set intendation=' ' %}
233+
{%- set indentation=' ' %}
274234
{%- for subnet, config in salt['pillar.get'](
275235
'dhcpd:shared_networks:{0}:subnets'.format(shared_network), {}).items() %}
276236
{%- include 'dhcpd/files/subnet.jinja' with context %}

dhcpd/files/host.jinja

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{%- if 'comment' in config %}
2+
{%- for line in config.comment.splitlines() %}
3+
{{ indentation }}# {{ line }}
4+
{%- endfor %}
5+
{%- endif %}
6+
{{ indentation }}host {{ host }} {
7+
{%- if 'allow' in config %}
8+
{%- if config.allow is iterable and config.allow is not string %}
9+
{%- for item in config.allow %}
10+
{{ indentation }} allow {{ item }};
11+
{%- endfor %}
12+
{%- else %}
13+
{{ indentation }} allow {{ config.allow }};
14+
{%- endif %}
15+
{%- endif %}
16+
{%- if 'deny' in config %}
17+
{%- if config.deny is iterable and config.deny is not string %}
18+
{%- for item in config.deny %}
19+
{{ indentation }} deny {{ item }};
20+
{%- endfor %}
21+
{%- else %}
22+
{{ indentation }} deny {{ config.deny }};
23+
{%- endif %}
24+
{%- endif %}
25+
{%- if 'hardware' in config %}
26+
{{ indentation }} hardware {{ config.hardware }};
27+
{%- endif %}
28+
{%- if 'fixed_address' in config %}
29+
{{ indentation }} fixed-address {{ config.fixed_address }};
30+
{%- endif %}
31+
{%- if 'filename' in config %}
32+
{{ indentation }} filename "{{ config.filename }}";
33+
{%- endif %}
34+
{%- if 'next_server' in config %}
35+
{{ indentation }} next-server {{ config.next_server }};
36+
{%- endif %}
37+
{%- if 'server_name' in config %}
38+
{{ indentation }} server-name "{{ config.server_name }}";
39+
{%- endif %}
40+
{%- if 'host_name' in config %}
41+
{{ indentation }} option host-name "{{ config.host_name }}";
42+
{%- endif %}
43+
{%- for option in customized.keys() %}
44+
{%- if option in config %}
45+
{%- if customized[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %}
46+
{{ indentation }} option {{ option|replace('_', '-') }} {{ quote }}{{ config.get(option) }}{{ quote }};
47+
{%- endif %}
48+
{%- endfor %}
49+
}

dhcpd/files/subnet.jinja

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,89 +3,92 @@
33
# {{ line }}
44
{%- endfor %}
55
{%- endif %}
6-
{{ intendation }}subnet {{ subnet }} netmask {{ config.netmask }} {
6+
{{ indentation }}subnet {{ subnet }} netmask {{ config.netmask }} {
77
{%- if 'use_host_decl_names' in config %}
8-
{{ intendation }} use-host-decl-names {{ config.use_host_decl_names }};
8+
{{ indentation }} use-host-decl-names {{ config.use_host_decl_names }};
99
{%- endif %}
1010
{%- if 'range' in config %}
1111
{%- if 'dynamic_bootp' in config and config.dynamic_bootp %}
12-
{{ intendation }} range dynamic-bootp {{ config.range[0] }} {{ config.range[1] }};
12+
{{ indentation }} range dynamic-bootp {{ config.range[0] }} {{ config.range[1] }};
1313
{%- else %}
14-
{{ intendation }} range {{ config.range[0] }} {{ config.range[1] }};
14+
{{ indentation }} range {{ config.range[0] }} {{ config.range[1] }};
1515
{%- endif %}
1616
{%- endif %}
1717
{%- if 'broadcast_address' in config %}
18-
{{ intendation }} option broadcast-address {{ config['broadcast_address'] }};
18+
{{ indentation }} option broadcast-address {{ config['broadcast_address'] }};
1919
{%- endif %}
2020
{%- if 'domain_name_servers' in config %}
21-
{{ intendation }} option domain-name-servers {{ config['domain_name_servers']|join(',') }};
21+
{{ indentation }} option domain-name-servers {{ config['domain_name_servers']|join(',') }};
2222
{%- endif %}
2323
{%- if 'netbios_name_servers' in config %}
24-
{{ intendation }} option netbios-name-servers {{ config['netbios_name_servers']|join(',') }};
24+
{{ indentation }} option netbios-name-servers {{ config['netbios_name_servers']|join(',') }};
2525
{%- endif %}
2626
{%- if 'ntp_servers' in config %}
27-
{{ intendation }} option ntp-servers {{ config['ntp_servers']|join(',') }};
27+
{{ indentation }} option ntp-servers {{ config['ntp_servers']|join(',') }};
2828
{%- endif %}
2929
{%- if 'lpr_servers' in config %}
30-
{{ intendation }} option lpr-servers {{ config['lpr_servers']|join(',') }};
30+
{{ indentation }} option lpr-servers {{ config['lpr_servers']|join(',') }};
3131
{%- endif %}
3232
{%- if 'irc_server' in config %}
33-
{{ intendation }} option irc-server {{ config['irc_server']|join(',') }};
33+
{{ indentation }} option irc-server {{ config['irc_server']|join(',') }};
3434
{%- endif %}
3535
{%- if 'tftp_server_name' in config %}
36-
{{ intendation }} option tftp-server-name "{{ config['tftp_server_name'] }}";
36+
{{ indentation }} option tftp-server-name "{{ config['tftp_server_name'] }}";
3737
{%- endif %}
3838
{%- if 'smtp_server' in config %}
39-
{{ intendation }} option smtp-server {{ config['smtp_server'] }};
39+
{{ indentation }} option smtp-server {{ config['smtp_server'] }};
4040
{%- endif %}
4141
{%- if 'domain_name' in config %}
42-
{{ intendation }} option domain-name "{{ config['domain_name'] }}";
42+
{{ indentation }} option domain-name "{{ config['domain_name'] }}";
4343
{%- endif %}
4444
{%- if 'domain_search' in config %}
45-
{{ intendation }} option domain-search "{{ config['domain_search']|join('","') }}";
45+
{{ indentation }} option domain-search "{{ config['domain_search']|join('","') }}";
4646
{%- endif %}
4747
{%- if 'filename' in config %}
48-
{{ intendation }} filename "{{ config['filename'] }}";
48+
{{ indentation }} filename "{{ config['filename'] }}";
4949
{%- endif %}
5050
{%- if 'next_server' in config %}
51-
{{ intendation }} next-server {{ config['next_server'] }};
51+
{{ indentation }} next-server {{ config['next_server'] }};
5252
{%- endif %}
5353
{%- if 'default_lease_time' in config %}
54-
{{ intendation }} default-lease-time {{ config['default_lease_time'] }};
54+
{{ indentation }} default-lease-time {{ config['default_lease_time'] }};
5555
{%- endif %}
5656
{%- if 'max_lease_time' in config %}
57-
{{ intendation }} max-lease-time {{ config['max_lease_time'] }};
57+
{{ indentation }} max-lease-time {{ config['max_lease_time'] }};
5858
{%- endif %}
5959
{%- if 'routers' in config and config.routers is string %}
60-
{{ intendation }} option routers {{ config.routers }};
60+
{{ indentation }} option routers {{ config.routers }};
6161
{%- elif 'routers' in config and config.routers is sequence %}
62-
{{ intendation }} option routers
62+
{{ indentation }} option routers
6363
{%- for router in config.routers %} {{ router }}
6464
{%- if not loop.last %},{% else %};{% endif %}
6565
{%- endfor %}
6666
{%- endif %}
6767
{%- for option in customized.keys() %}
6868
{%- if option in config %}
6969
{%- if customized[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %}
70-
{{ intendation }} option {{ option|replace('_', '-') }} {{ quote }}{{ config.get(option) }}{{ quote }};
70+
{{ indentation }} option {{ option|replace('_', '-') }} {{ quote }}{{ config.get(option) }}{{ quote }};
7171
{%- endif %}
7272
{%- endfor %}
7373
{%- for pool in salt['pillar.get']('dhcpd:subnets:{0}:pools'.format(subnet), []) %}
74-
{{ intendation }} pool {
74+
{{ indentation }} pool {
7575
{%- if 'failover_peer' in pool %}
76-
{{ intendation }} failover peer "{{ pool['failover_peer'] }}";
76+
{{ indentation }} failover peer "{{ pool['failover_peer'] }}";
7777
{%- endif %}
7878
{%- if 'max_lease_time' in pool %}
79-
{{ intendation }} max-lease-time {{ pool.max_lease_time }};
79+
{{ indentation }} max-lease-time {{ pool.max_lease_time }};
8080
{%- endif %}
8181
{%- if 'range' in pool %}
82-
{{ intendation }} range {{ pool.range[0] }} {{ pool.range[1] }};
82+
{{ indentation }} range {{ pool.range[0] }} {{ pool.range[1] }};
8383
{%- endif %}
8484
{%- if 'allow' in pool %}
85-
{{ intendation }} allow {{ pool.allow }};
85+
{{ indentation }} allow {{ pool.allow }};
8686
{%- elif 'deny' in pool %}
87-
{{ intendation }} deny {{ pool.deny }};
87+
{{ indentation }} deny {{ pool.deny }};
8888
{%- endif %}
89-
{{ intendation }} }
89+
{{ indentation }} }
9090
{%- endfor %}
91-
{{ intendation }}}
91+
{%- for host, config in salt['pillar.get']('dhcpd:subnets:{0}:hosts'.format(subnet), {}).items() %}
92+
{%- include 'dhcpd/files/host.jinja' with context %}
93+
{%- endfor %}
94+
{{ indentation }}}

pillar.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ dhcpd:
7070
broadcast_address: 10.5.5.31
7171
default_lease_time: 600
7272
max_lease_time: 7200
73+
hosts:
74+
jake:
75+
comment: |
76+
Hosts can be specified for subnets, taking subnets defaults
77+
hardware: ethernet 08:00:a7:26:c0:a9
78+
fixed_address: 10.5.5.27
7379

7480
# End of subnets
7581

0 commit comments

Comments
 (0)