Skip to content

Commit

Permalink
Merge pull request #46 from sticky-note/ref/pillar-get
Browse files Browse the repository at this point in the history
Refactor templates to not use `pillar.get`
  • Loading branch information
myii authored Apr 6, 2020
2 parents 1d0b735 + 174e8d2 commit 69d3fdc
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 109 deletions.
9 changes: 8 additions & 1 deletion dhcpd/config.sls
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{% from "dhcpd/map.jinja" import dhcpd with context %}
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}

{% from tplroot ~ "/map.jinja" import dhcpd with context %}
include:
- dhcpd
Expand Down Expand Up @@ -27,6 +30,8 @@ dhcpd.conf:
- mode: 644
- watch_in:
- service: dhcpd
- context:
dhcpd: {{ dhcpd | json }}
{% if dhcpd.service_config is defined %}
Expand All @@ -45,5 +50,7 @@ service_config:
- mode: 644
- watch_in:
- service: dhcpd
- context:
dhcpd: {{ dhcpd | json }}
{% endif %}
133 changes: 62 additions & 71 deletions dhcpd/files/dhcpd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,122 +3,115 @@
# SaltStack-generated configuration file for ISC dhcpd
#

{%- set customized = {} %}
{%- set types_to_quote = ['string', 'text'] %}
{%- set quote = '' %}
{%- set dquote = '"' %}
{% if salt['pillar.get']('dhcpd:customized_options', False) -%}
{%- set customized = salt['pillar.get']('dhcpd:customized_options', {}) %}

# Customized dhcp options
{%- for name, data in customized.items() %}
{%- for name, data in dhcpd.customized_options.items() %}
option {{ name|replace('_', '-') }} code {{ data.code }} = {{ data.type }};
{%- endfor %}
{% endif -%}
{%- endfor %}

# option definitions common to all supported networks...
{% if salt['pillar.get']('dhcpd:domain_name', False) -%}
option domain-name "{{ salt['pillar.get']('dhcpd:domain_name') }}";
{% if dhcpd.domain_name -%}
option domain-name "{{ dhcpd.domain_name }}";
{% endif -%}
{% if salt['pillar.get']('dhcpd:domain_name_servers', False) -%}
{% if dhcpd.domain_name_servers -%}
option domain-name-servers
{%- for dns_server in salt['pillar.get']('dhcpd:domain_name_servers') %} {{ dns_server }}
{%- for dns_server in dhcpd.domain_name_servers %} {{ dns_server }}
{%- if not loop.last %},{% else %};{% endif %}
{%- endfor %}
{% endif -%}
{% if salt['pillar.get']('dhcpd:subnet_mask', False) -%}
option subnet-mask {{ salt['pillar.get']('dhcpd:subnet_mask') }};
{% if dhcpd.subnet_mask -%}
option subnet-mask {{ dhcpd.subnet_mask }};
{% endif -%}
{% if salt['pillar.get']('dhcpd:routers', False) -%}
{% if dhcpd.routers -%}
option routers
{%- for router in salt['pillar.get']('dhcpd:routers') %} {{ router }}
{%- for router in dhcpd.routers %} {{ router }}
{%- if not loop.last %},{% else %};{% endif %}
{%- endfor %}
{% endif -%}
{% if salt['pillar.get']('dhcpd:domain_search', False) -%}
option domain-search {{ dquote }}{{ '" , "'.join(salt['pillar.get']('dhcpd:domain_search', [])) }}{{ dquote }};
{% if dhcpd.domain_search -%}
option domain-search {{ dquote }}{{ dhcpd.domain_search | join( '" , "' ) }}{{ dquote }};
{% endif -%}

