From adf511a35f39e7dfa8a0ed81fbcb8e0236bbb0b6 Mon Sep 17 00:00:00 2001 From: jdreichmann Date: Sun, 7 Jun 2020 20:13:46 +0200 Subject: [PATCH] dhcp: update role to template subnet configuration correctly --- dhcp_server.yml | 8 ++++++++ roles/dhcp/tasks/main.yml | 1 + roles/dhcp/templates/dhcpd.conf.j2 | 26 ++++++++++++++------------ 3 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 dhcp_server.yml diff --git a/dhcp_server.yml b/dhcp_server.yml new file mode 100644 index 0000000..5e91630 --- /dev/null +++ b/dhcp_server.yml @@ -0,0 +1,8 @@ +--- + +- name: "Setup ISC-DHCP for the internal network" + hosts: dhcp_server + become: true + roles: + - dhcp + diff --git a/roles/dhcp/tasks/main.yml b/roles/dhcp/tasks/main.yml index e51a97e..ed20e76 100644 --- a/roles/dhcp/tasks/main.yml +++ b/roles/dhcp/tasks/main.yml @@ -13,6 +13,7 @@ group: root mode: 0644 setype: dhcp_etc_t + validate: dhcpd -t - name: Restart & enable DHCP-server systemd: diff --git a/roles/dhcp/templates/dhcpd.conf.j2 b/roles/dhcp/templates/dhcpd.conf.j2 index 5d5c074..9b8eb66 100644 --- a/roles/dhcp/templates/dhcpd.conf.j2 +++ b/roles/dhcp/templates/dhcpd.conf.j2 @@ -21,34 +21,36 @@ option domain-name-servers {{ dhcp_common_name_servers|join(', ') }}; log-facility local7; # Subnet definitions + {% for subnet in dhcp_subnets %} subnet {{ subnet.net }} netmask {{ subnet.netmask }} { option routers {{ subnet.routers }}; option broadcast-address {{ subnet.broadcast }}; option domain-name "{{ subnet.domain_name }}"; option domain-name-servers {{ subnet.domain_name_servers|join(', ') }}; - {% if subnet.domain_search is defined and subnet.domain_search|length > 0 %} - option domain-search {{ subnet.domain_search | map('regex_replace', '(.*)', '\"\\1\"') | join(', ') }}; - {% endif %} +{% if subnet.domain_search is defined and subnet.domain_search|length > 0 %} + option domain-search {{ subnet.domain_search | map('regex_replace', '^(.*)$', '\"\\1\"') | join(', ') }}; +{% endif %} - {% if subnet.range_start is defined and subnet.range_end is defined %} +{% if subnet.range_start is defined and subnet.range_end is defined %} range {{ subnet.range_start }} {{ subnet.range_end }}; - {% endif %} +{% endif %} - {% if subnet.allow_unknown is defined and subnet.allow_unknown is sameas false %} +{% if subnet.allow_unknown is defined and subnet.allow_unknown is sameas false %} deny unknown-clients; - {% else %} +{% else %} allow unknown-clients; - {% endif %} +{% endif %} - {% for host in subnet.hosts %} +{% for host in subnet.hosts %} host {{ host.name }} { hardware ethernet {{ host.mac }}; - {% if host.ip is defined %} +{% if host.ip is defined %} fixed-address {{ host.ip }}; - {% endif %} +{% endif %} } - {% endfor %} +{% endfor %} } + {% endfor %}