From 7b4a8ed76361188673e37997bf0bf73b46b7855c Mon Sep 17 00:00:00 2001
From: Thomas Gambier <thomas.gambier@nexedi.com>
Date: Tue, 14 Dec 2021 13:23:52 +0100
Subject: [PATCH] Configure systemd-resolved when present

---
 playbook/roles/vm-bootstrap/handlers/main.yml     |  3 +++
 playbook/roles/vm-bootstrap/tasks/hostname.yml    | 15 ++++-----------
 playbook/roles/vm-bootstrap/tasks/resolv_conf.yml | 12 ++++++++++++
 .../vm-bootstrap/tasks/resolv_conf_systemd.yml    |  9 +++++++++
 4 files changed, 28 insertions(+), 11 deletions(-)
 create mode 100644 playbook/roles/vm-bootstrap/tasks/resolv_conf.yml
 create mode 100644 playbook/roles/vm-bootstrap/tasks/resolv_conf_systemd.yml

diff --git a/playbook/roles/vm-bootstrap/handlers/main.yml b/playbook/roles/vm-bootstrap/handlers/main.yml
index 17fb4ef..1c92009 100644
--- a/playbook/roles/vm-bootstrap/handlers/main.yml
+++ b/playbook/roles/vm-bootstrap/handlers/main.yml
@@ -7,3 +7,6 @@
   - name: restart sshd
     service: name=sshd state=restarted
     when: ansible_os_family == "RedHat"
+
+  - name: restart systemd-resolved
+    service: name=systemd-resolved state=restarted
diff --git a/playbook/roles/vm-bootstrap/tasks/hostname.yml b/playbook/roles/vm-bootstrap/tasks/hostname.yml
index 5ee8aec..de780d7 100644
--- a/playbook/roles/vm-bootstrap/tasks/hostname.yml
+++ b/playbook/roles/vm-bootstrap/tasks/hostname.yml
@@ -53,18 +53,11 @@
     script: format_hosts /tmp/hosts tl.teralab-datascience.fr 
     when: cluster_hash.stat.exists == True
 
-  - name: managing resolv.conf file
-    lineinfile: dest=/etc/resolv.conf line="search tl.teralab-datascience.fr"
+  - include: resolv_conf.yml
+    when: ansible_distribution != "Ubuntu" or ansible_distribution_major_version|int < 18
 
-  - name: adding entry from workspace
-    lineinfile: dest=/etc/resolv.conf line="nameserver 10.200.218.1"
-
-  - name: Delete default nameserver
-    lineinfile: dest=/etc/resolv.conf line="nameserver 10.0.2.3" state=absent
-
-  - name: Delete nameserver 8.8.8.8
-    lineinfile: dest=/etc/resolv.conf line="nameserver 8.8.8.8" state=absent
-    when: is_playbook_ok in [False, "False"]
+  - include: resolv_conf_systemd.yml
+    when: ansible_distribution == "Ubuntu" and ansible_distribution_major_version|int >= 18
 
   - name: Turn off dhclient if it's running
     shell: pkill dhclient
diff --git a/playbook/roles/vm-bootstrap/tasks/resolv_conf.yml b/playbook/roles/vm-bootstrap/tasks/resolv_conf.yml
new file mode 100644
index 0000000..3a88021
--- /dev/null
+++ b/playbook/roles/vm-bootstrap/tasks/resolv_conf.yml
@@ -0,0 +1,12 @@
+  - name: managing resolv.conf file
+    lineinfile: dest=/etc/resolv.conf line="search tl.teralab-datascience.fr"
+
+  - name: adding entry from workspace
+    lineinfile: dest=/etc/resolv.conf line="nameserver 10.200.218.1"
+
+  - name: Delete default nameserver
+    lineinfile: dest=/etc/resolv.conf line="nameserver 10.0.2.3" state=absent
+
+  - name: Delete nameserver 8.8.8.8
+    lineinfile: dest=/etc/resolv.conf line="nameserver 8.8.8.8" state=absent
+    when: is_playbook_ok in [False, "False"]
diff --git a/playbook/roles/vm-bootstrap/tasks/resolv_conf_systemd.yml b/playbook/roles/vm-bootstrap/tasks/resolv_conf_systemd.yml
new file mode 100644
index 0000000..a889423
--- /dev/null
+++ b/playbook/roles/vm-bootstrap/tasks/resolv_conf_systemd.yml
@@ -0,0 +1,9 @@
+  - name: configuring systemd-resolved (add search)
+    lineinfile: dest=/etc/systemd/resolved.conf line="Domains=tl.teralab-datascience.fr"
+
+  - name: configuring systemd-resolved (add DNS)
+    lineinfile: dest=/etc/systemd/resolved.conf line="DNS=10.200.218.1"
+
+  - name: configuring systemd-resolved (no FallbackDNS)
+    lineinfile: dest=/etc/systemd/resolved.conf line="FallbackDNS="
+    notify: restart systemd-resolved
-- 
2.30.9