diff --git a/defaults/main.yml b/defaults/main.yml index f0b7171..c942695 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -76,6 +76,12 @@ dhcpd_server_options: '{{ "-" + dhcpd_ipversion }}' dhcpd_authoritative: False +# .. envvar:: dhcpd_log_facility +# +# Log facility to use +dhcpd_log_facility: 'local7' + + # .. envvar:: dhcpd_interfaces # # List of network interfaces to listen on for DHCP requests @@ -84,40 +90,58 @@ dhcpd_authoritative: False dhcpd_interfaces: [] -# .. envvar:: dhcpd_domain -# -# Default domain to use -dhcpd_domain: '{{ ansible_domain }}' - - -# .. envvar:: dhcpd_dns_servers -# -# List of default DNS servers. By default, point users to the same host that -# serves them DHCP requests, on default interface. If this host is a router, -# you might need to set DNS server to internal interface IP address. -dhcpd_dns_servers: [ '{{ ansible_default_ipv4.address }}' ] - - # .. envvar:: dhcpd_lease_time # # Max lease time in hours (default lease time is calculated below) -dhcpd_lease_time: 24 +dhcpd_lease_time: '24' -# .. envvar:: dhcpd_global_options +# .. envvar:: dhcpd_global_default_lease_time # -# Default global options formatted as a text block -dhcpd_global_options: | - option domain-name "{{ ansible_domain }}"; - option domain-name-servers {{ dhcpd_dns_servers | join(' ') }}; - default-lease-time {{ (((dhcpd_lease_time / 2) + 6) * 60 * 60)|round|int }}; - max-lease-time {{ (dhcpd_lease_time * 60 * 60)|round|int }}; - log-facility local7; +# Default lease time for all IP address leases (18 hours) +dhcpd_global_default_lease_time: '{{ (((dhcpd_lease_time|int / 2) + 6) * 60 * 60)|round|int }}' + + +# .. envvar:: dhcpd_global_max_lease_time +# +# Maximum lease time for all IP addresses (24 hours) +dhcpd_global_max_lease_time: '{{ (dhcpd_lease_time|int * 60 * 60)|round|int }}' + + +# --------------------------- +# DHCP advertised options +# --------------------------- + +# .. envvar:: dhcpd_auto_options +# +# If enabled, ISC DHCP server will be configured with a set of automatically +# detected options. See ``auto_options.j2`` template for more details. +dhcpd_auto_options: True + + +# .. envvar:: dhcpd_domain_name +# +# Default host domain to advertise +dhcpd_domain_name: '{{ ansible_domain }}' + + +# .. envvar:: dhcpd_domain_search +# +# List of additional domains which should be checked when looking for hostnames +dhcpd_domain_search: [] + + +# .. envvar:: dhcpd_nameservers +# +# List of nameservers to advertise by default +# If it's not specified, nameservers from ``/etc/resolv.conf`` will be used +# instead. +dhcpd_nameservers: [] # .. envvar:: dhcpd_options # -# Custom options formatted as a text block +# Custom global options formatted as a text block dhcpd_options: False diff --git a/tasks/main.yml b/tasks/main.yml index 1758046..26d80d7 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -25,6 +25,17 @@ notify: [ 'Restart isc-dhcp-relay' ] when: dhcpd_register_relay_debconf|d() and dhcpd_register_relay_debconf.changed +- name: Get list of nameservers configured in /etc/resolv.conf + shell: grep -E '^nameserver\s' /etc/resolv.conf | awk '{print $2}' | sed -e 'N;s/\n/ /' + register: dhcpd_register_nameservers + changed_when: False + when: dhcpd_mode == 'server' + +- name: Convert list of nameservers to Ansible list + set_fact: + dhcpd_runtime_nameservers: "{{ dhcpd_register_nameservers.stdout.split(' ') }}" + when: (dhcpd_register_nameservers is defined and dhcpd_register_nameservers.stdout) + - name: Configure DHCP server template: src: '{{ item }}.j2' diff --git a/templates/etc/dhcp/auto_options.j2 b/templates/etc/dhcp/auto_options.j2 new file mode 100644 index 0000000..63ef653 --- /dev/null +++ b/templates/etc/dhcp/auto_options.j2 @@ -0,0 +1,26 @@ +{% if dhcpd_domain_name|d() and dhcpd_domain_name %} +option domain-name "{{ dhcpd_domain_name }}"; + +{% endif %} +{% if dhcpd_domain_search|d() and dhcpd_domain_search %} +option domain-search "{{ dhcpd_domain_search | join('", "') }}"; +option dhcp6.domain-search "{{ dhcpd_domain_search | join('", "') }}"; + +{% endif %} +{% if dhcpd_nameservers|d() and dhcpd_nameservers %} +{% set dhcpd_tpl_nameservers = dhcpd_nameservers %} +{% elif dhcpd_runtime_nameservers|d() and dhcpd_runtime_nameservers %} +{% set dhcpd_tpl_nameservers = dhcpd_runtime_nameservers %} +{% endif %} +{% if dhcpd_tpl_nameservers %} +{% if dhcpd_tpl_nameservers | ipv4 %} +option domain-name-servers {{ dhcpd_tpl_nameservers | ipv4 | join(", ") }}; +{% endif %} +{% if dhcpd_tpl_nameservers | ipv6 %} +option dhcp6.name-servers {{ dhcpd_tpl_nameservers | ipv6 | join(", ") }}; +{% endif %} + +{% endif %} +{# + vim: ft=dhcpd +#} diff --git a/templates/etc/dhcp/dhcpd.conf.j2 b/templates/etc/dhcp/dhcpd.conf.j2 index e619b1b..17b1902 100644 --- a/templates/etc/dhcp/dhcpd.conf.j2 +++ b/templates/etc/dhcp/dhcpd.conf.j2 @@ -8,10 +8,19 @@ authoritative; not authoritative; {% endif %} -{% if dhcpd_global_options is defined and dhcpd_global_options %} -# Global configuration options -{{ dhcpd_global_options }} +{% if dhcpd_global_default_lease_time|d() and dhcpd_global_default_lease_time %} +default-lease-time {{ dhcpd_global_default_lease_time }}; +{% endif %} +{% if dhcpd_global_max_lease_time|d() and dhcpd_global_max_lease_time %} +max-lease-time {{ dhcpd_global_max_lease_time }}; +{% endif %} +{% if dhcpd_log_facility|d() and dhcpd_log_facility %} +log-facility {{ dhcpd_log_facility }}; + +{% endif %} +{% if dhcpd_auto_options|d() and dhcpd_auto_options %} +{% include 'auto_options.j2' %} {% endif %} {% if dhcpd_options is defined and dhcpd_options %} # Configuration options