Ever since the first GeekLab I've used an Active Directory domain to define internal users. I have maybe half a dozen real "human" accounts and a few more that run services so it's not a huge deployment but with several PCs & laptops in the family, an internal email server and some Nextcloud instances having just one username and password for all of them makes life just that little bit simpler for everyone.

The original AD domain was created with a Windows 2003 Small Business Server which has long been retired and for the last four years the domain has been on a pair of Ubuntu 20.04 virtual machines running Samba 4 in ad-dc mode which are themselves nearing end-of-life so it's upgrade time. I also have test domains (renamed clones) in the same situation, so there's several servers that I need to sort out.

Both "real" and Samba 4 domain controllers replicate Active Directory between themselves, so rather than trying to upgrade Ubuntu I can build new hosts, install Samba 4 on them and join them to the domain. Once they've completed replication and everything looks stable I can then perform a takeover of the FSMO roles and retire the old instances, but that's a separate activity for later.

The Ansible Playbook and roles I'm going to describe in this series will build:

  1. A new Ubuntu 24.04 server
  2. Install NTP server to synchronise time for Windows hosts*
  3. Install Samba 4 and join it to the domain as an additional domain controller
  4. Install DNSmasq to act as a DHCP server and integrate into Samba DNS
  5. Install RSync to replicate the DNSmasq config and Samba SysVol file from a designated master server
  6. Install WSDD so my few remaining Windows PCs can see the NETLOGON and SYSVOL shares

It'll also deploy an extension to the Bacula scripts to do a local backup of the Samba database files to create a consistent backup of the data before Bacula copies off to separate storage and fix up the hosts file as Samba like it.

Both Samba 4 and DNSmasq require the host has static IP addresses so a prerequiste task for this was to update my Ubuntu 24.04 deployer to configure a static IP during VM build and to also add it to appropriate DNS zone so that Ansible could connect to the target VM.

Note: This is all about connecting to an existing Active Directory.  While the process to create a new Samba 4 Active Directory looks to be a very similar process - just  different call to samba-tool - that is a very different requirement and not one I hope to need any time soon.

Create Samba DC Deployer

This is all based on my automation setup (see Approach for the concepts behind this) and the first step is to set up a new Deployer directory under ~/automation/ansible/playbooks to hold the scripts and playbooks. I keep the config in git repositories so it's actually worth creating two, a u24-samba-dc-dev for the development version and and u24-samba-dc for live version. While this is pretty much a one-shot project since I don't intend to be deploying new DC on a regular basis, it's still better to have a separate Deployer instance in a known-good working state and a separate one for playing with. It's probably overkill but a good habit to keep to.

