In Samba 4 Domain Controller - part 1 we prepared our Samba DC deployer, created the bash scripts and config that will first call the Ubuntu 24.04 Deployer before executing an Ansible Playbook to install Samba 4 as an additional Active Directory Domain Controller. In this article we are going to update the Ansible Inventory with the variables that will be needed and then build the the roles and playbook that will do the Samba install.

Ansible Inventory Update

There's a lot of parameters that are required as input to the VM build and install processes which need to come from somewhere. Some operational ones such as SSH keys and which host to target are best passed on the command line, others can be build into the roles themselves if they never change but what we really need is a store of information about the server landscape can be be used by all the playbooks and maintained as a "single source of truth" about what we have. This is one of the key features of Ansible and it's called the Inventory.

As I've mentioned before, there a huge amount of material on the internet about how to create an Inventory. A common question is how best to do it and my take on that is whatever works best for you is best to do it. And the only way to work that out is to have a go and try it. If you've been reading other articles here you'll probaby notice the Inventory examples have changed a fair bit and they'd because I keep tinkering and finding a new best way to arrange things.

Most of the changes to the Inventory for the Samba DC playbook have been made to support static IP addresses, but we also need to make a couple more specific to Samba and AD itself.

At the very beginning I suspected I would need to handle domain controllers differently, as although they are part of a domain they're more than just members so i created a two separate groups:

 GroupInventory FilePurpose
 samba_dc 0010-group-ad.yaml Contains a domain-specific groups so we can easily test if a host is a domain controller.
 sambadc_[domain] Network file e.g.
3000-test.yaml
Contains all hosts that are domain controllers and network-specific variables for them. Each domain group is also listed as a sub-group of samba_dc in the 0010-group-ad file.

These are now coming into their own as it turns out I do need to keep them separate from domain members.

Within each network the following variable are defined:

 VariableExamplePurpose
ad_domain test.example.com The full name of the AD domain
ad_domain_primary s4dc1.test.example.com The nominated DC to be a replication source for SYSVOL and DNSmaq config

Ive added these just within the samba groups since that's where they're used but they could be defined at a higher level too.

Taking the test network as an example, and incorporating the updates for static IP and the other paramters in use for a host build we would now have something like this:

0010-group-ad

######################
# samba_dc
# Domain Controllers used for triggering DC join and promotion

samba_dc:
  children:
    sambadc_live:
    sambadc_test:
    sambadc_sandpit:

 3000-test.yaml

#################################################
# Test Network
#

test:
  vars:
    config_landscape_tier: "T"
    config_sambatool_dc: s4dc-1.test.example.com
   
apcupsd_device: 192.168.451.1
    apcupsd_batterylevel: 50
    apcupsd_runtime: 10
    esx_network: Test51
   
network_gateway4: 192.168.451.1
    network_ntp: 192.168.451.1
    network_address4_mask: 24
    network_nameserver4_1: 192.168.451.64
    network_nameserver4_2: 192.168.451.65
    network_dns_update: samba-tool
    network_search: test.example.com

  children:

    # Samba DC must be members of this group
    sambadc_test:
      vars:
        network_type: static
        ad_domain: test.bostoncube.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-200/X_U2404-base-2.ovf
          network_address4: 192.168.451.65

In this example the host s4dc-2 has just been deployed, s4dc-1 is just a placeholder and will be deployed once I'm happy that s4dc-2 is working normally.

Ansible Roles

I'm using seven roles for the installing and configuring the Samba 4 DC itself, and NTP server, DNSmasq as a simple DHCP server replicating configuration from the nominated primary DC and then putting everything into a safe state untill the manual configuration tasks are completed.

Some of these roles are tiny and you could argue some of them could be merged and maybe for a future version 2 I'll do that.

Each role is created in a subdirectory of ~/automation/ansible/rolefactory and has its own git repository linked to my internal git server. This lets me do a couple of things:

  • The live version of the playbook can use a requirements.yaml to automatically download roles at the specified version from my git host. The refresh-roles.sh script we created as part of Deployer setup takes care of that.
  • For the dev version of the playbook I can create a link from the playbook roles directory to each role under rolefactory so changes appear instantly without going through a checkin/checkout cycle for each change which just gets annoying.

It also means that I can have separate live and dev versions of roles too, so if I'm working on enhancements I don't break the Deployer in the mean time.

Role: host-statichosts

This role configures the /etc/hosts file how Samba likes it - and other apps have similar tastes too - with the actual IP address of the host defined for the hostname rather than the loopback address 127.0.1.1. This could be moved into the main samba-dc role too.

Full details: host-statichosts

Role: ntp-server

This role installs the Ubuntu ntpsec package from the standard repositories which provides a Network Time Protocol server and then replaces the configuration file with a customised version that disables the default pool servers and adds in the NTP server dedicated to the subnet as per the network_ntp parameter in the Ansible Inventory. It also enables a socket for NTP signing and mssntp for Windows hosts, although it appears there is currenty an incompatiblity betwen NTPsec and changes made to Windows so it's slightly broken right now.

Full details: ntp-server

Role: samba-dc

