diff --git a/templates/etc/dhcp/dhcpd.conf.j2 b/templates/etc/dhcp/dhcpd.conf.j2 index 3049878..e619b1b 100644 --- a/templates/etc/dhcp/dhcpd.conf.j2 +++ b/templates/etc/dhcp/dhcpd.conf.j2 @@ -1,192 +1,5 @@ -# This file is managed by Ansible, all changes will be lost -{% macro print_class(class) %} -{% if class.comment is defined and class.comment %} -# {{ class.comment }} -{% endif %} -class "{{ class.class }}" { -{% if class.options is defined and class.options %} -{{ class.options | indent(8,true) }} -{% endif %} -{% if class.include is defined and class.include %} - include "{{ class.include }}"; -{% endif %} -} -{% if class.subclass is defined and class.subclass %} - -{% for key, value in class.subclass.iteritems() %} -{% if value is defined and value %} -subclass "{{ class.class }}" "{{ key }}" { -{{ value | indent(8,true) }} -} - -{% else %} -subclass "{{ class.class }}" {{ key }}; -{% endif %} -{% endfor %} -{% endif %} -{% endmacro %} -{% macro print_group(group) %} -{% if group.comment is defined and group.comment %} -# {{ group.comment }} -{% endif %} -group { -{% if group.options is defined and group.options %} -{{ group.options | indent(8,true) }} -{% endif %} -{% if group.include is defined and group.include %} - include "{{ group.include }}"; -{% endif %} -{% if group.groups is defined and group.groups %} -{% for group in group.groups %} -{{ print_group(group) | indent(8, true) }} -{% endfor %} -{% endif %} -{% if group.subnets is defined and group.subnets %} -{% for subnet in group.subnets %} -{{ print_subnet(subnet) | indent(8, true) }} -{% endfor %} -{% endif %} -{% if group.hosts is defined and group.hosts %} -{{ print_hosts(group.hosts) | indent(8, true) }} -{% endif %} -} -{% endmacro %} -{% macro print_subnet(subnet) %} -{% if subnet.comment is defined and subnet.comment %} -# {{ subnet.comment }} -{% endif %} -{% if subnet.ipv6 is defined and subnet.ipv6 %} -subnet6 {{ subnet.subnet }} { -{% else %} -subnet {{ subnet.subnet }} netmask {{ subnet.netmask }} { -{% endif %} -{% if subnet.routers is defined and subnet.routers %} -{% if subnet.routers is string %} - option routers {{ subnet.routers }}; -{% else %} - option routers {{ subnet.routers | join(' ') }}; -{% endif %} -{% endif %} -{% if subnet.options is defined and subnet.options %} -{{ subnet.options | indent(8,true) }} -{% endif %} -{% if subnet.include is defined and subnet.include %} - include "{{ subnet.include }}"; -{% endif %} -{% if subnet.pools is defined and subnet.pools %} -{% for pool in subnet.pools %} - pool { -{% if pool.comment is defined and pool.comment %} - # {{ pool.comment }} -{% endif %} -{% if subnet.ipv6 is defined and subnet.ipv6 %} - range6 {{ pool.range }}; -{% else %} - range {{ pool.range }}; -{% endif %} -{% if pool.options is defined and pool.options %} -{{ pool.options | indent(16,true) }} -{% endif %} -{% if pool.include is defined and pool.include %} - include "{{ pool.include }}"; -{% endif %} - } -{% endfor %} -{% endif %} -{% if subnet.hosts is defined and subnet.hosts %} -{{ print_hosts(subnet.hosts) | indent(8, true) }} -{% endif %} -} -{% endmacro %} -{% macro print_hosts(hosts) %} -{% if hosts is string %} -include "{{ hosts }}"; -{% else %} -{% for host in hosts %} -{% if host.comment is defined and host.comment %} -# {{ host.comment }} -{% endif %} -host {{ host.hostname }} { -{% if host.options is defined and host.options %} -{{ host.options | indent(8,true) }} -{% endif %} -{% if host.ethernet is defined and host.ethernet %} - hardware ethernet {{ host.ethernet }}; -{% endif %} -{% if host.address is defined and host.address %} - fixed-address {{ host.address }}; -{% endif %} -} -{% endfor %} -{% endif %} -{% endmacro %} -{% macro print_failover(failover) %} -{% if failover.comment is defined and failover.comment %} -# {{ failover.comment }} -{% endif %} -failover peer "{{ failover.failover }}" { -{% if failover.primary is defined and failover.primary == inventory_hostname %} - primary; - mclt {{ failover.mclt|default(3600) }}; -{% if failover.primary_fo_addr is defined and failover.primary_fo_addr %} - address {{ failover.primary_fo_addr }}; -{% else %} - address {{ failover.primary }}; -{% endif %} -{% if failover.secondary_fo_addr is defined and failover.secondary_fo_addr %} - peer address {{ failover.secondary_fo_addr }}; -{% else %} - peer address {{ failover.secondary }}; -{% endif %} -{% if failover.split is defined and failover.split %} - split {{ failover.split }}; -{% elif failover.hba is defined and failover.hba %} - hba {{ failover.hba }}; -{% endif %} -{% else %} - secondary; -{% if failover.secondary_fo_addr is defined and failover.secondary_fo_addr %} - address {{ failover.secondary_fo_addr }}; -{% else %} - address {{ failover.secondary }}; -{% endif %} -{% if failover.primary_fo_addr is defined and failover.primary_fo_addr %} - peer address {{ failover.primary_fo_addr }}; -{% else %} - peer address {{ failover.primary }}; -{% endif %} -{% endif %} - max-response-delay {{ failover.max_response_delay|default(30) }}; - max-unacked-updates {{ failover.max_unacked_updates|default(10) }}; -{% if failover.load_balance_max_seconds is defined and failover.load_balance_max_seconds %} - load balance max seconds {{ failover.load_balance_max_seconds }}; -{% endif %} -{% if failover.max_lease_misbalance is defined and failover.max_lease_misbalance %} - max-lease-misbalance {{ failover.max_lease_misbalance }}; -{% endif %} -{% if failover.max_lease_ownership is defined and failover.max_lease_ownership %} - max-lease-ownership {{ failover.max_lease_ownership }}; -{% endif %} -{% if failover.min_balance is defined and failover.min_balance %} - min-balance {{ failover.min_balance }}; -{% endif %} -{% if failover.max_balance is defined and failover.max_balance %} - max-balance {{ failover.max_balance }}; -{% endif %} -{% if failover.auto_partner_down is defined and failover.auto_partner_down %} - auto-partner-down {{ failover.auto_partner_down }}; -{% endif %} -} -{% endmacro %} -{% macro print_key(key) %} -{% if key.comment is defined and key.comment %} -# {{ key.comment }} -{% endif %} -key "{{ key.key }}" { - algorithm {{ key.algorithm|default('hmac-md5') }}; - secret {{ key.secret }}; -} -{% endmacro %} +{% import 'macros.j2' as print with context %} +# {{ ansible_managed }} {% if dhcpd_authoritative is defined and dhcpd_authoritative %} authoritative; @@ -207,17 +20,17 @@ not authoritative; {% endif %} {% if dhcpd_keys is defined and dhcpd_keys %} {% for key in dhcpd_keys %} -{{ print_key(key) }} +{{ print.key(key) }} {% endfor %} {% endif %} {% if dhcpd_classes is defined and dhcpd_classes %} {% for class in dhcpd_classes %} -{{ print_class(class) }} +{{ print.class(class) }} {% endfor %} {% endif %} {% if dhcpd_failovers is defined and dhcpd_failovers %} {% for failover in dhcpd_failovers %} -{{ print_failover(failover) }} +{{ print.failover(failover) }} {% endfor %} {% endif %} {% if dhcpd_shared_networks is defined and dhcpd_shared_networks %} @@ -235,7 +48,7 @@ shared-network "{{ network.name }}" { {% endif %} {% for subnet in network.subnets %} -{{ print_subnet(subnet) | indent(8,true) }} +{{ print.subnet(subnet) | indent(8,true) }} {% endfor %} } @@ -244,16 +57,16 @@ shared-network "{{ network.name }}" { {% endif %} {% if dhcpd_groups is defined and dhcpd_groups %} {% for group in dhcpd_groups %} -{{ print_group(group) }} +{{ print.group(group) }} {% endfor %} {% endif %} {% if dhcpd_subnets is defined and dhcpd_subnets %} {% for subnet in dhcpd_subnets %} -{{ print_subnet(subnet) }} +{{ print.subnet(subnet) }} {% endfor %} {% endif %} {% if dhcpd_hosts is defined and dhcpd_hosts %} -{{ print_hosts(dhcpd_hosts) }} +{{ print.hosts(dhcpd_hosts) }} {% endif %} {% if dhcpd_includes is defined and dhcpd_includes %} {% for include in dhcpd_includes %} diff --git a/templates/etc/dhcp/macros.j2 b/templates/etc/dhcp/macros.j2 new file mode 100644 index 0000000..096cb12 --- /dev/null +++ b/templates/etc/dhcp/macros.j2 @@ -0,0 +1,218 @@ +{# +# List of macros for ISC DHCP configuration, IPv4 +# =============================================== +# +# ---- Macro: print.class() ---- +#} +{% macro class(class) %} +{% if class.comment is defined and class.comment %} +# {{ class.comment }} +{% endif %} +class "{{ class.class }}" { +{% if class.options is defined and class.options %} +{{ class.options | indent(8,true) }} +{% endif %} +{% if class.include is defined and class.include %} + include "{{ class.include }}"; +{% endif %} +} +{% if class.subclass is defined and class.subclass %} + +{% for key, value in class.subclass.iteritems() %} +{% if value is defined and value %} +subclass "{{ class.class }}" "{{ key }}" { +{{ value | indent(8,true) }} +} + +{% else %} +subclass "{{ class.class }}" {{ key }}; +{% endif %} +{% endfor %} +{% endif %} +{% endmacro %} +{# +# +# ---- Macro: print.group() ---- +#} +{% macro group(group) %} +{% if group.comment is defined and group.comment %} +# {{ group.comment }} +{% endif %} +group { +{% if group.options is defined and group.options %} +{{ group.options | indent(8,true) }} +{% endif %} +{% if group.include is defined and group.include %} + include "{{ group.include }}"; +{% endif %} +{% if group.groups is defined and group.groups %} +{% for group in group.groups %} +{{ print.group(group) | indent(8, true) }} +{% endfor %} +{% endif %} +{% if group.subnets is defined and group.subnets %} +{% for subnet in group.subnets %} +{{ print.subnet(subnet) | indent(8, true) }} +{% endfor %} +{% endif %} +{% if group.hosts is defined and group.hosts %} +{{ print.hosts(group.hosts) | indent(8, true) }} +{% endif %} +} +{% endmacro %} +{# +# +# ---- Macro: print.subnet() ---- +#} +{% macro subnet(subnet) %} +{% if subnet.comment is defined and subnet.comment %} +# {{ subnet.comment }} +{% endif %} +{% if dhcpd_ipversion is defined and dhcpd_ipversion == '6' %} +subnet6 {{ subnet.subnet | ipaddr('network') + '/' + subnet.subnet | ipaddr('prefix') | string }} { +{% else %} +subnet {{ subnet.subnet | ipaddr('cidr') | ipaddr('network') }} netmask {{ subnet.netmask | default(subnet.subnet | ipaddr('netmask')) }} { +{% endif %} +{% if subnet.routers is defined and subnet.routers %} +{% if subnet.routers is string %} + option routers {{ subnet.routers }}; +{% else %} + option routers {{ subnet.routers | join(' ') }}; +{% endif %} +{% endif %} +{% if subnet.options is defined and subnet.options %} +{{ subnet.options | indent(8,true) }} +{% endif %} +{% if subnet.include is defined and subnet.include %} + include "{{ subnet.include }}"; +{% endif %} +{% if subnet.pools is defined and subnet.pools %} +{% for pool in subnet.pools %} + pool { +{% if pool.comment is defined and pool.comment %} + # {{ pool.comment }} +{% endif %} +{% if dhcpd_ipversion is defined and dhcpd_ipversion == '6' %} + range6 {{ pool.range }}; +{% else %} + range {{ pool.range }}; +{% endif %} +{% if pool.options is defined and pool.options %} +{{ pool.options | indent(16,true) }} +{% endif %} +{% if pool.include is defined and pool.include %} + include "{{ pool.include }}"; +{% endif %} + } +{% endfor %} +{% endif %} +{% if subnet.hosts is defined and subnet.hosts %} +{{ print.hosts(subnet.hosts) | indent(8, true) }} +{% endif %} +} +{% endmacro %} +{# +# +# ---- Macro: print.hosts() ---- +#} +{% macro hosts(hosts) %} +{% if hosts is string %} +include "{{ hosts }}"; +{% else %} +{% for host in hosts %} +{% if host.comment is defined and host.comment %} +# {{ host.comment }} +{% endif %} +host {{ host.hostname }} { +{% if host.options is defined and host.options %} +{{ host.options | indent(8,true) }} +{% endif %} +{% if host.ethernet is defined and host.ethernet %} + hardware ethernet {{ host.ethernet }}; +{% endif %} +{% if host.address is defined and host.address %} +{% if dhcpd_ipversion is defined and dhcpd_ipversion == '6' %} + fixed-address6 {{ host.address }}; +{% else %} + fixed-address {{ host.address }}; +{% endif %} +{% endif %} +} +{% endfor %} +{% endif %} +{% endmacro %} +{# +# +# ---- Macro: print.failover() ---- +#} +{% macro failover(failover) %} +{% if failover.comment is defined and failover.comment %} +# {{ failover.comment }} +{% endif %} +failover peer "{{ failover.failover }}" { +{% if failover.primary is defined and failover.primary == inventory_hostname %} + primary; + mclt {{ failover.mclt|default(3600) }}; +{% if failover.primary_fo_addr is defined and failover.primary_fo_addr %} + address {{ failover.primary_fo_addr }}; +{% else %} + address {{ failover.primary }}; +{% endif %} +{% if failover.secondary_fo_addr is defined and failover.secondary_fo_addr %} + peer address {{ failover.secondary_fo_addr }}; +{% else %} + peer address {{ failover.secondary }}; +{% endif %} +{% if failover.split is defined and failover.split %} + split {{ failover.split }}; +{% elif failover.hba is defined and failover.hba %} + hba {{ failover.hba }}; +{% endif %} +{% else %} + secondary; +{% if failover.secondary_fo_addr is defined and failover.secondary_fo_addr %} + address {{ failover.secondary_fo_addr }}; +{% else %} + address {{ failover.secondary }}; +{% endif %} +{% if failover.primary_fo_addr is defined and failover.primary_fo_addr %} + peer address {{ failover.primary_fo_addr }}; +{% else %} + peer address {{ failover.primary }}; +{% endif %} +{% endif %} + max-response-delay {{ failover.max_response_delay|default(30) }}; + max-unacked-updates {{ failover.max_unacked_updates|default(10) }}; +{% if failover.load_balance_max_seconds is defined and failover.load_balance_max_seconds %} + load balance max seconds {{ failover.load_balance_max_seconds }}; +{% endif %} +{% if failover.max_lease_misbalance is defined and failover.max_lease_misbalance %} + max-lease-misbalance {{ failover.max_lease_misbalance }}; +{% endif %} +{% if failover.max_lease_ownership is defined and failover.max_lease_ownership %} + max-lease-ownership {{ failover.max_lease_ownership }}; +{% endif %} +{% if failover.min_balance is defined and failover.min_balance %} + min-balance {{ failover.min_balance }}; +{% endif %} +{% if failover.max_balance is defined and failover.max_balance %} + max-balance {{ failover.max_balance }}; +{% endif %} +{% if failover.auto_partner_down is defined and failover.auto_partner_down %} + auto-partner-down {{ failover.auto_partner_down }}; +{% endif %} +} +{% endmacro %} +{# +# +# ---- Macro: print.key() ---- +#} +{% macro key(key) %} +{% if key.comment is defined and key.comment %} +# {{ key.comment }} +{% endif %} +key "{{ key.key }}" { + algorithm {{ key.algorithm|default('hmac-md5') }}; + secret {{ key.secret }}; +} +{% endmacro %}