As a kick-start we can copy the files from the Ubuntu 24 deployer we created in Deployment - Bash scripts. You'll want the following files:

  • .gitignore
  • ansible.cfg
  • ansible-env.sh
  • refresh-roles.sh
  • ubuntu24.conf copies to sambadc.conf
  • ubuntu24.sh copied to sambadc.sh
  • ubuntu24-deploy.sh copied to sambadc-deploy.sh
  • vars/*
  • roles/requirements.yaml

If you're going to use git (or some other version control) get that set up first and then copy the files into your dev directory.

The first four files on this list don't need any changes. We can use the same VM template series (u2404-base-2) we've used previously as there's no additional disks required and we need to pass the same variables to the Ubuntu deployer.

The remaining files all need to be customised.

sambadc.conf

The first part of this is the same as the ubuntu24.conf file we copied it from. We still need to pass all the parameters to the Ubuntu deployer for it to build our virtual machine. We need to enhance it with two extra parameters for the sambadc.sh script to specificy where the Ubuntu deployer is actually located so it can call it.

# Config file included by sambadc.sh
# This provides paramters to the launcher script

# ======================================================
# Part 1 : Ubuntu VM deployment Parameters
#

# Ansible vars files with ESXi details
export ESX_DEPLOY="@../../control/newhost/esx-deploy.yaml"

# Hostname of an freshly deployed template. Domain name is an input parameter.
export OVF_HOSTNAME=u2404-base-2

# User for SSH password login for playbook 1
export TARGET_BUILD_SSH_USER=template-build-user
export TARGET_BUILD_SSH_KEY=../../control/newhost/template-build-user

# User and their key location for SSH login after playbook 1
export TARGET_ADMIN_SSH_USER=automation-user
export TARGET_ADMIN_SSH_KEY=../../control/automation-user

# ======================================================
# Part 2 : Ubuntu VM deployer location
#

# Path to the deploy script for Ubuntu VM build
export UBUNTU_BUILDER_LOCATION="../u24-base"
export UBUNTU_BUILDER_SCRIPT="ubuntu24-deploy.sh"

# ======================================================
# Part 3 : Samba DC specific variables
#

I've added a few more comments to the script and as it turned out I didn't need to add any bash environment variables for the Samba DC build but I'm going to keep that as a reminder for future Deployers.

sambadc.sh

This is the main script for deploying an additional Samba 4 DC virtual machine, and just like the original ubuntu24.sh it will read in the config files. There are two changes:

  1. Instead of calling ./ubuntu-deploy.sh it now calls the Ubuntu deployer script in the location specified by the UBUNTU_BUILDER_LOCATION and UBUNTU_DEPLOYER_SCRIPT parameters in sambadc.conf
  2. If VM deployment is then claimed as a success it then calls ./sambadc-deploy.sh to deploy the Samba 4 DC on our freshly built VM

 

#!/bin/bash
echo ""
echo "This will launch the deployment of a new Samba DC on Ubuntu 24.04 LTS virtual machine."
echo ""
echo "Setting environment..."

TARGET_HOST=""
TARGET_DOMAIN=""

MYDIR=$PWD

# Import configuration for this script and ansible environment

source ./sambadc.conf

source ./ansible-env.sh

if [[ $# -ne 2 ]]; then
  echo ""
  echo "Please specify the hostname and the domain name"
  echo "e.g. sambadc.sh myhost mydomain.example.com"
  echo ""
  exit
else
  TARGET_HOST=$1
  TARGET_DOMAIN=$2
fi

echo ""
echo "Launching deployment scripts..."
echo ""

# Check Ubuntu deployment playbooks exist
if [[ -f $UBUNTU_BUILDER_LOCATION/$UBUNTU_BUILDER_SCRIPT ]]; then
  echo "Found Ubuntu deployer at $UBUNTU_BUILDER_LOCATION/$UBUNTU_BUILDER_SCRIPT"
  echo ""
else
  echo "Cannot find Ubuntu deployer at $UBUNTU_DEPLOYER_LOCATION/$UBUNTU_BUILDER_SCRIPT"
  exit 1
fi

cd $UBUNTU_BUILDER_LOCATION
./$UBUNTU_BUILDER_SCRIPT $TARGET_HOST.$TARGET_DOMAIN $OVF_HOSTNAME.$TARGET_DOMAIN
DEPLOY_OK=$?

if [[ $DEPLOY_OK == 0 ]]; then
  echo ""
  echo "------------------------"
  echo "Ubuntu Build Complete"
  echo ""
else
  echo ""
  echo "------------------------"
  echo "Ubuntu Build Failed : $DEPLOY_OK"
  echo ""
  exit $DEPLOY_OK
fi

cd $MYDIR

# And now call the Samba DC deployment scripts
./sambadc-deploy.sh $TARGET_HOST.$TARGET_DOMAIN
DEPLOY_OK=$?

if [[ $DEPLOY_OK == 0 ]]; then
  echo ""
  echo "------------------------"
  echo "Samba DC Build Complete"
  echo ""
else
  echo ""
  echo "------------------------"
  echo "Samba DC Build Failed : $DEPLOY_OK"
  echo ""
  exit $DEPLOY_OK
fi

 

sambadc-deploy.sh

This script now only needs to run a single playbook using the RunPlaybookSSH function so can be significantlly simplified.

#!/bin/bash
echo "################################################################################"
echo "#"
echo "# Deploy a Samba DC on a prepared Ubuntu 24.04 LTS VM to ESXi"
echo "#"
echo "# Running Ansible Playbooks to prepare a new Samba DC installation."
echo "# This playbook uses the base image"
echo "#

"# Validation performed by calling script
TARGET_HOST_FULL=$1

# Via environment exports in launcher script
# TARGET_ADMIN_SSH_USER=automation-user
# TARGET_ADMIN_SSH_KEY=../../control/automation-user

echo ""
echo "Launch parameters:"
echo ""
echo " Target Host: $TARGET_HOST_FULL"
echo ""
echo " Inventory: $ANSIBLE_INVENTORY"
echo " Vault Key: $ANSIBLE_VAULT_PASSWORD_FILE"
echo ""
echo "For first run of the playbook manually install collections:"
echo "ansible-galaxy collection install -r ./roles/requirements.yaml"
echo "Use the refresh-roles.sh script to get latest versions of the roles for this playbook"
echo ""

# ansible-galaxy install -f -r ./roles/requirements.yaml

#######################################
# Bash functions for this script
#

RunPlaybookSSH () {
# Run a playbook as given user and SSH key.

  local PLAYBOOK=$1
  local SSH_USER=$2
  local SSH_KEY=$3

  local PLAYBOOK_RET="X"

  echo "----------------------------------------------------------"
  echo "- Running $PLAYBOOK for $TARGET_HOST_FULL"
  echo "- "
  echo "-"

  ansible-playbook -v --private-key $SSH_KEY -u $SSH_USER $PLAYBOOK --extra-vars target=$TARGET_HOST_FULL

  PLAYBOOK_RET=$?
  if [[ $PLAYBOOK_RET == 0 ]]; then
    echo "Playbook $PLAYBOOK reported success for $TARGET_HOST_FULL"
    echo ""
  else
    echo "Playbook $PLAYBOOK reported error $PLAYBOOK_RET for $TARGET_HOST_FULL, stopping."
    echo ""
    exit $PLAYBOOK_RET
  fi

}

#######################################
# Main execution
#

RunPlaybookSSH sambadc-10.yaml $TARGET_ADMIN_SSH_USER $TARGET_ADMIN_SSH_KEY

echo ""
echo "Samba DC deploy complete."
echo ""

I was half expecting to use a couple of playbooks but in the end one is enough to do it all. While some may argue keeping a function that only used once and is the whole point of the script is not optimal I'm going to keep it like this just in case once day I need to add a second playbook.

Next Steps

We now have everything in place ready to run our sambadc Ansible Playbook. The next step is to prepare the playbook itself and the roles that will do the installation and coniguration of Samba on our freshly built Ubuntu 24.04 LTS virtual machine.

The playbook and roles sambadc-10.yaml is discussed in part 2, and the setup end execution will be covered in part 3.