• Python 65.6%
  • Jinja 34.4%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-10-27 16:44:29 +00:00
.forgejo/workflows feat: initial version of samba ad 2026-10-27 15:39:52 +01:00
defaults feat: added lxc support, online backup etc. 2026-10-27 17:40:48 +01:00
files feat: initial version of samba ad 2026-10-27 15:39:52 +01:00
handlers feat: added lxc support, online backup etc. 2026-10-27 17:40:48 +01:00
meta feat: initial version of samba ad 2026-10-27 15:39:52 +01:00
tasks feat: added lxc support, online backup etc. 2026-10-27 17:40:48 +01:00
templates feat: added lxc support, online backup etc. 2026-10-27 17:40:48 +01:00
vars feat: initial version of samba ad 2026-10-27 15:39:52 +01:00
.gitignore feat: initial version of samba ad 2026-10-27 15:39:52 +01:00
CHANGELOG.md feat: added lxc support, online backup etc. 2026-10-27 17:40:48 +01:00
LICENSE feat: initial version of samba ad 2026-10-27 15:39:52 +01:00
README.md Prettified Code! 2026-10-27 16:44:29 +00:00

Samba AD DC

This role installs and configures a Samba Active Directory Domain Controller.

Integrated features

Domain provisioning

The role provisions a new Samba AD domain using the internal DNS backend and RFC2307 schema. The provisioning state is tracked via local Ansible facts to prevent re-provisioning on subsequent runs.

DNS configuration

DNS forwarders can be configured for Samba's internal DNS server to resolve external queries. The local resolver is automatically configured to use the Samba AD DC.

The default forwarder is set to 1.1.1.1 if no other has been provided by the samba_ad_dns_forwarder variable.

Kerberos configuration

The Kerberos configuration (/etc/krb5.conf) is managed by this role and configured to work with the AD realm.

SMB configuration

The SMB configuration is templated and supports optional LDAPS (LDAP over TLS) when certificates are provided.

Organizational Unit management

OUs can be created and deleted via the samba_ad_ous variable. Nested OUs are supported by specifying a parent DN.

User management

AD users can be created, deleted, and disabled. The following user attributes are supported:

  • Username (SAM account name)
  • Password
  • Given name and surname
  • Email and phone
  • Organizational Unit placement
  • Service account flag (password never expires)
  • Force password change at next login

Existing users are reconciled on every run:

  • Given name, surname, email and phone are replaced when they differ from the inventory. Attributes not set in the inventory are left untouched, removing one from the inventory does not remove it from AD.
  • The user is moved (samba-tool user move) when ou is set and differs from its current OU. Without ou the user stays where it is.
  • The password is only used on creation by default (update_password: on_create), so a password the user has changed is never reset. With update_password: always it is reset on every run, which is not idempotent and also resets pwdLastSet - meant for service accounts whose password is rotated via the inventory.

Changing given name or surname does not rename the object (CN) or change its displayName.

Group management

AD groups can be created and deleted. Group membership can be managed by specifying members to add or remove. Existing groups are moved when ou is set and differs from their current OU.

RDP access for domain users

Windows only allows local administrators to log in via Remote Desktop by default. The role therefore always manages a GPO named RDP access for Domain Users that adds the AD group Domain Users to the local group Remote Desktop Users (S-1-5-32-555) on every domain-joined computer. This way every regular domain user can log in via RDP without adding them on each machine by hand.

  • The GPO uses Group Policy Preferences (Local Users and Groups) with the action Update: existing members of the local group are kept.
  • The local group is addressed by its well-known SID, so it works regardless of the Windows display language.
  • The GPO is linked to the domain root and therefore applies to all computers.
  • On changes the GPO version is raised so clients pick them up on their next policy refresh (gpupdate /force to apply immediately).

This behavior is not configurable. It is only applied on primary DCs (skipped when samba_ad_secondary_dc: true), secondary DCs receive the GPO via replication and SYSVOL synchronization. The tasks carry the gpo tag.

