This installs DNSmasq and drops an initial config that disables the DNS features and loads all other config from the /etc/dnsmasq.d directory. It also creates a Systemd job that replicates the actual DNSmasq config from another another host using rsync. The DHCP and rsync config is not part of this role but is/will be documented separately. It's quite cunning even if I do say so myself.

Manual Process

Install dnsmasq and then copy the config files from an existing Samba 4 DC to enable the DHCP server functionality. Then remember to keep the reservations up to date on all notes.

Then realise you can use rsync, which you've already installed to handle Samba SYSVOL shares to also keep the DNSmasq config up to date so configure a Systemd service and timer unit to trigger a regular rsync replication.

Ansible Role: dnsmasq-samba

This was created for Building an additional Samba 4 Domain Controller 2 and I'm still refining the DNSmasq config and suspect I need to add some exclusions to the replication which will affect the rsync-dnsmasq.service file.

Only a minimal config is deployed by this role, most of the DNSmasq config is replicated from the primary server using an rsync share. The detail config for DNSmasq DHCP will be in a future article.

Dependencies

  • This also needs the gitdeploy role to be available to deploy a script for DHCP updates into Samba DNS
  • The rsync replication for most of the config is currently set up by the rsync-samba role so that needs to have run first. I'm considering splitting that role up in which case it'll be one of its children that needs to run first. Not sure if I can put that into meta/main.yaml to make that automatic without having to subscribe to ansible-galaxy.

File Structure

This is a standard Ansible role with as single tasks .yaml file, three config files, a meta to keep Ansible happy and a README.md for my own notes.

gitdeploy  

  meta
    main.yaml

  tasks
    main.yaml

  files    dnsmasq.conf
    rsync-dnsmasq.service
    rsync-dnsmasq.timer

  handlers
    main.yaml

  README.md

tasks/main.yaml

--- # Ansible Role Tasks

# Role for deploying DNSmasq DHCP for use with Samba AD

- name: Install DNSmasq
  become: true
  ansible.builtin.apt:
    package: dnsmasq

- name: Save config file
  become: true
  ansible.builtin.command:
    cmd: mv /etc/dnsmasq.conf /etc/dnsmasq.original
    creates: /etc/dnsmasq.original

- name: Drop DNSmasq configuration
  become: true
  ansible.builtin.copy:
    src: dnsmasq.conf
    dest: /etc/dnsmasq.conf
    owner: root
    group: root
    mode: '0640'
    backup: true

- name: Disable DNSmasq until Samba ready
  become: true
  ansible.builtin.systemd:
    name: dnsmasq.service
    state: stopped
    enabled: false

This first part of the role just installs DNSmasq from the standard repos, moves the original dnsmasq.conf file to dnsmasq.original, providing dnsmasq.original doesn't already exist,  in case you want to revert back and then drops our own config in there.

- name: Drop DNSmasq config replication service
  become: true
  ansible.builtin.copy:
    src: rsync-dnsmasq.service
    dest: /etc/systemd/system/rsync-dnsmasq.service
    owner: root
    group: root
    mode: '0644'
    backup: true
  notify: Systemd rsync-dnsmasq-service enable

- name: Drop DNSmasq config replication timer
  become: true
  ansible.builtin.copy:
    src: rsync-dnsmasq.timer
    dest: /etc/systemd/system/rsync-dnsmasq.timer
    owner: root
    group: root
    mode: '0644'
    backup: true
  notify: Systemd rsync-dnsmasq-timer enable

# Configuration is replicated from the primary server by RSync jobs which are also
# replicated from the primary.

The rsync-dnsmasq.service will execute an rsync replication at intervals defined in rsync-dnsmasq.timer. These are not actviated immediately as it's left to a hander which will trigger this at the end of the playbook.

- name: Deploy the DNS update scripts
  ansible.builtin.include_role:
  name: gitdeploy
    vars:
    git_repo: mygit.example.com:/srv/git/scripts/dnsmasq.git
    git_version: master
    destination: /opt/bostoncube/dnsmasq

I also need to deploy the scripts referred to in the DNSmasq config to handle the update of Samba DNS whenever DNSmasq issues a DHCP lease.

...

I might add some config for logrotate in future once I've seen this configuration running long enough to see some log growth.

 handlers/main.yaml

These handlers just start the config replication service at the end of the playbook

--- # Ansible Role Handler

# Handlers for deploying RSync

- name: Systemd rsync-dnsmasq-service enable
  become: true
  ansible.builtin.systemd:
    name: rsync-dnsmasq.service
    state: restarted

    enabled: true
    daemon_reload: true

- name: Systemd rsync-dnsmasq-timer enable
  become: true
  ansible.builtin.systemd:
   
name: rsync-dnsmasq.timer
    state: restarted

    enabled: true
    daemon_reload: true

files/rsync-dnsmasq.service

The samba-primary.env file must exist before this service can run. This should have already been replicated by the rsync-core jobs configured in rsync-samba-core

[Unit]
Description=DNSmaq DHCP config replication

[Service]
Type=oneshot
EnvironmentFile=/etc/rsync.d/samba-primary.env

ExecStart=rsync -av --delete-after --password-file=/etc/rsync.d/dnsmasq-dhcp.cred --exclude dhcp-delay.conf rsync://dnsmasq-replication@${SAMBA_PRIMARY}:/DNSmasq-DHCP /etc/dnsmasq.d

[Install]
WantedBy=multi-user.target

Update Apr '25: This now ignores the dhcp-delay.conf file which should not exist on the source but may exist on locally. If it does then we do not want it removed. the dhcp-delay.conf make the local DNSmasq DHCP service wait before responding to requests to that another DHCP server should be preferred by clients but leaving the local available to process requests if needed so I don't end up with annoyed users if something has broken on the primary.

files/rsync-dnsmasq.timer

This should run 5 minutes after boot and then every 15 minutes after that.

[Unit]
Description=DNSmaq DHCP config replication timer

[Timer]
OnBootSec=5m
OnUnitInactiveSec=15m
Persistent=true

[Install]
WantedBy=timers.target

files/dnsmasq.conf

Since we create a copy of the original config in dnsmasq.original we only need a seriously cut down version for running. The changes are:

  1. port=0 to disable DNS functions since Samba has to do that
  2. logging set to /var/log/dnsmasq.log
  3. All other config will be loaded from .conf files in  /etc/dnsmasq.d

# Configuration file for dnsmasq.
#
# Format is one option per line, legal options are the same
# as the long options legal on the command line. See
# "/usr/sbin/dnsmasq --help" or "man 8 dnsmasq" for details.

# Listen on this specific port instead of the standard DNS port
# (53). Setting this to zero completely disables DNS function,
# leaving only DHCP and/or TFTP.
#port=5353
port=0

# For debugging purposes, log each DNS query as it passes through
# dnsmasq.
#log-queries
log-async
log-facility=/var/log/dnsmasq.log

# Log lots of extra information about DHCP transactions.
#log-dhcp

# Include all the files in a directory except those ending in .bak
#conf-dir=/etc/dnsmasq.d,.bak

# Include all files in a directory which end in .conf
conf-dir=/etc/dnsmasq.d/,*.conf