This is an Ansible Role to deploy and configure Samba 4 as a Domain Controller with all the pre-requisites (that I haven't put in separate roles) and then apply config from the Ansible Inventory.

Manual Process

This is a lot to do manually. There's a reconfiguration of systemd-resolved, installing the packages and then stopping unwanted services. Kerberos then needs to be configured and the default Samba config removed before finally running the samba-tool to perform the DC join operation.

Ansible Role: samba-dc

This was created for Building an additional Samba 4 Domain Controller 2

Samba requires a signed TLS certificate for full operation and this role cannot perform that step since I do that with an offline key so there is alway some manual post-processing required. For that reason the parameters are added to the smb.conf file but commented out - Samba objects if you leave placeholder values with TLS Enabled = no.  I also leave the samba-ad-dc service in a stopped state for this reason.

If you have a join failure you can use samba-tool domain demote to try and gracefully exit the domain - you cannot use domain leave in this case. While it doesn't really matter what goes wrong with the VM since you could always delete and recreate it, you really don't want to mess up the domain itself. You may need to start the samba-ad-dc service in order to demote yourself from being a domain controller (TLS cert is not required for this) and while a quick exit seems to be mostly successful, it appears if you leave it too long you can be caught in a trap that you cannot properly start samba but you also cannot leave as it fails to replicate out your demotion request and then crashes samba-tool. I've found leaving the wounded DC online overnight seems to allow this to resolve itself and you can then demote yourself normally.

I've also used a handy Windows machine to run ADUC and Sites & Services to remove the final traces of a failed DC before trying again.

Dependencies

These roles need to run first bfore samba-dc:

  1. host-statichosts - Prepare the /etc/hosts file to Samba's liking
  2. ntp-server - Install an NTP server with support for mssntp to keep Windows hosts happy.

If they're not run then it won't cause any immediate issues but Samba may well get upset.

File Structure

This is a standard Ansible role with a single task .yaml file, two j2 templates, a meta to keep Ansible happy and a README.md for my own notes.

host-statichosts  

  meta

    main.yaml

  tasks     

    main.yaml  

  templates

    krb5.conf.j2
    smb.conf.j2

  README.md

tasks/main.yaml

This is the remaining samba-specific preparation tasks leading up to the call to samba-tool to join the domain.

--- # Ansible Role Tasks

# Role for deploying Samba 4 AD-DC

- name: Disable Systemd DNSStubListener
  become: true
  ansible.builtin.lineinfile:
    path: /etc/systemd/resolved.conf
    search_string: '#DNSStubListener=yes'
    line: 'DNSStubListener=no'

- name: Restart systemd-resolved
  become: true
  ansible.builtin.systemd_service:
    name: systemd-resolved
    state: restarted

- name: Configure resolve.conf
  become: true
  ansible.builtin.file:
    path: /etc/resolv.conf
    src: /run/systemd/resolve/resolv.conf
    owner: root
    group: root
    state: link

By default systemd is listening for DNS queries on port 53 which we need to use for real DNS so we need to explicitly set DNSStubListener to No and restart systemd-resolved so it takes effect. We then set /etc/resolv.conf to point to the a dynamic version maintained by systemd in memory and accessed as the tempfs filesystem /run instead of being an actual file.

- name: Install Samba packages
  become: true
  ansible.builtin.apt:
    package:
    - bind9-dnsutils
    - krb5-user
    - samba-ad-dc
    - python3-pexpect

The samba-ad-dc package is specific to Samba when used in DC mode. We also need the utilities from bind9-dnsutils (this is not BIND9 itself) and the Kerberos user programs. The python3-pexpect module is needed for the responding to a password prompt during installation. We could remove it after this role is complete too.

- name: Check domain membership
  become: true
  ansible.builtin.command: net ads testjoin
    register: testjoin_result
    changed_when: false
    ignore_errors: true

Before attempting to join the domain as DC we can use the net ads testjoin command to check if we're a member or not. If the host is not yet a member this returns an error which would stop the play so we need to explicitly set ignore_errors so it can continue. The Join domain task defines a block of tasks that will all be executed if the testjoin dd not report that the host is already joined to the domain. For clarity I alway add a comment at the end of a block althought the indentation is supposed to be enough. Ugh.

- name: Join domain
  when: testjoin_result.stdout != "Join is OK"
  block:

 

  - name: Mask unwanted Samba services - smbd
    become: true
    ansible.builtin.systemd_service:
      name: smbd
      state: stopped
      enabled: false
      masked: true

  - name: Mask unwanted Samba services - nmbd
    become: true
    ansible.builtin.systemd_service:
      name: nmbd
      state: stopped
      enabled: false
      masked: true

  - name: Mask unwanted Samba services - winbind
    become: true
    ansible.builtin.systemd_service:
      name: winbind
      state: stopped
      enabled: false
      masked: true

  - name: Disable samba AD services for now
    become: true
    ansible.builtin.systemd_service:
      name: samba-ad-dc
      state: stopped
      enabled: false

The standard Samba smbd, nmbd and windbind services must be stopped and masked to prevent them from being started as they will otherwise interfere with the samba-ad-dc service has simiilar internal services. I also stop samba-ad-dc at this point since we're going to adjust the configuration.

  - name: Drop Kerberos config
    become: true
    ansible.builtin.template:
      src: krb5.conf.j2
      dest: /etc/krb5.conf
      owner: root
      group: root
      mode: '0644'

  - name: Rename default smb.conf file
    become: true
    ansible.builtin.command: mv /etc/samba/smb.conf /etc/samba/smb.installed
      args:
      removes: /etc/samba/smb.conf
      creates: /etc/samba/smb.installed

The original smb.conf file is moved to smb.installed otherwise samba-tool can get confused. If there's already an smb.installed it'll skip that in case you're rerunning as part of troubleshooting.

  - name: Join Samba Domain
    become: true
    expect:
      command: /bin/bash -c "/usr/bin/samba-tool domain join {{ ad_domain }} DC -U {{ config_ad_admin_user }}"
      responses:
        Password.*: "{{ config_ad_admin_cred }}"
      timeout: 300
      ignore_errors: true

      # dns forwarder not added to smb.conf even if specified in --options

I'm using expect instead of command to execute the samba-tool inside a bash shell to avoid putting the domain admin password into a command line where it could potentially be intercepted. Expect is not entirely reliable and seting the timeout to 5 minutes - which should be longer than samba-tool takes to run seems to avoid problems. If this should fail then we'll handle that later so this task is also set to ignore errors.

  - name: Drop Samba config
    become: true
    ansible.builtin.template:
      src: smb.conf.j2
      dest: /etc/samba/smb.conf
      owner: root
      group: root
      mode: '0640'

#----- End of block Join Domain

- name: Recheck domain membership
  become: true
  ansible.builtin.command: net ads testjoin
  register: retestjoin_result
  changed_when: false
  ignore_errors: true

- name: Requst manual domain join
  when: retestjoin_result.stdout != "Join is OK"
  ansible.builtin.pause:
    prompt: "There was an issue joining the domamin. Please fix before going any further. [Enter] when ready"

We do another check if the join worked, and if not then we pause the playbook to allow a chance for manual intervention to fix the problem rather than fail the build.

# Log rotation now controlled within Samba (smb.conf), we do not need to set up logrotate ourselves

...

 

The role is actually quite simple and samba-tool does most of the work - it take take a few minutes to run. Unlike doing it manually you don't get any updates while its running and then a deluge of output at the end which isn't exactly friendly.

templates/krb5.conf

This is the standard config from the krb5user package with the following changes:

  1. default_realm set to the AD domain from the inventory using capital letters
  2. realms and domain_realm also set with inventory parameters, although I'm not sure if that's really needed. The default has a whole bunch of domain as "tribute" to the original authors that really shouldn't be there.

Using a template is much easier than trying to do line-by-line search/replace or delete. This might be from an slightly older install so I will recheck and confirm but it does work.

[libdefaults]
  default_realm = {{ ad_domain | upper }}
  dns_lookup_realm = false
  dns_lookup_kdc = true

# The following krb5.conf variables are only for MIT Kerberos.
  kdc_timesync = 1
  ccache_type = 4
  forwardable = true
  proxiable = true

# The following encryption type specification will be used by MIT Kerberos
# if uncommented. In general, the defaults in the MIT Kerberos code are
# correct and overriding these specifications only serves to disable new
# encryption types as they are added, creating interoperability problems.
#
# The only time when you might need to uncomment these lines and change
# the enctypes is if you have local software that will break on ticket
# caches containing ticket encryption types it doesn't know about (such as
# old versions of Sun Java).

# default_tgs_enctypes = des3-hmac-sha1
# default_tkt_enctypes = des3-hmac-sha1
# permitted_enctypes = des3-hmac-sha1

# The following libdefaults parameters are only for Heimdal Kerberos.
# fcc-mit-ticketflags = true

[realms]
{{ ad_domain | upper }} = {
  default_domain = {{ ad_domain }}
}

[domain_realm]
  {{ inventory_hostname_short | upper }} = {{ ad_domain | upper }}

templates/smb.conf.j2

 This is the smb.conf generated by samba-tool with the following changes and is specific to a DC configuration. Many of the options for a "normal" Samba install are not valid and some will cause damage if you even try them.

  1. dns forwarder added since samba-tool does do this if passed to it as an additional option.
  2. The TLS options added a placeholder once certificates are ready.
  3. All other parameters are as samba-tool sets them, just substituted for values from the Ansible Inventory that would be appropriate for the domain with some additional comments as reminders for myself.

# Global parameters
[global]
   dns forwarder = {{ network_gateway4 }}
   netbios name = {{ inventory_hostname_short | upper }}
   realm = {{ ad_domain | upper }}
   server role = active directory domain controller
   workgroup = {{ ad_domain.split('.')[0] | upper }}

  # TLS using system cert for LDAPS, maybe others
  # tls enabled = yes
  # tls keyfile = /etc/ssl/private/{{ inventory_hostname_short }}_key.pem
  # tls certfile = /etc/ssl/certs/{{ inventory_hostname_short }}_chain.pem
  # tls cafile = /etc/ssl/certs/Example_RootCA.crt

# Delete local ansible user before enabling winbind for domain users on this host.
# Enabling domain users at host level is NOT recommended by Samba!

[sysvol]
  path = /var/lib/samba/sysvol
  read only = No

[netlogon]
  path = /var/lib/samba/sysvol/{{ ad_domain }}/scripts
  read only = No