Note: The GPO does not enable Remote Desktop itself or open the firewall on the clients.

Password policy

The domain maximum password age can be set via samba_ad_max_pwd_age (days, 0 = passwords never expire). Samba's provisioning default is 42 days, after which every account without the service account flag can no longer bind via LDAP (error data 532). When the variable is unset, the role does not touch the password policy.

This task is only applied on primary DCs (skipped when samba_ad_secondary_dc: true).

PTR record maintenance

A systemd timer runs a script every 10 minutes that reads all forward DNS zones and automatically creates, updates, or deletes the corresponding PTR records in the matching reverse zones. Missing reverse zones are created on the fly.

This task is only applied on primary DCs (skipped when samba_ad_secondary_dc: true).

Note: The script is sourced from a separate repository: https://gitlab.cinoware.com/ansible/helpers/samba-tools

DNS zone management

Tip: All DNS tasks (zones, records and PTR maintenance) carry the dns tag. Run --tags dns to apply only the DNS-related parts of the role without touching the rest of the configuration.

DNS zones can be created and deleted in Samba's internal DNS server via the samba_ad_dns_zones variable. Each item takes a zone name; set delete: true to remove a zone. Zones are created before DNS records, so records placed in a custom zone (via the record's zone field) find their zone present.

This task is only applied on primary DCs (skipped when samba_ad_secondary_dc: true).

Warning: Deleting a zone removes all records it contains.

DNS record management

DNS records can be created and deleted in Samba's internal DNS server via the samba_ad_dns_records variable. Supported record types are A, AAAA, CNAME, TXT and SRV. Each record takes its fully qualified name, type and value; the zone defaults to the realm but can be overridden per record.

This task is only applied on primary DCs (skipped when samba_ad_secondary_dc: true).

Note: samba-tool cannot set a custom TTL, so the optional ttl field is validated but not applied — Samba uses its default TTL. Records are created only; to change a record's value, delete it and recreate it.

Generating A records from the Ansible inventory

The role does not create records for inventory hosts on its own. To get an A record for every host in the inventory, add the following pre_tasks to the play that runs this role. It appends one record per host to samba_ad_dns_records before the role processes them:

pre_tasks:
  - name: Create A Records from Ansible inventory
    ansible.builtin.set_fact:
      samba_ad_dns_records: "{{ samba_ad_dns_records + [record] }}"
    vars:
      _name: "{{ (hostvars[item].dns_name | default(item)) | lower }}"
      _realm: "{{ samba_ad_realm | lower }}"
      record:
        name: "{{ _name if (_name == _realm or _name.endswith('.' ~ _realm)) else (_name ~ '.' ~ _realm) }}"
        type: A
        value: "{{ hostvars[item].ansible_host | default(hostvars[item].ansible_default_ipv4.address) }}"
    loop: "{{ groups['all'] }}"
    when: >-
      hostvars[item].ansible_host is defined
      or hostvars[item].ansible_default_ipv4 is defined
    tags: dns

Notes on the behavior:

  • The record name defaults to the host's dns_name (or the inventory hostname if dns_name is unset) and is qualified with samba_ad_realm unless it already ends in the realm.
  • The IP comes from ansible_host, falling back to the host's discovered ansible_default_ipv4.address. Hosts with neither defined are skipped.
  • The dns tag matches the role's DNS tasks, so --tags dns runs both the inventory expansion and the record creation together.

Secondary Domain Controller

The role supports joining an existing AD domain as a secondary (replica) DC. Set samba_ad_secondary_dc: true and provide the primary DC's IP via samba_ad_pdc_ip.

Warning: The variable samba_ad_pdc_ansible_group must match an inventory group that contains the primary DC. This group is used by delegate_to during SYSVOL synchronization to authorize the SSH key on the primary DC. The default value is samba-pdc — either create this group in your inventory or override the variable.

Backup and restore

