From 340dc063c0a481afae5c30a62c0676c59c4d3ed2 Mon Sep 17 00:00:00 2001 From: Ondrej Holecek Date: Fri, 15 Jun 2018 11:35:41 +0200 Subject: [PATCH 1/3] Allow hosts to be specified for subnet as well This allows host entries to be defined under subnet. --- dhcpd/files/dhcpd.conf | 42 +--------------------------------------- dhcpd/files/host.jinja | 41 +++++++++++++++++++++++++++++++++++++++ dhcpd/files/subnet.jinja | 3 +++ pillar.example | 6 ++++++ 4 files changed, 51 insertions(+), 41 deletions(-) create mode 100644 dhcpd/files/host.jinja diff --git a/dhcpd/files/dhcpd.conf b/dhcpd/files/dhcpd.conf index 567a06c..d042461 100644 --- a/dhcpd/files/dhcpd.conf +++ b/dhcpd/files/dhcpd.conf @@ -221,47 +221,7 @@ zone {{ zone }} { {%- endfor %} {% for host, config in salt['pillar.get']('dhcpd:hosts', {}).items() %} - {%- if 'comment' in config %} - {%- for line in config.comment.splitlines() %} -# {{ line }} - {%- endfor %} - {%- endif %} -host {{ host }} { - {%- if 'allow' in config %} - {%- if config.allow is iterable and config.allow is not string %} - {%- for item in config.allow %}allow {{ item }};{%- endfor %} - {%- else %}allow {{ config.allow }};{%- endif %} - {%- endif %} - {%- if 'deny' in config %} - {%- if config.deny is iterable and config.deny is not string %} - {%- for item in config.deny %}deny {{ item }};{%- endfor %} - {%- else %}deny {{ config.deny }};{%- endif %} - {%- endif %} - {%- if 'hardware' in config %} - hardware {{ config.hardware }}; - {%- endif %} - {%- if 'fixed_address' in config %} - fixed-address {{ config.fixed_address }}; - {%- endif %} - {%- if 'filename' in config %} - filename "{{ config.filename }}"; - {%- endif %} - {%- if 'next_server' in config %} - next-server {{ config.next_server }}; - {%- endif %} - {%- if 'server_name' in config %} - server-name "{{ config.server_name }}"; - {%- endif %} - {%- if 'host_name' in config %} - option host-name "{{ config.host_name }}"; - {%- endif %} - {%- for option in customized.keys() %} - {%- if option in config %} - {%- if customized[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %} - option {{ option|replace('_', '-') }} {{ quote }}{{ config.get(option) }}{{ quote }}; - {%- endif %} - {%- endfor %} -} +{%- include 'dhcpd/files/host.jinja' with context %} {% endfor %} {%- for shared_network, config in salt['pillar.get']('dhcpd:shared_networks', {}).items() %} {%- if 'comment' in config %} diff --git a/dhcpd/files/host.jinja b/dhcpd/files/host.jinja new file mode 100644 index 0000000..ec682e2 --- /dev/null +++ b/dhcpd/files/host.jinja @@ -0,0 +1,41 @@ +{%- if 'comment' in config %} + {%- for line in config.comment.splitlines() %} +{{ intendation }}# {{ line }} + {%- endfor %} +{%- endif %} +{{ intendation }}host {{ host }} { + {%- if 'allow' in config %} + {%- if config.allow is iterable and config.allow is not string %} + {%- for item in config.allow %}allow {{ item }};{%- endfor %} + {%- else %}allow {{ config.allow }};{%- endif %} + {%- endif %} + {%- if 'deny' in config %} + {%- if config.deny is iterable and config.deny is not string %} + {%- for item in config.deny %}deny {{ item }};{%- endfor %} + {%- else %}deny {{ config.deny }};{%- endif %} + {%- endif %} + {%- if 'hardware' in config %} +{{ intendation }} hardware {{ config.hardware }}; + {%- endif %} + {%- if 'fixed_address' in config %} +{{ intendation }} fixed-address {{ config.fixed_address }}; + {%- endif %} + {%- if 'filename' in config %} +{{ intendation }} filename "{{ config.filename }}"; + {%- endif %} + {%- if 'next_server' in config %} +{{ intendation }} next-server {{ config.next_server }}; + {%- endif %} + {%- if 'server_name' in config %} +{{ intendation }} server-name "{{ config.server_name }}"; + {%- endif %} + {%- if 'host_name' in config %} +{{ intendation }} option host-name "{{ config.host_name }}"; + {%- endif %} + {%- for option in customized.keys() %} + {%- if option in config %} + {%- if customized[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %} +{{ intendation }} option {{ option|replace('_', '-') }} {{ quote }}{{ config.get(option) }}{{ quote }}; + {%- endif %} + {%- endfor %} +} diff --git a/dhcpd/files/subnet.jinja b/dhcpd/files/subnet.jinja index 8e47905..7cd715e 100644 --- a/dhcpd/files/subnet.jinja +++ b/dhcpd/files/subnet.jinja @@ -88,4 +88,7 @@ {%- endif %} {{ intendation }} } {%- endfor %} +{%- for host, config in salt['pillar.get']('dhcpd:subnets:{0}:hosts'.format(subnet), {}).items() %} +{%- include 'dhcpd/files/host.jinja' with context %} +{%- endfor %} {{ intendation }}} diff --git a/pillar.example b/pillar.example index 5c37110..eef0076 100644 --- a/pillar.example +++ b/pillar.example @@ -70,6 +70,12 @@ dhcpd: broadcast_address: 10.5.5.31 default_lease_time: 600 max_lease_time: 7200 + hosts: + jake: + comment: | + Hosts can be specified for subnets, taking subnets defaults + hardware: ethernet 08:00:a7:26:c0:a9 + fixed_address: 10.5.5.27 # End of subnets From c462d8fb2533c43d2cc48989c922a50da79f9038 Mon Sep 17 00:00:00 2001 From: Ondrej Holecek Date: Tue, 30 Jul 2019 11:25:29 +0200 Subject: [PATCH 2/3] Expand oneline for cycles based on review feedback --- dhcpd/files/host.jinja | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/dhcpd/files/host.jinja b/dhcpd/files/host.jinja index ec682e2..6ced40d 100644 --- a/dhcpd/files/host.jinja +++ b/dhcpd/files/host.jinja @@ -1,18 +1,26 @@ {%- if 'comment' in config %} - {%- for line in config.comment.splitlines() %} + {%- for line in config.comment.splitlines() %} {{ intendation }}# {{ line }} - {%- endfor %} + {%- endfor %} {%- endif %} {{ intendation }}host {{ host }} { {%- if 'allow' in config %} {%- if config.allow is iterable and config.allow is not string %} - {%- for item in config.allow %}allow {{ item }};{%- endfor %} - {%- else %}allow {{ config.allow }};{%- endif %} + {%- for item in config.allow %} +{{ intendation }} allow {{ item }}; + {%- endfor %} + {%- else %} +{{ intendation }} allow {{ config.allow }}; + {%- endif %} {%- endif %} {%- if 'deny' in config %} {%- if config.deny is iterable and config.deny is not string %} - {%- for item in config.deny %}deny {{ item }};{%- endfor %} - {%- else %}deny {{ config.deny }};{%- endif %} + {%- for item in config.deny %} +{{ intendation }} deny {{ item }}; + {%- endfor %} + {%- else %} +{{ intendation }} deny {{ config.deny }}; + {%- endif %} {%- endif %} {%- if 'hardware' in config %} {{ intendation }} hardware {{ config.hardware }}; From e8bb413dea2c5322e8f8d37fd8910b495648c27b Mon Sep 17 00:00:00 2001 From: Ondrej Holecek Date: Tue, 30 Jul 2019 11:27:45 +0200 Subject: [PATCH 3/3] Sed intendation to indentation --- dhcpd/files/dhcpd.conf | 4 +-- dhcpd/files/host.jinja | 26 +++++++++--------- dhcpd/files/subnet.jinja | 58 ++++++++++++++++++++-------------------- 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/dhcpd/files/dhcpd.conf b/dhcpd/files/dhcpd.conf index d042461..45bbdd1 100644 --- a/dhcpd/files/dhcpd.conf +++ b/dhcpd/files/dhcpd.conf @@ -193,7 +193,7 @@ failover peer "{{ failover_peer }}" { } {%- endfor %} -{%- set intendation='' %} +{%- set indentation='' %} {%- for key, config in salt['pillar.get']('dhcpd:keys', {}).items() %} key {{ key }} { {%- if 'algorithm' in config %} @@ -230,7 +230,7 @@ zone {{ zone }} { {%- endfor %} {%- endif %} shared-network {{ shared_network }} { -{%- set intendation=' ' %} +{%- set indentation=' ' %} {%- for subnet, config in salt['pillar.get']( 'dhcpd:shared_networks:{0}:subnets'.format(shared_network), {}).items() %} {%- include 'dhcpd/files/subnet.jinja' with context %} diff --git a/dhcpd/files/host.jinja b/dhcpd/files/host.jinja index 6ced40d..770f07d 100644 --- a/dhcpd/files/host.jinja +++ b/dhcpd/files/host.jinja @@ -1,49 +1,49 @@ {%- if 'comment' in config %} {%- for line in config.comment.splitlines() %} -{{ intendation }}# {{ line }} +{{ indentation }}# {{ line }} {%- endfor %} {%- endif %} -{{ intendation }}host {{ host }} { +{{ indentation }}host {{ host }} { {%- if 'allow' in config %} {%- if config.allow is iterable and config.allow is not string %} {%- for item in config.allow %} -{{ intendation }} allow {{ item }}; +{{ indentation }} allow {{ item }}; {%- endfor %} {%- else %} -{{ intendation }} allow {{ config.allow }}; +{{ indentation }} allow {{ config.allow }}; {%- endif %} {%- endif %} {%- if 'deny' in config %} {%- if config.deny is iterable and config.deny is not string %} {%- for item in config.deny %} -{{ intendation }} deny {{ item }}; +{{ indentation }} deny {{ item }}; {%- endfor %} {%- else %} -{{ intendation }} deny {{ config.deny }}; +{{ indentation }} deny {{ config.deny }}; {%- endif %} {%- endif %} {%- if 'hardware' in config %} -{{ intendation }} hardware {{ config.hardware }}; +{{ indentation }} hardware {{ config.hardware }}; {%- endif %} {%- if 'fixed_address' in config %} -{{ intendation }} fixed-address {{ config.fixed_address }}; +{{ indentation }} fixed-address {{ config.fixed_address }}; {%- endif %} {%- if 'filename' in config %} -{{ intendation }} filename "{{ config.filename }}"; +{{ indentation }} filename "{{ config.filename }}"; {%- endif %} {%- if 'next_server' in config %} -{{ intendation }} next-server {{ config.next_server }}; +{{ indentation }} next-server {{ config.next_server }}; {%- endif %} {%- if 'server_name' in config %} -{{ intendation }} server-name "{{ config.server_name }}"; +{{ indentation }} server-name "{{ config.server_name }}"; {%- endif %} {%- if 'host_name' in config %} -{{ intendation }} option host-name "{{ config.host_name }}"; +{{ indentation }} option host-name "{{ config.host_name }}"; {%- endif %} {%- for option in customized.keys() %} {%- if option in config %} {%- if customized[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %} -{{ intendation }} option {{ option|replace('_', '-') }} {{ quote }}{{ config.get(option) }}{{ quote }}; +{{ indentation }} option {{ option|replace('_', '-') }} {{ quote }}{{ config.get(option) }}{{ quote }}; {%- endif %} {%- endfor %} } diff --git a/dhcpd/files/subnet.jinja b/dhcpd/files/subnet.jinja index 7cd715e..6e65ecd 100644 --- a/dhcpd/files/subnet.jinja +++ b/dhcpd/files/subnet.jinja @@ -3,63 +3,63 @@ # {{ line }} {%- endfor %} {%- endif %} -{{ intendation }}subnet {{ subnet }} netmask {{ config.netmask }} { +{{ indentation }}subnet {{ subnet }} netmask {{ config.netmask }} { {%- if 'use_host_decl_names' in config %} -{{ intendation }} use-host-decl-names {{ config.use_host_decl_names }}; +{{ indentation }} use-host-decl-names {{ config.use_host_decl_names }}; {%- endif %} {%- if 'range' in config %} {%- if 'dynamic_bootp' in config and config.dynamic_bootp %} -{{ intendation }} range dynamic-bootp {{ config.range[0] }} {{ config.range[1] }}; +{{ indentation }} range dynamic-bootp {{ config.range[0] }} {{ config.range[1] }}; {%- else %} -{{ intendation }} range {{ config.range[0] }} {{ config.range[1] }}; +{{ indentation }} range {{ config.range[0] }} {{ config.range[1] }}; {%- endif %} {%- endif %} {%- if 'broadcast_address' in config %} -{{ intendation }} option broadcast-address {{ config['broadcast_address'] }}; +{{ indentation }} option broadcast-address {{ config['broadcast_address'] }}; {%- endif %} {%- if 'domain_name_servers' in config %} -{{ intendation }} option domain-name-servers {{ config['domain_name_servers']|join(',') }}; +{{ indentation }} option domain-name-servers {{ config['domain_name_servers']|join(',') }}; {%- endif %} {%- if 'netbios_name_servers' in config %} -{{ intendation }} option netbios-name-servers {{ config['netbios_name_servers']|join(',') }}; +{{ indentation }} option netbios-name-servers {{ config['netbios_name_servers']|join(',') }}; {%- endif %} {%- if 'ntp_servers' in config %} -{{ intendation }} option ntp-servers {{ config['ntp_servers']|join(',') }}; +{{ indentation }} option ntp-servers {{ config['ntp_servers']|join(',') }}; {%- endif %} {%- if 'lpr_servers' in config %} -{{ intendation }} option lpr-servers {{ config['lpr_servers']|join(',') }}; +{{ indentation }} option lpr-servers {{ config['lpr_servers']|join(',') }}; {%- endif %} {%- if 'irc_server' in config %} -{{ intendation }} option irc-server {{ config['irc_server']|join(',') }}; +{{ indentation }} option irc-server {{ config['irc_server']|join(',') }}; {%- endif %} {%- if 'tftp_server_name' in config %} -{{ intendation }} option tftp-server-name "{{ config['tftp_server_name'] }}"; +{{ indentation }} option tftp-server-name "{{ config['tftp_server_name'] }}"; {%- endif %} {%- if 'smtp_server' in config %} -{{ intendation }} option smtp-server {{ config['smtp_server'] }}; +{{ indentation }} option smtp-server {{ config['smtp_server'] }}; {%- endif %} {%- if 'domain_name' in config %} -{{ intendation }} option domain-name "{{ config['domain_name'] }}"; +{{ indentation }} option domain-name "{{ config['domain_name'] }}"; {%- endif %} {%- if 'domain_search' in config %} -{{ intendation }} option domain-search "{{ config['domain_search']|join('","') }}"; +{{ indentation }} option domain-search "{{ config['domain_search']|join('","') }}"; {%- endif %} {%- if 'filename' in config %} -{{ intendation }} filename "{{ config['filename'] }}"; +{{ indentation }} filename "{{ config['filename'] }}"; {%- endif %} {%- if 'next_server' in config %} -{{ intendation }} next-server {{ config['next_server'] }}; +{{ indentation }} next-server {{ config['next_server'] }}; {%- endif %} {%- if 'default_lease_time' in config %} -{{ intendation }} default-lease-time {{ config['default_lease_time'] }}; +{{ indentation }} default-lease-time {{ config['default_lease_time'] }}; {%- endif %} {%- if 'max_lease_time' in config %} -{{ intendation }} max-lease-time {{ config['max_lease_time'] }}; +{{ indentation }} max-lease-time {{ config['max_lease_time'] }}; {%- endif %} {%- if 'routers' in config and config.routers is string %} -{{ intendation }} option routers {{ config.routers }}; +{{ indentation }} option routers {{ config.routers }}; {%- elif 'routers' in config and config.routers is sequence %} -{{ intendation }} option routers +{{ indentation }} option routers {%- for router in config.routers %} {{ router }} {%- if not loop.last %},{% else %};{% endif %} {%- endfor %} @@ -67,28 +67,28 @@ {%- for option in customized.keys() %} {%- if option in config %} {%- if customized[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %} -{{ intendation }} option {{ option|replace('_', '-') }} {{ quote }}{{ config.get(option) }}{{ quote }}; +{{ indentation }} option {{ option|replace('_', '-') }} {{ quote }}{{ config.get(option) }}{{ quote }}; {%- endif %} {%- endfor %} {%- for pool in salt['pillar.get']('dhcpd:subnets:{0}:pools'.format(subnet), []) %} -{{ intendation }} pool { +{{ indentation }} pool { {%- if 'failover_peer' in pool %} -{{ intendation }} failover peer "{{ pool['failover_peer'] }}"; +{{ indentation }} failover peer "{{ pool['failover_peer'] }}"; {%- endif %} {%- if 'max_lease_time' in pool %} -{{ intendation }} max-lease-time {{ pool.max_lease_time }}; +{{ indentation }} max-lease-time {{ pool.max_lease_time }}; {%- endif %} {%- if 'range' in pool %} -{{ intendation }} range {{ pool.range[0] }} {{ pool.range[1] }}; +{{ indentation }} range {{ pool.range[0] }} {{ pool.range[1] }}; {%- endif %} {%- if 'allow' in pool %} -{{ intendation }} allow {{ pool.allow }}; +{{ indentation }} allow {{ pool.allow }}; {%- elif 'deny' in pool %} -{{ intendation }} deny {{ pool.deny }}; +{{ indentation }} deny {{ pool.deny }}; {%- endif %} -{{ intendation }} } +{{ indentation }} } {%- endfor %} {%- for host, config in salt['pillar.get']('dhcpd:subnets:{0}:hosts'.format(subnet), {}).items() %} {%- include 'dhcpd/files/host.jinja' with context %} {%- endfor %} -{{ intendation }}} +{{ indentation }}}