{% if salt['pillar.get']('dhcpd:default_lease_time', False) -%}
default-lease-time {{ salt['pillar.get']('dhcpd:default_lease_time') }};
{% if dhcpd.default_lease_time -%}
default-lease-time {{ dhcpd.default_lease_time }};
{% endif -%}
{% if salt['pillar.get']('dhcpd:max_lease_time', False) -%}
max-lease-time {{ salt['pillar.get']('dhcpd:max_lease_time') }};
{% if dhcpd.max_lease_time -%}
max-lease-time {{ dhcpd.max_lease_time }};
{% endif -%}
{% if salt['pillar.get']('dhcpd:one_lease_per_client', False) -%}
one-lease-per-client {{ salt['pillar.get']('dhcpd:one_lease_per_client') }};
{% if dhcpd.one_lease_per_client -%}
one-lease-per-client {{ dhcpd.one_lease_per_client }};
{% endif -%}
{% if salt['pillar.get']('dhcpd:get_lease_hostnames', False) -%}
get-lease-hostnames {{ salt['pillar.get']('dhcpd:get_lease_hostnames') }};
{% if dhcpd.get_lease_hostnames -%}
get-lease-hostnames {{ dhcpd.get_lease_hostnames }};
{% endif -%}
{% if salt['pillar.get']('dhcpd:server_identifier') -%}
server-identifier {{ salt['pillar.get']('dhcpd:server_identifier') }};
{% if dhcpd.server_identifier -%}
server-identifier {{ dhcpd.server_identifier }};
{% endif -%}
{% if salt['pillar.get']('dhcpd:server_name') -%}
server-name {{ dquote }}{{ salt['pillar.get']('dhcpd:server_name') }}{{ dquote }};
{% if dhcpd.server_name -%}
server-name {{ dquote }}{{ dhcpd.server_name }}{{ dquote }};
{% endif -%}
{%- if salt['pillar.get']('dhcpd:use_host_decl_names', False) %}
{%- if dhcpd.use_host_decl_names %}
use-host-decl-names on;
{%- else %}
#use-host-decl-names off;
{%- endif %}

{%- if salt['pillar.get']('dhcpd:allow', False) %}
{%- set allow = salt['pillar.get']('dhcpd:allow') %}
{%- if allow is iterable and allow is not string %}
{%- for item in allow %}allow {{ item }};{%- endfor %}
{%- else %}allow {{ allow }};
{%- if dhcpd.allow %}
{%- if dhcpd.allow is iterable and dhcpd.allow is not string %}
{%- for item in dhcpd.allow %}allow {{ item }};{%- endfor %}
{%- else %}allow {{ dhcpd.allow }};
{%- endif %}
{%- endif %}

{%- if salt['pillar.get']('dhcpd:deny', False) %}
{%- set deny = salt['pillar.get']('dhcpd:deny') %}
{%- if deny is iterable and deny is not string %}
{%- for item in deny %}
deny {{ item }};
{%- endfor %}
{%- else %}deny {{ deny }};
{%- if dhcpd.deny %}
{%- if dhcpd.deny is iterable and dhcpd.deny is not string %}
{%- for item in dhcpd.deny %}deny {{ item }};{%- endfor %}
{%- else %}deny {{ dhcpd.deny }};
{%- endif %}
{%- endif %}

# Use this to enble / disable dynamic dns updates globally.
{%- if salt['pillar.get']('dhcpd:ddns_update_style', False) %}
ddns-update-style {{ salt['pillar.get']('dhcpd:ddns_update_style') }};
{%- if dhcpd.ddns_update_style %}
ddns-update-style {{ dhcpd.ddns_update_style }};
{%- else %}
#ddns-update-style none;
{%- endif %}

{%- if salt['pillar.get']('dhcpd:ddns_domainname', '') %}
ddns-domainname "{{ salt['pillar.get']('dhcpd:ddns_domainname') }}";
{%- if dhcpd.ddns_domainname %}
ddns-domainname "{{ dhcpd.ddns_domainname }}";
{%- endif %}

{%- if salt['pillar.get']('dhcpd:update_static_leases', False) %}
{%- if dhcpd.update_static_leases %}
update-static-leases on;
{%- else %}
#update-static-leases off;
{%- endif %}

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
{%- if salt['pillar.get']('dhcpd:authoritative', False) %}
{%- if dhcpd.authoritative %}
authoritative;
{%- else %}
#authoritative;
{%- endif %}

# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
{%- if salt['pillar.get']('dhcpd:log_facility', False) %}
log-facility {{ salt['pillar.get']('dhcpd:log_facility') }};
{%- if dhcpd.log_facility %}
log-facility {{ dhcpd.log_facility }};
{%- endif %}