The primary DC writes a consistent online backup every night (samba-tool domain backup online, run as the DC's machine account) to samba_ad_backup_dir and keeps the newest samba_ad_backup_keep files. Ship that directory off-site (e.g. with restic). The files contain password hashes; the directory is root-only.

Losing one DC needs no backup: rebuild it as a secondary DC (samba_ad_secondary_dc: true, samba_ad_pdc_ip = a running DC); it replicates everything back. If the lost DC held the FSMO roles, seize them on a remaining DC (samba-tool fsmo seize --role=all) first. samba_ad_peer_dcs makes the role refuse to provision a second domain with the same realm when a rebuilt primary is run by mistake.

Losing all DCs: restore the newest backup on a fresh host (packages installed, Samba stopped), as a new DC name:

samba-tool domain backup restore --backup-file=<file> --newservername=<NEW> \
  --targetdir=/var/lib/samba-restore \
  --option="vfs objects = dfs_samba4 acl_xattr xattr_tdb"   # only with xattr_tdb
chmod 0755 /var/lib/samba-restore/state   # restore creates it 0700: users can't read SYSVOL
samba-tool ntacl sysvolreset -s /var/lib/samba-restore/etc/smb.conf
cp /var/lib/samba-restore/private/krb5.conf /etc/krb5.conf
samba -s /var/lib/samba-restore/etc/smb.conf

Then join new secondary DCs to it. Tested in unprivileged LXC: users, groups, GPOs, DNS, Kerberos logins and SYSVOL access of a normal user come back.

Containers (unprivileged LXC)

The role runs in unprivileged Proxmox LXC containers. What it handles itself:

  • /etc/resolv.conf: Proxmox rewrites it on every container start; the role creates /etc/.pve-ignore.resolv.conf and manages the file. Create the container with a working upstream resolver (the package install runs before Samba DNS exists); the primary switches to its own DNS after provisioning.
  • NT ACLs: the kernel refuses security.* extended attributes in a user namespace (EPERM), so the role detects it and stores them via the xattr_tdb VFS module (samba_ad_xattr_tdb: auto). samba-tool ntacl sysvolcheck reads the filesystem directly and reports "No data available" then; access over SMB is correct (samba-tool ntacl get shows the ACLs).

What the container needs (Proxmox side):

  • features: nesting=1 (Debian 13, systemd 257).
  • An ID mapping that covers Samba's IDs (3000000 upwards), otherwise provisioning panics in chown_if_needed: /etc/subuid and /etc/subgid on the node root:100000:4000000, and in the container config lxc.idmap: u 0 100000 4000000 / lxc.idmap: g 0 100000 4000000.
  • Kerberos needs correct clocks: the container uses the node's clock (NTP on the Proxmox node).

Requirements

  • basic-setup role (for custom Ansible facts)
  • community.crypto collection (for the community.crypto.openssh_keypair module used in sysvol sync)

Role variables

See defaults/main/required.yml for required variables See defaults/main/optional.yml for optional variables

Dependencies

  • Debian
    • 13

Example playbooks

- name: Samba AD DC
  hosts: dc
  become: true
  roles:
    - basic-setup
    - samba-ad
  vars:
    samba_ad_domain: "AD"
    samba_ad_realm: "ad.company.local"
    samba_ad_admin_password: "{{ vault_samba_admin_password }}"
    samba_ad_dns_forwarder:
      - "8.8.8.8"
    samba_ad_ous:
      - name: "Users"
      - name: "Admins"
        parent: "OU=Users"
    samba_ad_users:
      - username: "jdoe"
        password: "{{ vault_jdoe_password }}"
        given_name: "John"
        surname: "Doe"
        mail: "jdoe@company.local"
        ou: "OU=Users"
    samba_ad_groups:
      - name: "Developers"
        ou: "OU=Users"
        members:
          - "jdoe"
    samba_ad_dns_zones:
      - name: "internal.company.local"
    samba_ad_dns_records:
      - name: "www.internal.company.local"
        type: A
        value: "192.168.1.10"
        zone: "internal.company.local"