2025-10-19 22:25:19 +02:00
---
# Disable tools, such as resolvconf, that automatically update your /etc/resolv.conf DNS resolver configuration file
- name : Stop and disable systemd-resolved if present
ansible.builtin.systemd :
name : systemd-resolved
enabled : false
state : stopped
when : ansible_facts.services['systemd-resolved.service'] is defined
- name : Remove /etc/resolv.conf if it's a symlink to systemd-resolved
ansible.builtin.file :
path : /etc/resolv.conf
state : absent
2025-11-04 20:21:02 +01:00
when : "'/run/systemd/resolve' in ansible_facts.lsb.description | default('')"
2025-10-19 22:25:19 +02:00
- name : Create static /etc/resolv.conf
ansible.builtin.copy :
dest : /etc/resolv.conf
content : |
nameserver {{ location_internal_dns }}
nameserver {{ location_external_dns }}
owner : root
group : root
mode : '0644'
- name : Disable resolvconf package (if installed)
ansible.builtin.package :
name : resolvconf
state : absent
- name : Disable DNS updates from NetworkManager (if present)
ansible.builtin.blockinfile :
path : /etc/NetworkManager/NetworkManager.conf
block : |
[ main]
dns=none
notify : Restart NetworkManager
when : ansible_facts.services['NetworkManager.service'] is defined
- name : Prevent dhclient from modifying resolv.conf (if present)
ansible.builtin.lineinfile :
path : /etc/dhcp/dhclient.conf
regexp : '^#?supersede domain-name-servers'
line : 'supersede domain-name-servers {{ location_internal_dns }}, {{ location_external_dns }};'
create : yes
# Verify that the /etc/hosts file on the DC correctly resolves the fully-qualified domain name (FQDN) and short host name to the LAN IP address of the DC
- name : Set /etc/hosts entry for Samba AD DC
ansible.builtin.lineinfile :
path : /etc/hosts
2025-11-04 19:22:07 +01:00
line : '{{ addc_ansible_host }} {{ addc_hostname | upper }}.{{ addc_tld | lower }} {{ addc_hostname | upper }}'
2025-10-19 22:25:19 +02:00
state : present
create : yes
- name : Ensure '127.0.0.1 localhost' is present and nothing else on that line
ansible.builtin.lineinfile :
path : /etc/hosts
regexp : '^127\.0\.0\.1\s+'
line : '127.0.0.1 localhost'
state : present
# Remove any existing smb.conf file
- name : Get compiled default smb.conf path from smbd
ansible.builtin.shell : smbd -b | grep CONFIGFILE | awk '{print $2}'
register : smb_conf_path
changed_when : false
failed_when : smb_conf_path.rc != 0
- name : Remove smb.conf using discovered path
ansible.builtin.file :
2025-11-04 19:22:07 +01:00
path : '{{ smb_conf_path.stdout }}'
2025-10-19 22:25:19 +02:00
state : absent
# Remove all Samba database files, such as *.tdb and *.ldb files
- name : Get Samba directories from smbd -b
2025-11-04 19:22:07 +01:00
ansible.builtin.shell : smbd -b | egrep 'LOCKDIR|STATEDIR|CACHEDIR|PRIVATE_DIR' | awk '{print $2}'
2025-10-19 22:25:19 +02:00
register : samba_dirs
changed_when : false
failed_when : samba_dirs.rc != 0
- name : Filter existing directories
ansible.builtin.find :
2025-11-04 19:22:07 +01:00
paths : '{{ item }}'
2025-10-19 22:25:19 +02:00
file_type : directory
recurse : no
2025-11-04 19:22:07 +01:00
loop : '{{ samba_dirs.stdout_lines }}'
2025-10-19 22:25:19 +02:00
register : existing_dirs
- name : Collect existing directories
ansible.builtin.set_fact :
2025-11-04 20:21:02 +01:00
valid_dirs : "{{ existing_dirs.results | selectattr('matched', '>', 0) | map(attribute='files') | sum(start=[]) | map(attribute='path') | list }}"
2025-10-19 22:25:19 +02:00
- name : Find *.tdb and *.ldb files
ansible.builtin.find :
2025-11-04 19:22:07 +01:00
paths : '{{ item }}'
patterns : '*.tdb,*.ldb'
2025-10-19 22:25:19 +02:00
recurse : yes
use_regex : false
2025-11-04 19:22:07 +01:00
loop : '{{ valid_dirs }}'
2025-10-19 22:25:19 +02:00
register : db_files
- name : Remove found tdb/ldb files
ansible.builtin.file :
2025-11-04 19:22:07 +01:00
path : '{{ item.path }}'
2025-10-19 22:25:19 +02:00
state : absent
2025-11-04 20:21:02 +01:00
loop : "{{ db_files.results | map(attribute='files') | sum(start=[]) }}"
2025-10-19 22:25:19 +02:00
when : item.path is defined
- name : Report removed files
ansible.builtin.debug :
2025-11-04 19:22:07 +01:00
msg: 'Removed : {{ item.path }}'
2025-11-04 20:21:02 +01:00
loop : "{{ db_files.results | map(attribute='files') | sum(start=[]) }}"