From 6f3911a43332ab02c44a5e057493ddd9657fbd70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rcio=20Fernandes?= Date: Mon, 5 Oct 2026 12:49:00 +0000 Subject: [PATCH] add resolv.conf task --- playbooks/host_maintenance/resolv_conf.yaml | 7 +++++++ roles/host_maintenance/tasks/resolv_conf.yaml | 17 +++++++++++++++++ roles/host_maintenance/templates/resolv.conf.j2 | 17 +++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 playbooks/host_maintenance/resolv_conf.yaml create mode 100644 roles/host_maintenance/tasks/resolv_conf.yaml create mode 100644 roles/host_maintenance/templates/resolv.conf.j2 diff --git a/playbooks/host_maintenance/resolv_conf.yaml b/playbooks/host_maintenance/resolv_conf.yaml new file mode 100644 index 0000000..1716d83 --- /dev/null +++ b/playbooks/host_maintenance/resolv_conf.yaml @@ -0,0 +1,7 @@ +- name: host maintenance - setup resolv.conf + hosts: '{{target|default("all")}}' + become: true + tasks: + - ansible.builtin.include_role: + name: mylimbo.globals.host_maintenance + tasks_from: resolv_conf \ No newline at end of file diff --git a/roles/host_maintenance/tasks/resolv_conf.yaml b/roles/host_maintenance/tasks/resolv_conf.yaml new file mode 100644 index 0000000..05712d8 --- /dev/null +++ b/roles/host_maintenance/tasks/resolv_conf.yaml @@ -0,0 +1,17 @@ +# source https://oneuptime.com/blog/post/2026-02-21-how-to-use-ansible-to-manage-etc-resolv-conf/view +# vars: +# resolv_conf: +# nameservers: +# - x.x.x.x +# search_domains: +# - local.lan +- name: Deploy resolv.conf + ansible.builtin.template: + src: templates/resolv.conf.j2 + dest: /etc/resolv.conf + mode: '0644' + when: "resolv_conf is defined" + +# todo tasks: +# systemd-resolvd:systemctl restart systemd-resolved +# clear cache: resolvectl flush-caches \ No newline at end of file diff --git a/roles/host_maintenance/templates/resolv.conf.j2 b/roles/host_maintenance/templates/resolv.conf.j2 new file mode 100644 index 0000000..81f424f --- /dev/null +++ b/roles/host_maintenance/templates/resolv.conf.j2 @@ -0,0 +1,17 @@ +# templates/resolv.conf.j2 +# Managed by Ansible - DO NOT EDIT MANUALLY +# Changes will be overwritten on next Ansible run + +{% if resolv_conf.nameservers is defined and resolv_conf.nameservers | length > 0 %} +{% for ns in resolv_conf.nameservers %} +nameserver {{ ns }} +{% endfor %} +{% endif %} + +{% if resolv_conf.search_domains is defined and resolv_conf.search_domains | length > 0 %} +search {{ resolv_conf.search_domains | join(' ') }} +{% endif %} + +{% if resolv_conf.resolver_options is defined and resolv_conf.resolver_options | length > 0 %} +options {{ resolv_conf.resolver_options | join(' ') }} +{% endif %} \ No newline at end of file