# Read DNS server configured on Linux and Windows hosts

**URL:** <https://forum.ansible.com/t/read-dns-server-configured-on-linux-and-windows-hosts/6514>\
**Category:** Get Help\
**Tags:** windows\
**Created:** [June 14, 2024, 12:35pm UTC](https://forum.ansible.com/t/read-dns-server-configured-on-linux-and-windows-hosts/6514 "2024-06-14T12:35:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matteo](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@Matteo](https://forum.ansible.com/u/Matteo)\
**Post date:** [June 14, 2024, 12:35pm UTC](https://forum.ansible.com/t/read-dns-server-configured-on-linux-and-windows-hosts/6514/1 "2024-06-14T12:35:22Z")

</div>

Hi everyone, I’m starting to use Ansible and I’d like to make a workbook to read the DNS configuration on Linux and Windows virtual machines, with a view to then modifying them. Is there something like that, I guess yes, without having to go and read e.g. /etc/resolv.conf and run an ifconfig?

Thank you,  
Matteo

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [June 14, 2024, 12:42pm UTC](https://forum.ansible.com/t/read-dns-server-configured-on-linux-and-windows-hosts/6514/2 "2024-06-14T12:42:15Z")

</div>

I’ve not used Windows for 20+ years so can’t help with that aspect of this question…

You can’t assume that `/etc/resolv.conf` is used on Linux these days, [systemd](https://systemd.io/) might have replaced it with a symlink:

```bash
ls -lah /etc/resolv.conf 
lrwxrwxrwx 1 root root 39 Jul 24 2023 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

```

Also `ifconfig` has been replaced on many (most?) distros with `ip addr`:

```bash
ifconfig
-bash: ifconfig: command not found

```

On Debian and Ubuntu servers I install the `systemd-resolved` package and generate a `/etc/systemd/resolved.conf.d/resolved.conf` file [using Ansible](https://git.coop/webarch/systemd) and it generaly contains something like this:

```auto
# Ansible managed

[Resolve]
DNS=9.9.9.9 8.8.8.8

# vim: syntax=systemd

```

---

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [June 15, 2024, 2:50pm UTC](https://forum.ansible.com/t/read-dns-server-configured-on-linux-and-windows-hosts/6514/3 "2024-06-15T14:50:16Z")

</div>

The `setup` module (gather facts) provides an `ansible_dns` dict with information gleaned from `/etc/resolv.conf` on Linux/Unix systems.

```json
"ansible_dns": {
    "nameservers": [
        "192.168.1.82",
        "192.168.1.1"
    ],
    "search": [
        "ww.example.net"
    ]
}

```

I forget whether that is reported for Windows as well.

You might be able to use that information to decide whether you need/want to replace anything with, say, a template.

---

<div class="post-metadata">

**Author:** ![Matteo](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@Matteo](https://forum.ansible.com/u/Matteo)\
**Post date:** [June 24, 2024, 4:05pm UTC](https://forum.ansible.com/t/read-dns-server-configured-on-linux-and-windows-hosts/6514/4 "2024-06-24T16:05:49Z")

</div>

Thanks Jan-Piet, that was exactly what I needed and it works great with Linux hosts.

Documentation says it should work also with windows hosts, but I only get an empty string.

I search for other modules, but without success, except “ansible.windows.win\_dns\_client”, but it seems it can only set DNS, not read them: is it possible that no one else has ever had this need? 🙂

```auto
---
- hosts: all
  gather_facts: false
  tasks:
    - name: Get all DNS
      ansible.builtin.setup:
        gather_subset:
          - '!all'
          - '!any'
          - dns

    - name: Run several insert queries against db test_db in single transaction
      vars:
        dns1: "{{ ansible_facts.dns.nameservers[0] | default('') }}"
        dns2: "{{ ansible_facts.dns.nameservers[1] | default('') }}"
        domain: "{{ ansible_facts.dns.domain | default('') }}"
        os: "{{ ansible_facts['lsb']['description'] | default('') }}"
      community.mysql.mysql_query:
        login_host: ***
        login_db: ***
        login_user: ***
        login_password: ***
        query:
          - INSERT INTO DNS (DNSId, Host, DNS1, DNS2, Domain, Timestamp, OS) VALUES (null, "{{ inventory_hostname }}", "{{ dns1 }}", "{{ dns2 }}", "{{ domain }}", NOW(), "{{ os }}");
      delegate_to: 127.0.0.1

```

Matteo
