refactor ♻️: Refactored the code to use a more structured approach for DNS configuration and added optional tests.
Updated the role to include optional tests after running it, ensuring that DNS resolution is correctly configured. The code has been refactored to improve readability and maintainability.
This commit is contained in:
@@ -1,4 +1,67 @@
|
||||
---
|
||||
- name: Gather service facts
|
||||
service_facts:
|
||||
|
||||
- name: Determine DNS manager
|
||||
set_fact:
|
||||
dns_manager: >-
|
||||
{% if 'systemd-resolved.service' in ansible_facts.services and ansible_facts.services['systemd-resolved.service'].state == 'running' %}
|
||||
systemd-resolved
|
||||
{% elif 'NetworkManager.service' in ansible_facts.services and ansible_facts.services['NetworkManager.service'].state == 'running' %}
|
||||
NetworkManager
|
||||
{% else %}
|
||||
manual
|
||||
{% endif %}
|
||||
|
||||
- name: Configure DNS for systemd-resolved
|
||||
when: dns_manager == 'systemd-resolved'
|
||||
template:
|
||||
src: resolved.conf.j2
|
||||
dest: /etc/systemd/resolved.conf
|
||||
owner: root
|
||||
group: root
|
||||
mode: '0644'
|
||||
notify: Restart systemd-resolved
|
||||
|
||||
- name: Ensure /etc/resolv.conf points to systemd stub
|
||||
when: dns_manager == 'systemd-resolved'
|
||||
file:
|
||||
src: /run/systemd/resolve/stub-resolv.conf
|
||||
dest: /etc/resolv.conf
|
||||
state: link
|
||||
force: true
|
||||
|
||||
- name: Configure DNS via NetworkManager (nmcli)
|
||||
when: dns_manager == 'NetworkManager'
|
||||
block:
|
||||
- name: Set DNS servers with nmcli
|
||||
shell: >
|
||||
nmcli con mod "{{ nm_connection_name }}"
|
||||
ipv4.dns "{{ dns_servers | join(' ') }}"
|
||||
ipv4.ignore-auto-dns yes
|
||||
args:
|
||||
warn: false
|
||||
|
||||
- name: Set search domain with nmcli
|
||||
shell: >
|
||||
nmcli con mod "{{ nm_connection_name }}"
|
||||
ipv4.dns-search "{{ dns_search }}"
|
||||
args:
|
||||
warn: false
|
||||
|
||||
- name: Bring connection down and up to apply changes
|
||||
shell: >
|
||||
nmcli con down "{{ nm_connection_name }}" && nmcli con up "{{ nm_connection_name }}"
|
||||
ignore_errors: true
|
||||
|
||||
when: dns_manager == 'manual'
|
||||
template:
|
||||
src: resolv.conf.j2
|
||||
dest: /etc/resolv.conf
|
||||
owner: root
|
||||
group: root
|
||||
mode: '0644'
|
||||
|
||||
- name: Install required packages
|
||||
apt:
|
||||
name:
|
||||
@@ -10,6 +73,15 @@
|
||||
state: present
|
||||
become: yes
|
||||
|
||||
- name: Configure /etc/resolv.conf for AD DNS resolution
|
||||
template:
|
||||
src: resolv.conf.j2
|
||||
dest: /etc/resolv.conf
|
||||
owner: root
|
||||
group: root
|
||||
mode: '0644'
|
||||
notify: Restart networking if required
|
||||
|
||||
- name: Configure Kerberos
|
||||
template:
|
||||
src: krb5.conf.j2
|
||||
|
||||
Reference in New Issue
Block a user