Skip to content

Commit

Permalink
Move global options before subnets configuration
Browse files Browse the repository at this point in the history
Some option can only be configured globally but have to be defined before they can be used as option for a subnet, e.g. `option ztp_provisioning_script_url code`
  • Loading branch information
robertvolkmann committed Nov 20, 2023
1 parent 2530fdd commit 101ba35
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 47 deletions.
16 changes: 8 additions & 8 deletions partition/roles/dhcp/templates/dhcpd.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ max-lease-time {{ dhcp_max_lease_time }};

log-facility local7;

{% for option in dhcp_global_options %}
option {{ option }};
{% endfor %}
{% for deny in dhcp_global_deny_list %}
deny {{ deny }};
{% endfor -%}

{% for subnet in dhcp_subnets %}
{% if subnet.comment | default("") %}
# {{ subnet.comment }}
Expand All @@ -21,14 +28,7 @@ subnet {{ subnet.network }} netmask {{ subnet.netmask }} {
deny {{ deny }};
{% endfor %}
}
{% endfor %}

{% for option in dhcp_global_options %}
option {{ option }};
{% endfor %}
{% for deny in dhcp_global_deny_list %}
deny {{ deny }};
{% endfor %}
{% endfor -%}

{% if dhcp_static_hosts is defined %}
include "/etc/dhcp/dhcpd.hosts";
Expand Down
6 changes: 0 additions & 6 deletions partition/roles/dhcp/test/__init__.py

This file was deleted.

73 changes: 40 additions & 33 deletions partition/roles/dhcp/test/template_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import os
import unittest

from test import read_template_file
from textwrap import dedent

from ansible.template import Templar


def read_template_file(name):
with open(os.path.join(os.path.dirname(__file__), "..", "templates", name), 'r') as f:
return f.read()


class DHCPD(unittest.TestCase):
def test_dhcpd_config_template(self):
t = read_template_file("dhcpd.conf.j2")
Expand All @@ -28,24 +33,25 @@ def test_dhcpd_config_template(self):
hostvars=dict(mgmt01=dict(switch_mgmt_ip="3.3.3.3"), mgmt02=dict(switch_mgmt_ip="4.4.4.4")),
))

res = templar.template(t)

self.assertIn("""
authoritative;
result = templar.template(t)

default-lease-time 600;
max-lease-time 600;
log-facility local7;
# testing
subnet 1.2.3.4 netmask 24 {
range 1 2;
option routers 2.2.2.2;
option domain-name-servers 1.1.1.1, 8.8.8.8;
deny unknown-clients;
}
""".strip(), res.strip())
self.assertEqual(dedent("""\
# indicate that the DHCP server should send DHCPNAK messages to misconfigured client
authoritative;
default-lease-time 600;
max-lease-time 600;
log-facility local7;
# testing
subnet 1.2.3.4 netmask 24 {
range 1 2;
option routers 2.2.2.2;
option domain-name-servers 1.1.1.1, 8.8.8.8;
deny unknown-clients;
}
"""), result)

def test_dhcpd_hosts_config_template(self):
t = read_template_file("dhcpd.hosts.j2")
Expand All @@ -67,18 +73,19 @@ def test_dhcpd_hosts_config_template(self):
],
))

res = templar.template(t)

self.assertEqual("""
host test1 {
hardware ethernet aa:bb:cc:dd:ee:ef;
fixed-address 10.1.2.1;
option test1;
}
result = templar.template(t)

host test2 {
hardware ethernet aa:bb:cc:dd:ee:ff;
fixed-address 10.1.2.2;
option test2;
}
""".strip(), res.strip())
self.assertEqual(dedent("""\
host test1 {
hardware ethernet aa:bb:cc:dd:ee:ef;
fixed-address 10.1.2.1;
option test1;
}
host test2 {
hardware ethernet aa:bb:cc:dd:ee:ff;
fixed-address 10.1.2.2;
option test2;
}
"""), result)

0 comments on commit 101ba35

Please sign in to comment.