From 9b0ad40354d592ec364e6d1e93a338720ade1437 Mon Sep 17 00:00:00 2001 From: Maciej Delmanowski Date: Fri, 27 Mar 2015 14:32:44 +0100 Subject: [PATCH] Add support for ISC DHCP Relay You can enable installation of DHCP relay instead of the server via 'dhcpd_mode' variable. By default, relays send the packets to their network gateway, but that might not be what you really want; however selection of correct DHCP server can be performed by specifying it in 'dhcpd_relay_servers' list. --- defaults/main.yml | 23 +++++++++++++++++++++++ handlers/main.yml | 5 +++++ tasks/main.yml | 21 ++++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/defaults/main.yml b/defaults/main.yml index c9aaffd..60c9e72 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,5 +1,28 @@ --- +# What service type to configure on this host: +# 'server' - host is an ISC DHCP server, see dhcpd(8) +# 'relay' - host is an ISC DHCP relay, see dhcrelay(8) +dhcpd_mode: 'relay' + +# What packages should be installed, depending on mode of operation +dhcpd_base_packages_map: + 'server': [ 'isc-dhcp-server' ] + 'relay': [ 'isc-dhcp-relay' ] + + +# ---- ISC DHCP Relay configuration ---- + +# List of DHCP servers which should receive the relayed packets +dhcpd_relay_servers: [ '{{ ansible_default_ipv4.gateway }}' ] + +# List of network interfaces that dhcrelay should listen on +dhcpd_relay_interfaces: [] + +# Additional dhcrelay options +dhcpd_relay_options: '-4' + + # ---- Global ISC DHCP Server configuration ---- # Is this DHCP server authoritative? diff --git a/handlers/main.yml b/handlers/main.yml index 7a85b53..a021695 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -5,3 +5,8 @@ name: 'isc-dhcp-server' state: 'restarted' +- name: Restart isc-dhcp-relay + service: + name: 'isc-dhcp-relay' + state: 'restarted' + diff --git a/tasks/main.yml b/tasks/main.yml index b18e5c9..b23321e 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,11 +1,29 @@ --- +- name: Configure DHCP relay in debconf + debconf: + name: 'isc-dhcp-relay' + question: 'isc-dhcp-relay/{{ item.key }}' + vtype: 'string' + value: '{{ item.value }}' + with_dict: + servers: '{{ dhcpd_relay_servers | join(" ") }}' + interfaces: '{{ dhcpd_relay_interfaces | join(" ") }}' + options: '{{ dhcpd_relay_options }}' + register: dhcpd_register_relay_debconf + when: dhcpd_mode == 'relay' + - name: Install DHCP packages apt: name: '{{ item }}' state: 'present' install_recommends: False - with_items: [ 'isc-dhcp-server' ] + with_items: dhcpd_base_packages_map[dhcpd_mode] + +- name: Reconfigure ISC DHCP relay + command: dpkg-reconfigure --frontend=noninteractive isc-dhcp-relay + notify: [ 'Restart isc-dhcp-relay' ] + when: dhcpd_register_relay_debconf|d() and dhcpd_register_relay_debconf.changed - name: Configure DHCP server template: @@ -16,4 +34,5 @@ mode: '0644' with_items: [ 'etc/default/isc-dhcp-server', 'etc/dhcp/dhcpd.conf' ] notify: [ 'Restart isc-dhcp-server' ] + when: dhcpd_mode == 'server'