With the playbooks and roles prepared we're now ready to deploy our new Samba DCs and then move the FSMO role across and retire the old ones. A little bit of preparation on one of the existing DCs will also make for a smoother transition.
Prepare the Primary DC
While Samba will take care of AD replication we need our own solution to replicate other configuration settings:
- Sysvol files - login scripts and GPO policies
- DNSmasq DHCP config and reservations
- Replication config itself
I'm currently using rsync for this task as it's a well known tool related to Samba anyway. I also plan to test out Syncthing at some point now that it can (allegedly) handle synchronisation of users, groups and permissions but that's a future project.
This automation deploys an additional DC into an existing Active Directory domain and for simplicity it also expects everything else to be properly configured which means we need to manually prepare an existing DC so it will play nicely. This is a one-off task and any future deployments should then just work. I suppose I could create a specific playbook to do this but it doesn't take long to do by hand.
Install Rsync and configure Primary DC
Rsync is included in the Ubuntu repositories and can be installed with
sudo apt install rsync
We will use a simple configuration file to set defaults to read only on the primary and to load all config from /etc/rsync.d. Edit the /etc/rsync.conf file as below.
# Rsync Daemon Configuration # 6 Feb '25 # Global parameters # Module Defaults uid = root gid = root read only = yes # All modules &include /etc/rsync.d
Not only are we going to keep the rsync configuration in /etc/rsync.d, this will also be replicated by the other Samba Domain Controllers (DC's) so they can take over hosting the rsync config if needed. Create the /etc/rsync.d directory and set permissions 750 root:root since we do not want this config to be visible to any other users.
Each replication will need its own set of three files - the rsync module definition, a credential file with username(s) and password(s) for who can access and a separate file with just the password. These will be copied to all other DCs so they will all be sharing the same config and credentials. As these are all DCs they're trusted machines anyway and while maintaining per-host credentials is possible that's more admin. If I was being super paranoid I could also limit access via rsync alllow/deny or firewall rules since the hosts have static IP addresses and also enable ssh-encrypted replication but most of the files being replicated are not sensitive - they're mostly world-readable on either the host or via SMB shares anyway - and it's all on an internal network too. If I had unencrypted WAN links then I'd definitely want something to protect the rsync-core replication since that handles the rsync credentials.
Replication module rsync-core
This replication module keeps the replication config in /etc/rsync.d the same on all the additional DCs.
| Module Definition | Users | Password |
|---|---|---|
| Filename: rsync-core.conf | Filename: rsync-core.users | Filename: rsync-core.cred |
[RSync-Core] |
rsync-core:passphrase |
passphrase |
Replication module SysVol
This replication module ensure any updates to the Samba sysvol are also replicated to additional DCs.
| Module Definition | Users | Password |
|---|---|---|
| Filename: samba-sysvol.conf | Filename: samba-sysvol.users | Filename: samba-sysvol.cred |
[SysVol] path = /var/lib/samba/sysvol comment = Samba Sysvol Share uid = root gid = root read only = yes auth users = sysvol-replication # secrets file = /var/lib/samba/private/rsync-sysvol.users secrets file = /etc/rsync.d/samba-sysvol.users |
sysvol-replication:passphrase |
passphrase |
Replication module DNSmasq-DHCP
This replication module keeps the DHCP config the same on each of the DCs, mainly for any DHCP reservations.
| Module Definition | Users | Password |
|---|---|---|
| Filename: dnsmasq-dhcp.conf | Filename: dnsmasq-dhcp.users | Filename: dnsmasq-dhcp.cred |
[DNSmasq-DHCP] path = /etc/dnsmasq.d comment = DNSmasq DHCP configuration auth users = dnsmasq-replication secrets file = /etc/rsync.d/dnsmasq-dhcp.users |
dnsmasq-replication:passphrase |
passphrase |
Before we enable the replication service on the additional DCs they need to know where to replicate from. Since this could change I don't want to hard-code that in the systemd service units that run each replication cycle as that's not easy to change. Instead we use an environment variable which is read from /etc/rsync.d/samba-primary.env to set the hostname of the PDC and then reference this in the system units. While the PDC doesn't need this setting itself, we need to create the file on the PDC so it can be replicated out to the additional DCs that do need it. For example:
SAMBA_PRIMARY=s4dc-1
As we can see in the cutover process below, for planned downtime on the Primary DC we only need to update this file with the name of the new Primary and let it replicate out without having to manually update the other DCs. If you've only got two DC then it's not much of an saving.
As we also have a DHCP server on each Samba host we really don't want them both responding to all requests either. While one option is to keep one alive and shut the other down that's not exactly resilient. A better solution is to use a DNSmasq config option to introduce a delay into responses from additional servers, but DNSmasq doesn't support conditional config. What we can do instead is create a template text file on the Primary with the required config which is replicated to the other server where it can then be activated as needed. The file should be /etc/dnsmasq.d/dhcp-delay.txt and contain
# Delay DHCP responses.
# This file should only be active on 2nd server (it's excluded from rsync in its active form)
dhcp-reply-delay=2
On the secondary hosts rename it to dhcp-delay.conf and restart DNSmasq. The systemd service deployed by the dnsmasq-samba role should be configured so that it excludes dhcp-delay.conf from replication, or more accurately, it excludes it from being deleted by replication on the basis of it doesn't exist on the primary.
Update Ansible Inventory
With the PDC prepared the next step is to update the Ansible Inventory with the details of the new additional DC if you haven't completed that step already. Using our inventory schema this is just a case of adding the new host to the samba_tier group along with the template to use and the static IP address to be assigned.
...
children:
# Samba DC must be members of this group
sambadc_test:
vars:
network_type: static
ad_domain: test.example.com
ad_domain_primary: s4dc-1.test.example.com
hosts:
s4dc-1.test.example.com:
s4dc-2.test.example.com:
esx_ovf_source: U2404-base-201/X_U2404-base-2.ovf
network_address4: 192.168.451.65
Obviously you'll have your own domain name here and a valid IP address...
Deploy the New DC
On the Deployer go to the playbook directory e.g. cd ~/automation/ansible/playbooks/samba-dc or wherever you chose back in Samba 4 Domain Controller - part 1.The final step before we run the Deployer is to use the refresh-roles.sh script just to make sure the roles are up to date and then we can run our deployment
./refresh-roles.sh
./sambadc.sh s4dc-2.test.example.com
This will take several minutes to run, but at the end of it we should have a brand new Samba 4 domain controller joined to our domain and ready to start.
Activation
Although the playbook installs and configures the additional DC and joins it to the domain, it does not perform the final activation. This is partly down to me being overly cautious and wanting to be sure before anything touches the domain.but also on a more practical note is we should be using signed TLS certificates for Samba so that LDAPS clients can connect properly and my certificate signing process is still manual with the signing keys kept offline.
- A private key and Certificate Signing Request are created by the host-tls role while running the ubunt24-20.yaml playbook. This signed certificate needs to be uploaded to the host and then configured /etc/samba/smb.conf and TLS activated. This will let other applications use Samba AD as an LDAPS server for authenticating users.
- Update the /opt/bostoncube/dnsmasq/script/sambareg.conf file deployed by dnsmasq-samba with the IP address and domain name of a DNS host to be updated whenever a DHCP address is leased - typically the same one it's installed on.
- Enable and verify the rsync-core replication is working and /etc/rsync.d the same as the PDC
- Enable and verify the DNSmasq-dhcp replication is working and /etc/dnsmasq.d is the same as the PDC
- Enable the DHCP response delay by copying the dhcp-delay.txt file to dhcp-delay.conf in the /etc/dnsmasq.d directory on the New DC only
- Enable and verify the rsync-sysvol replication is working and files have been copied across.
- Start the samba-ad-dc service and check it's properly connected - Samba Wiki can help here
- Enable and start DNSmasq and confirm it's OK
- Add new host IP to DNS servers in DHCP config on the Primary - let the config replicate and then restart DNSmasq just to be sure
Backout
As these playbooks create a new Ubuntu server and join it to the domain as a domain controller, if something goes wrong we need to be careful with fixing.
Up until the the playbook sambadc-10.yaml runs the samba-dc role a failed VM can simply be deleted and the deployment run again. As long as the samba-dc role hasn't yet attempted samba-tool domain join command it's still safe to delete the VM and try again.
If the problem occurs during or after the attempt to execute samba-tool domain join - and the playbook should pause if that fails so you can attempt a fix - you need to login to the DC and either fix the issue and successfully run the domain join or run a samba-tool domain demote and samba-tool domain leave before trying again. Otherwise the domain will be left in an unstable state and you'll need to go through the process to manually remove all traces of a dead DC before trying again.
Thankfully since the playbooks were finished I've not had any issues with the join but it can be messy trying cleaning up - mostly I found it was a case of fixing per-requisites and then it worked. I also found that even if the join has failed it might not allow a demote or remote untill you've attempted to start the samba-ad-dc service, after which it is able to clean up its own mess.
Cut-over of Primary Roles
At this point you should now have a fully configured and activated additional DC. If this is being deployed purely as an additional then make sure the current Primary is prepared to become a secondary (e.g. it has the rsync service and timer units defined and disabled). I used this process to build the new DCs as part of a migration from Ubuntu 20.04 to 24.04 so I also needed to make one of the two new DCs the new primary before retiring the old.
You can also use the same process for planned maintenance where you want to take the current Primary out of use for a while
Planned Switchover
It's always good to have a plan for any sort of switchover, especially into a production environment. Whether you stick to it on the day or decide to wing it use your technical skills an years of experience to optimise the process is another question.
To avoid confusion I will refer to the current Primary DC as OldDC and the new additional DC as NewDC.
- For any applications using the OldDC for LDAP/S - including any proxies - add the NewDC and either remove the OldDC or mark it as offline depending on what the application/proxy supports
- On OldDC remove the IP of OldDC from DNSmasq DHCP server options dns-server and ntp-server. This will be replicated to any other DCs via rsync.
- On NewDC enable and start the rsync service ready for other DCs to connect to it.
- On NewDC delete the /etc/dnsmasq.d/dhcp-delay.conf file and restart DNSmasq so it now responds immediately
- On OldDC either copy /etc/dnsmasq.d/dhcp-delay.text to dhcp-delay.conf and restart DNSmasq or stop and disable DNSmasq so it no longer provides a DHCP service
- [ Change freeze start ] - no more config changes (GPO and DNS reservations in particular) for a few minutes
- On NewDC stop and disable the rsync replication timer units
- On NewDC manually start each of the rsync replication jobs to ensure OldDC and NewDC are in sync and we have a "known good" time for this.
- On NewDC use samba-tool to take over the FSMO roles from OldDC, including the DNS roles.
- On OldDC update /etc/rsync.d/samba-primary.env so SAMBA_PRIMARY=NewDC host name
- On NewDC manually start the rsync-core and confirm samba-primary.env has updated.
- [ Change freeze end ] - Updates to GPO permitted again as NewDC will be updated. Changes to DNSmasq should now be done on NewDC too
- Disable rsync service unit on NewDC
Any other DCs should now get one last rsync update from OldDC and then start using NewDC once they get the updated samba-prmary.env file. Once they've all updated themselves OldDC can be stopped.
The key thing is to avoid a situation where any of the GPO files in sysvol get updated during the change freeze. If the PDC Emulator role is moved before the last sysvol rsync then the changes to the policy file will get reverted back to the old version. If the PDC Emulator role is moved after the last sysvol rsync the changes could be lost. Either way this is not an optimal outcome but the second option is least worst in my opinion.
OldDC can now be decommissioned using samba-tool to nicely demote it from being a DC and then remove it from the domain.
Backout
Just in case something interesting happens during the switchover, up to step 9 you can just stop and revert the previous steps in reverse order. From step 9 onwards you either need fix the issue and complete the switchover or use the Unplanned Switchover process to move back to OldDC or any other additional DC. And if you end up having to seize the FSMO roles at least you know it's not too much effort to delete and redeploy NewDC once cleanup is complete.
Restart OldDC
If this switchover was just for maintenance on OldDC then to bring it back into use as an additional DC it's just a matter of:
- Confirm the RSync service is stopped and disabled on OldDC
- Confirm samba-primary.env on OldDC contains hostname of NewDC
- Confirm /etc/dnsmasq.d/dhcp-delay.conf exists on OldDC
- Set the rsync replication service and timer units to enabled and started on on OldDC and that all files are brought up to date
- Confirm samba-ad-dc service is running on OldDC and no issues reported
- Update DHCP server options on NewDC to include the IP address of OldDC in dns-server and ntp-server once again. This will then be replicated to OldDC
- Mark OldDC as available again for any LDAP/S applications or proxies
Unplanned Switchover
And just for completeness, if something really bad happens to OldDC (or whatever host is now Primary) and you cannot get it back online then it's a much shorter and brutal process
Remember: Don't panic.You have two DCs and the other should be able to keep things going for a while. If you've only got one then you've picked a bad day to be in the office...
- Stop and disable the rsync replication service and timer units on NewDC
- Enable and start the rsync server on NewDC
- Remove the /etc/dnsmasq.d/dhcp.delay.conf file on NewDC. Also remove the IP address for OldDC from DHCP options dns-server and ntp-server. Restart DNSmasq
- Update the /etc/rsync.d/samba-primary.env file on NewDC so SAMBA_PRIMARY it set to the host name of NewDC
- Update the /etc/rsync.d/samba-primary.env files on any other surviving DCs so they also know that NewDC is now the Primary and should be able to replicate from it.
If OldDC is no longer recoverable and you've gone too long with it offline:
- Make sure OldDC cannot contact any other DCs just in case it miraculously comes back online. Move to an isolated network or delete it.
- Seize the FSMO roles on NewDC
- Remove the OldDC from AD and complete cleanup steps
- Remove OldDC from any applications or proxies using it for LDAPS
- Inquisition and retribution as merited
Seizing the FSMO roles is not a step to be taken lightly. There are dire warnings about the potential for catastrophic corruption of AD if this goes wrong and especially if OldDC comes back online and is able to reconnect to AD. You do keep backups of the AD configuration don't you? It's really not that hard in a Samba environment you know and it may just save your sanity.