{%- for option in customized.keys() %}
{%- if option in salt['pillar.get']('dhcpd') %}
{%- if customized[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %}
option {{ option|replace('_', '-') }} {{ quote }}{{ salt['pillar.get']('dhcpd:' ~ option) }}{{ quote }};
{%- for option in dhcpd.customized_options.keys() %}
{%- if option in dhcpd %}
{%- if dhcpd.customized_options[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %}
option {{ option|replace('_', '-') }} {{ quote }}{{ dhcpd.get(option) }}{{ quote }};
{%- endif %}
{%- endfor %}

{%- for class, config in salt['pillar.get']('dhcpd:classes', {}).items() %}
{%- for class, config in dhcpd.classes.items() %}
{%- if 'comment' in config %}
{%- for line in config.comment.splitlines() %}
# {{ line }}
Expand All @@ -128,16 +121,16 @@ class "{{ class }}" {
{%- if 'match' in config %}
match {{ config.match }};
{%- endif %}
{%- for option in customized.keys() %}
{%- for option in dhcpd.customized_options.keys() %}
{%- if option in config %}
{%- if customized[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %}
{%- if dhcpd.customized_options[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %}
option {{ option|replace('_', '-') }} {{ quote }}{{ config.get(option) }}{{ quote }};
{%- endif %}
{%- endfor %}
}
{%- endfor %}

{%- for failover_peer, config in salt['pillar.get']('dhcpd:failover_peers', {}).items() %}
{%- for failover_peer, config in dhcpd.failover_peers.items() %}
{%- if 'comment' in config %}
{%- for line in config.comment.splitlines() %}
# {{ line }}
Expand Down Expand Up @@ -193,7 +186,7 @@ failover peer "{{ failover_peer }}" {
}
{%- endfor %}

{%- for key, config in salt['pillar.get']('dhcpd:keys', {}).items() %}
{%- for key, config in dhcpd.get('keys', {}).items() %}
key {{ key }} {
{%- if 'algorithm' in config %}
algorithm {{ config.algorithm }};
Expand All @@ -204,7 +197,7 @@ key {{ key }} {
}
{%- endfor %}

{%- for zone, config in salt['pillar.get']('dhcpd:zones', {}).items() %}
{%- for zone, config in dhcpd.get('zones', {}).items() %}
zone {{ zone }} {
{%- if 'primary' in config %}
primary {{ config.primary }};
Expand All @@ -215,28 +208,26 @@ zone {{ zone }} {
}
{%- endfor %}

{%- for subnet, config in salt['pillar.get']('dhcpd:subnets', {}).items() %}
{%- for subnet, config in dhcpd.subnets.items() %}
{%- include 'dhcpd/files/subnet.jinja' with context %}
{%- endfor %}

{% for host, config in salt['pillar.get']('dhcpd:hosts', {}).items() %}
{% for host, config in dhcpd.hosts.items() %}
{%- include 'dhcpd/files/host.jinja' with context %}
{% endfor %}
{%- for shared_network, config in salt['pillar.get']('dhcpd:shared_networks', {}).items() %}
{%- if 'comment' in config %}
{%- for line in config.comment.splitlines() %}
{%- for shared_network, sn_config in dhcpd.shared_networks.items() %}
{%- if 'comment' in sn_config %}
{%- for line in sn_config.comment.splitlines() %}
# {{ line }}
{%- endfor %}
{%- endif %}
shared-network {{ shared_network }} {
{%- for subnet, config in salt['pillar.get'](
'dhcpd:shared_networks:{0}:subnets'.format(shared_network), {}).items() %}
{%- for subnet, config in sn_config.get('subnets', {}).items() %}
{%- filter indent(width=2) %}
{% include 'dhcpd/files/subnet.jinja' with context %}
{%- endfilter %}
{%- endfor %}
{%- for pool in salt['pillar.get'](
'dhcpd:shared_networks:{0}:pools'.format(shared_network), []) %}
{%- for pool in sn_config.get( 'pools', [] ) %}
pool {
{%- if 'allow' in pool %}
allow {{ pool.allow }};
Expand All @@ -248,9 +239,9 @@ shared-network {{ shared_network }} {
{%- endif %}
}
{%- endfor %}
{%- for option in customized.keys() %}
{%- if option in config %}
{%- if customized[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %}
{%- for option in dhcpd.customized_options.keys() %}
{%- if option in sn_config %}
{%- if dhcpd.customized_options[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %}
option {{ option|replace('_', '-') }} {{ quote }}{{ config.get(option) }}{{ quote }};
{%- endif %}
{%- endfor %}
Expand Down
4 changes: 2 additions & 2 deletions dhcpd/files/host.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ host {{ host }} {
{%- if 'host_name' in config %}
option host-name "{{ config.host_name }}";
{%- endif %}
{%- for option in customized.keys() %}
{%- for option in dhcpd.customized_options.keys() %}
{%- if option in config %}
{%- if customized[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %}
{%- if dhcpd.customized_options[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %}
option {{ option|replace('_', '-') }} {{ quote }}{{ config.get(option) }}{{ quote }};
{%- endif %}
{%- endfor %}
Expand Down
2 changes: 1 addition & 1 deletion dhcpd/files/service_config.Debian
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@

# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
# Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACES="{{ ' '.join(salt['pillar.get']('dhcpd:listen_interfaces', [])) }}"
INTERFACES="{{ dhcpd.listen_interfaces | join(' ') }}"
2 changes: 1 addition & 1 deletion dhcpd/files/service_config.FreeBSD
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# SaltStack-generated demon configuration file for ISC dhcpd

dhcpd_ifaces="{{ ' '.join(salt['pillar.get']('dhcpd:listen_interfaces', [])) }}"
dhcpd_ifaces="{{ dhcpd.listen_interfaces | join(' ') }}"
2 changes: 1 addition & 1 deletion dhcpd/files/service_config.Gentoo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# Configure which interface or interfaces to for dhcpd to listen on.
# List all interfaces space separated. If this is not specified then
# we listen on all interfaces.
DHCPD_IFACE="{{ ' '.join(salt['pillar.get']('dhcpd:listen_interfaces', [])) }}"
DHCPD_IFACE="{{ dhcpd.listen_interfaces | join(' ') }}"

# Insert any other dhcpd options - see the man page for a full list.
# DHCPD_OPTS=""
2 changes: 1 addition & 1 deletion dhcpd/files/service_config.RedHat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[Service]
ExecStart=
ExecStart=/usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid {{ ' '.join(salt['pillar.get']('dhcpd:listen_interfaces', [])) }}
ExecStart=/usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid {{ dhcpd.listen_interfaces | join(' ') }}
8 changes: 4 additions & 4 deletions dhcpd/files/subnet.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ subnet {{ subnet }} netmask {{ config.netmask }} {
{%- if not loop.last %},{% else %};{% endif %}
{%- endfor %}
{%- endif %}
{%- for option in customized.keys() %}
{%- for option in dhcpd.customized_options.keys() %}
{%- if option in config %}
{%- if customized[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %}
{%- if dhcpd.customized_options[option]['type'] in types_to_quote %} {% set quote = dquote %} {%- endif %}
option {{ option|replace('_', '-') }} {{ quote }}{{ config.get(option) }}{{ quote }};
{%- endif %}
{%- endfor %}
{%- for pool in salt['pillar.get']('dhcpd:subnets:{0}:pools'.format(subnet), []) %}
{%- for pool in config.get( 'pools', [] ) %}
pool {
{%- if 'failover_peer' in pool %}
failover peer "{{ pool['failover_peer'] }}";
Expand All @@ -88,7 +88,7 @@ subnet {{ subnet }} netmask {{ config.netmask }} {
{%- endif %}
}
{%- endfor %}
{%- for host, config in salt['pillar.get']('dhcpd:subnets:{0}:hosts'.format(subnet), {}).items() %}
{%- for host, config in config.get('hosts', {}).items() %}
{%- filter indent(width=2) %}
{% include 'dhcpd/files/host.jinja' with context %}
{%- endfilter %}
Expand Down
9 changes: 8 additions & 1 deletion dhcpd/map.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{#- Retrieve the config dict only once #}
{%- set _config = salt['config.get'](tplroot, default={}) %}

{%- set config = salt['grains.filter_by'](
{%- set defaults = salt['grains.filter_by'](
default_settings,
default=tplroot,
merge=salt['grains.filter_by'](
Expand All @@ -39,4 +39,11 @@
)
%}

{%- set config = salt['grains.filter_by'](
{'defaults': defaults},
default='defaults',
merge=_config
)
%}

{%- set dhcpd = config %}
Loading

0 comments on commit 69d3fdc

Please sign in to comment.