Close Menu
DPC Virtual Tips
    Read More

    Configure vCenter File-Based Backups to NFS: Practical Lab Guide

    September 11, 2026

    Creating Your First Ansible Playbook: A Practical Lab Guide

    September 10, 2026

    Linux Commands to Investigate High Disk Partition Usage

    September 9, 2026
    • Home
    • About Us
    • Contact
    • Cookie Policy
    • Comment Policy
    • Privacy Policy
    • Terms of Use
    Monday, September 14
    DPC Virtual Tips
    • Home
    • Linux & Automation
    • HPC & Slurm
    • VMware & Virtualization
    • About Us
    • Contact
    DPC Virtual Tips
    Home » Creating an Ansible Playbook for Keeping DNS Client Settings
    Linux & Automation

    Creating an Ansible Playbook for Keeping DNS Client Settings

    By Danilo ChiacchioAugust 6, 20266 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Creating an Ansible Playbook for Keeping DNS Client Settings
    Creating an Ansible Playbook for Keeping DNS Client Settings
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Creating an Ansible DNS playbook is a practical way to automate DNS client configuration across multiple Linux servers. Maintaining consistent DNS settings helps ensure reliable network communication and simplifies administration in environments with many managed hosts.

    On Red Hat Enterprise Linux systems, NetworkManager is responsible for managing network connections and can dynamically update DNS settings in the /etc/resolv.conf file. By using Ansible, administrators can define the desired DNS configuration and apply it consistently across all managed nodes.

    In this guide, we will create an Ansible playbook to configure DNS settings through NetworkManager. You will learn how to build the inventory, define the playbook tasks, apply DNS changes, and validate the final configuration on managed servers.

    If you are new to Ansible, start with “Creating Your First Ansible Playbook: A Practical Lab Guide“ to review inventory, SSH connectivity, playbook execution, and validation before continuing.

    By default, NetworkManager dynamically updates the /etc/resolv.conf file with the DNS settings from the active NetworkManager connection profiles

    What is NetworkManager?

    In some Linux distributions, the default networking service is provided by NetworkManager, which is a dynamic network control and configuration daemon to keep network devices and connections up and active when they are available. The traditional ifcfg type configuration files are still supported.

    NetworkManager is installed by default on Red Hat Enterprise Linux. So, in our case, since we’re using a Linux distribution that has it by default, we’ll not install anything to use NetworkManager.

    So, let’s get started and create an Ansible playbook to keep the DNS configuration consistent on all hosts.

    Creating the Inventory

    Before going forward, our environment is composed of:

    • One Control VM (A CentOS 9 VM that runs Ansible).
    • Six Managed VMs (Red Hat 8 VMs that Ansible manages).

    So, Ansible needs to know which hosts to handle. Let’s create a directory to store the inventory file:

    mkdir -p /root/ansible/inventory

    Create the hosts.ini file:

    touch /root/ansible/inventory/hosts.ini

    And then, add the hosts to hosts.ini. In this case, for instance, our inventory file has some groups

    [hpc2_login_nodes]    # This is a group
    hpc2-login
    
    [hpc2_head_nodes]     # This is a group
    hpc2-head
    
    [hpc2_compute_nodes]   # This is a group
    hpc2-node[01:06]
    
    [all:vars]             # This is a variable section
    ansible_user=root      # This is a variable applies to all hosts

    Creating the Playbook

    This playbook uses the community.general.nmcli module. Verify that the collection is available on the Ansible control node:

    ansible-galaxy collection list community.general

    If it is not installed:

    ansible-galaxy collection install community.general

    Let’s create a directory to store the playbook files:

    mkdir -p /root/ansible/playbooks

    Inside the playbooks directory, generate the playbook file dns.yml:

    touch /root/ansible/playbooks/dns.yml

    Before using the playbook, identify the active NetworkManager connection profile associated with the interface:

    nmcli -t -f NAME,DEVICE connection show --active

    In this lab, the connection profile and interface are both named ens192. If your connection profile uses a different name, replace ens192 in conn_name with the actual profile name.

    And then, add the following content:

    ---
    - name: Ensure DNS settings using NetworkManager
      hosts: all
      become: yes
      tasks:
        - name: Set DNS on interface
          community.general.nmcli:
            conn_name: "ens192"
            type: ethernet
            dns4:
              - "192.168.255.3"
            dns4_search:
              - "lab.local"
            state: present
          notify: Reactivate connection to apply DNS changes
    
      handlers:
        - name: Reactivate connection to apply DNS changes
          command: nmcli connection up ens192

    Notes:

    • In our case, for instance, all managed nodes have an interface “ens192”.
    • Our DNS server IP is “192.168.255.3”.
    • Our DNS domain name is “lab.local”.

    Important: Reactivating a NetworkManager connection can briefly interrupt network connectivity. When changing the same interface used by Ansible over SSH, test the procedure in a lab before applying it to production systems.

    Running the Playbook

    To run the playbook:

    ansible-playbook -i inventory/hosts.ini playbooks/dns.yml

    Output’s example – Considering that all managed hosts are in the desired state (no change):

    PLAY [Ensure DNS settings using NetworkManager] *****************************************************************************************************************************
    
    TASK [Gathering Facts] ******************************************************************************************************************************************************
    ok: [hpc2-login]
    ok: [hpc2-node03]
    ok: [hpc2-node01]
    ok: [hpc2-node02]
    ok: [hpc2-head]
    ok: [hpc2-node04]
    ok: [hpc2-node05]
    ok: [hpc2-node06]
    
    TASK [Set DNS on interface] *************************************************************************************************************************************************
    ok: [hpc2-head]
    ok: [hpc2-login]
    ok: [hpc2-node01]
    ok: [hpc2-node02]
    ok: [hpc2-node03]
    ok: [hpc2-node04]
    ok: [hpc2-node05]
    ok: [hpc2-node06]
    
    PLAY RECAP ******************************************************************************************************************************************************************
    hpc2-head                  :ok=2 changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    hpc2-login                 :ok=2 changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    hpc2-node01                :ok=2 changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    hpc2-node02                :ok=2 changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    hpc2-node03                :ok=2 changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    hpc2-node04                :ok=2 changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    hpc2-node05                :ok=2 changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    hpc2-node06                : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

    If we access one managed node and inspect the DNS configuration, we can confirm that it is correct:

    [root@hpc2-node01 ~]# cat /etc/resolv.conf
    # Generated by NetworkManager
    search lab.local
    nameserver 192.168.255.3

    Supposing that you have a demand to change the DNS IP from 192.168.255.3 to 10.10.10.10 on all managed hosts, you just need to change the IP in the Playbook and rerun it:

    PLAY [Ensure DNS settings using NetworkManager] *****************************************************************************************************************************
    
    TASK [Gathering Facts] ******************************************************************************************************************************************************
    ok: [hpc2-node02]
    ok: [hpc2-login]
    ok: [hpc2-node01]
    ok: [hpc2-node03]
    ok: [hpc2-head]
    ok: [hpc2-node04]
    ok: [hpc2-node06]
    ok: [hpc2-node05]
    
    TASK [Set DNS on interface] *************************************************************************************************************************************************
    changed: [hpc2-node02]
    changed: [hpc2-node01]
    changed: [hpc2-node03]
    changed: [hpc2-login]
    changed: [hpc2-head]
    changed: [hpc2-node04]
    changed: [hpc2-node05]
    changed: [hpc2-node06]
    
    RUNNING HANDLER [Reactivate connection to apply DNS changes] ****************************************************************************************************************
    changed: [hpc2-login]
    changed: [hpc2-node01]
    changed: [hpc2-head]
    changed: [hpc2-node02]
    changed: [hpc2-node03]
    changed: [hpc2-node04]
    changed: [hpc2-node05]
    changed: [hpc2-node06]
    
    PLAY RECAP ******************************************************************************************************************************************************************
    hpc2-head                  :ok=3changed=2 unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    hpc2-login                 :ok=3changed=2 unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    hpc2-node01                :ok=3changed=2 unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    hpc2-node02                :ok=3changed=2 unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    hpc2-node03                :ok=3changed=2 unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    hpc2-node04                :ok=3changed=2 unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    hpc2-node05                :ok=3changed=2 unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    hpc2-node06                :ok=3changed=2 unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

    And inspecting the managed host again, we can confirm the change:

    [root@hpc2-node01 ~]# cat /etc/resolv.conf
    # Generated by NetworkManager
    search lab.local
    nameserver 10.10.10.10

    External References

    • Ansible community.general.nmcli Module Official Ansible documentation for managing NetworkManager connection profiles, interfaces, DNS servers, search domains, automatic DNS behavior, and connection state with community.general.nmcli.
    • Ansible Community General Collection Official reference for the community.general collection that provides the nmcli module used by this playbook.
    • NetworkManager nmcli Settings Reference Official NetworkManager reference for connection-profile properties including ipv4.dns, ipv4.dns-search, and ipv4.ignore-auto-dns.
    • NetworkManager Reference Manual Official documentation covering NetworkManager architecture, connection profiles, devices, activation, DNS configuration, and network-management behavior on Linux systems.
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow to Change the vCenter Server FQDN/PNID Safely
    Next Article vSphere MTU Mismatch: How VDS and VMkernel Settings Can Break vSAN Connectivity
    Danilo Chiacchio
    • LinkedIn

    Infrastructure Engineer with hands-on experience in virtualization, Linux, Windows Server, and enterprise infrastructure troubleshooting. I work with real-world infrastructure environments and technical labs, focusing on diagnosing problems, understanding root causes, and documenting practical solutions. DPC Virtual Tips was created to share hands-on troubleshooting guides, lab experiences, technical procedures, and lessons learned while working with technologies such as VMware, Linux, HPC/Slurm, networking, storage, and infrastructure automation with Python.

    Related Posts

    Creating Your First Ansible Playbook: A Practical Lab Guide

    September 10, 2026

    Linux Commands to Investigate High Disk Partition Usage

    September 9, 2026

    How to Investigate TCP Retransmissions on Linux

    September 3, 2026

    Comments are closed.

    Search
    Categories
    • HPC & Slurm (11)
    • Linux & Automation (12)
    • VMware & Virtualization (16)
    Read More
    VMware & Virtualization

    Configure vCenter File-Based Backups to NFS: Practical Lab Guide

    By Danilo ChiacchioSeptember 11, 202610 Mins Read
    Linux & Automation

    Creating Your First Ansible Playbook: A Practical Lab Guide

    By Danilo ChiacchioSeptember 10, 202610 Mins Read
    Linux & Automation

    Linux Commands to Investigate High Disk Partition Usage

    By Danilo ChiacchioSeptember 9, 20267 Mins Read
    HPC & Slurm

    How to Investigate Jobs Stuck in COMPLETING State on Slurm

    By Danilo ChiacchioSeptember 8, 202612 Mins Read
    VMware & Virtualization

    Restoring vCenter Server from a File-Based Backup: Practical Lab Walkthrough

    By Danilo ChiacchioSeptember 7, 20269 Mins Read
    Latest Posts

    Configure vCenter File-Based Backups to NFS: Practical Lab Guide

    September 11, 2026

    Creating Your First Ansible Playbook: A Practical Lab Guide

    September 10, 2026

    Linux Commands to Investigate High Disk Partition Usage

    September 9, 2026
    Images from Gallery
    hpc main commands
    linux commands
    install rock linux
    lustre fs
    shell scripting
    vSAN Trace Files
    Categories
    • HPC & Slurm
    • Linux & Automation
    • VMware & Virtualization
    • Home
    • About Us
    • Contact
    • Cookie Policy
    • Comment Policy
    • Privacy Policy
    • Terms of Use
    Copyright © 2026, DPC Virtual Tips. All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.

    We use cookies to improve your browsing experience, analyze website traffic, and display relevant advertising. You can accept all cookies or manage your preferences at any time.