This is the main role for this play and completes the rest of the pre-requisites before installing Samba 4 and applying the steps from the configuration guide.

For full operation the DC needs a signed TLS certificate which this role obviously cannot provide so that is a manual task to be completed once the Deployer has finished.

Full details: samba-dc

Role: gitdeploy

This role is used to deploy scripts held in a git repository and is used in a couple of other plays too. Parameters passed to the role specify the repository and a tag or branch and the destination.

Full details: gitdeploy

Role: rsync-samba

This is an unhappy role, in that while it works and does what I need, it doesn't feel quite right and needs a bit of a rethink. Well, to be entirely honest I've a good idea how it needs to change, it's just picking the time to do it.

That aside, its purpose is to configure RSync to replicate the content of the Samba NETLOGIN and SYSVOL shares from the nominated primary server to the one the one we're installing. This can happen regardless of the state Samba is in, although it requires the primary server to be running an RSync daemon.

Full details: I will add this once I've finished restructuring it.

Role: dnsmasq-samba

This relies on the rysnc-samba role to have already completed some config of the RSync replication (you see why I'm wanting to change that now?) and then configures a separate replication for the DNSmasq DHCP configuration. This is a new for this this rebuild as I've previously been applying config changes to each DC separately.

Full details: dnsmasq-samba

Role: wsdd

This last role is tiny and just deploys the WSDD scripts so it announces itself to Windows systems so they can see the NETLOGIN and SYSVOL shares to pick up login scripts and GPO policies.

Full details: wsdd

Roles: requirements.yaml

Ansible has a nice feature for roles. If you prepare a requirements.yaml file you can pass this to ansible-galaxy and it will then install all the listed roles at the specified version for you so you don't have to keep worrying. I put this in the playbook roles directory and use the refresh-roles.sh script to do the update.

I use an internal server to host git repos using SSH as it was a very quick and easy way to get started with git, although you can use hosted services like gitlab or even that microsoft one everyone complains about. ;-)

Each role needs to be listed and version can be a branch or a tag.

--- # Required roles
# Make sure .gitignore set to exclude these from the playbook

roles:

- name: host-statichosts
  scm: git
  src: git-host.example.com:/srv/git/ansible/roles/host-statichosts.git
  version: master

- name: ntp-server
  scm: git
  src: git-host.example.com:/srv/git/ansible/roles/ntp-server.git
  version: master

- name: samba-dc
  scm: git
  src: git-host.example.com:/srv/git/ansible/roles/samba-dc.git
  version: master

- name: gitdeploy
  scm: git
  src: git-host.example.com:/srv/git/ansible/roles/gitdeploy.git
  version: master

- name: rsync-samba
  scm: git
  src: git-host.example.com:/srv/git/ansible/roles/rsync.git
  version: master

- name: dnsmasq-samba
  scm: git
  src: git-host.example.com:/srv/git/ansible/roles/dnsmasq.git
  version: master

- name: wsdd
  scm: git
  src: git-host.example.com:/srv/git/ansible/roles/wsdd.git
  version: master

#collections:
# - name: community.general

We don't need any extra collections for this Deployer but I've left the collections: section as a comment as in case it's wanted in future.

Playbook: sambadc-10.yaml.

We only need a single playbook to complete the installation of Samba and all its pre-requisites, configuration and dependencies which are performed by the roles above.

--- # Ansible Playbook

# Build a Samba AD DC and add to domain
#
# Updated: 1 Feb 25
#
# This playbook prepares a freshly delivered host ready for Samba AD DC configuration
# Note: Host must have a static IP

- name: Prepare for Samba 4 installation
  hosts: "{{ target }}"

  vars_files:
  - vars/vault-newhost.yaml
  - vars/config-newhost.yaml

We need to use the domain admin credential to join and promote the host to be a Domain Controller. These are held in an Ansible Vault  and made available to the playbooks than need them

  pre_tasks:

  - name: Stop unattended upgrades
    become: true
    ansible.builtin.systemd:
      name: unattended-upgrades
      state: stopped

During testing the one of the package installations was broken because Ubuntu decided to do an unattended upgrade which was rather annoying. I might even have said a rude word or three. To prevent this the unattended-upgrades service can be stopped, whether you disable it or uninstall it completely is a separate discussion.

  roles:

  - role: host-statichosts

  - role: ntp-server

  - role: samba-dc

  - role: gitdeploy
    vars:
      git_repo: ssh://internal-git.example.com:/srv/git/scripts/bacula.git
      git_version: samba24
      destination: /opt/bostoncube/bacula

I use Bacula to backup each server and this is configured during server build. This deploys an add-in script to safely backup the Samba databases while they're online. You can skip this if you're doing backup differently. You are going to do backups aren't you?

  - role: rsync-samba

  - role: dnsmasq-samba

  - role: wsdd

...

 Ready

We are now ready to start deplying new additional domain controllers built on the latest LTS version of Ubuntu.

The next step is to update the inventory with the new variable and to prepare the nominated primary domain controller host with the central RSync config so it can be replicated to the new DCs by these playbooks. And that will be covered in the final article in this series.