Skip to content

Commit

Permalink
dnsmasq - use dhcp-hostdir
Browse files Browse the repository at this point in the history
The restart done by the handler happens only at the very
end of the play. By using dhcp-hostsdir we get the benefit
of dnsmasq reading any new or changed file in the hosts dir,
automatically applying the new dhcp host config on the fly.

Since adding a host is loaded on-the-fly - we don't need to
notify the handler when adding hosts.

Removing hosts however, reqires s SIGHUP - so for that case
notify the handler.

Also change the computation of _host_entry - it should include
both ipv4 and ipv6 address if both are configured.
  • Loading branch information
hjensas committed May 31, 2024
1 parent 81c904d commit 76d93e6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion roles/dnsmasq/tasks/configure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
become: true
ansible.builtin.file:
mode: "0755"
path: "{{ cifmw_dnsmasq_basedir }}"
path: "{{ cifmw_dnsmasq_basedir }}/dhcp-hosts.d"
state: "{{ (_act == 'cleanup') | ternary('absent', 'directory') }}"

- name: Manage systemd unit file
Expand Down
38 changes: 21 additions & 17 deletions roles/dnsmasq/tasks/manage_host.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,38 @@
- name: Compute entry
ansible.builtin.set_fact:
_host_entry: |
{% if cifmw_dnsmasq_host_ipv4 is defined and cifmw_dnsmasq_host_ipv4 | length > 0 -%}
_host_entry: >
{% set data = [cifmw_dnsmasq_host_mac] -%}
{% if cifmw_dnsmasq_host_ipv4 is defined and cifmw_dnsmasq_host_ipv4 | length > 0 -%}
{% set _ = data.append(cifmw_dnsmasq_host_ipv4) -%}
{% set _ = data.append(cifmw_dnsmasq_host_name) if
cifmw_dnsmasq_host_name is defined and cifmw_dnsmasq_host_name | length > 0 -%}
dhcp-host={{ data | join(',') }}
{% endif -%}
{% if cifmw_dnsmasq_host_ipv6 is defined and cifmw_dnsmasq_host_ipv6 | length > 0 -%}
{% set data = [cifmw_dnsmasq_host_mac] -%}
{% set _ = data.append('['~cifmw_dnsmasq_host_ipv6~']') -%}
{% endif -%}
{% set _ = data.append(cifmw_dnsmasq_host_name) if
cifmw_dnsmasq_host_name is defined and cifmw_dnsmasq_host_name | length > 0 -%}
dhcp-host={{ data | join(',') }}
{% endif -%}
{{ data | join(',') }}
- name: Debug _host_entry
ansible.builtin.debug:
var: _host_entry

- name: Manage host entry
- name: Manage host entry - add
become: true
when:
- cifmw_dnsmasq_host_state == 'present'
ansible.builtin.copy:
content: "{{ _host_entry }}"
dest: "{{ cifmw_dnsmasq_basedir }}/dhcp-hosts.d/{{ cifmw_dnsmasq_host_mac }}"
mode: '0644'
owner: root
group: root

- name: Manage host entry - remove
become: true
when:
- cifmw_dnsmasq_host_state == 'absent'
notify: Restart dnsmasq
ansible.builtin.blockinfile:
block: "{{ _host_entry }}"
dest: "{{ cifmw_dnsmasq_basedir }}/{{ cifmw_dnsmasq_host_network }}-hosts.conf"
create: true
mode: "0644"
state: "{{ cifmw_dnsmasq_host_state }}"
marker: "## {mark} {{ cifmw_dnsmasq_host_mac }}"
validate: "/usr/sbin/dnsmasq -C %s --test"
ansible.builtin.file:
state: absent
path: "{{ cifmw_dnsmasq_basedir }}/dhcp-hosts.d/{{ cifmw_dnsmasq_host_mac }}"
3 changes: 2 additions & 1 deletion roles/dnsmasq/templates/cifmw-dnsmasq.conf.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Managed by ci-framework/dnsmasq

log-debug
user=dnsmasq
group=dnsmasq
pid-file=/var/run/cifmw-dnsmasq.pid
Expand All @@ -10,3 +10,4 @@ dhcp-leasefile=/var/lib/dnsmasq/cifmw-dnsmasq.leases
{% endif %}

conf-dir={{ cifmw_dnsmasq_basedir }},*.conf
dhcp-hostsdir="{{ cifmw_dnsmasq_basedir }}/dhcp-hosts.d"

0 comments on commit 76d93e6

Please sign in to comment.