- name: Check if DNS is already configured stat: path: /etc/resolv.conf register: resolv_conf - name: Read /etc/resolv.conf slurp: path: /etc/resolv.conf register: resolv_conf_content when: resolv_conf.stat.exists - name: Check if DNS servers are already present set_fact: dns_configured: "{{ resolv_conf_content['content'] | b64decode | regex_search('^nameserver') is not none }}" when: resolv_conf.stat.exists - name: Apply resolv.conf template only if DNS is not configured template: src: templates/resolv.conf dest: /etc/resolv.conf owner: root group: root mode: u=rw,g=r,o=r backup: true when: not resolv_conf.stat.exists or not dns_configured