How to add resolvers via ansible on Ubuntu 16.04

ansiblenetworkingubuntu-16.04

I'm starting on some Ubuntu 14.04 upgrades and creating some Ansible playbooks along the way to be used on several other hosts later on. First off is setting up /etc/resolv.conf to point to local DNS resources.

The old method of provisioning /etc/resolv.conf was a shell script with awk/sed/grep which ran over ssh which edited the file directly. This seems to be a no-no in 16.04 which warns against this with:

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
.

The man page for resolvconf mentions adding dns-nameservers into /etc/network/interfaces and also a note discouraging the use of resolvconf to add nameserver info by hand:

The administrator can run resolvconf from the command line...but this is not normally necessary or advisable.

It seems the only option left is to edit /etc/network/interfaces but how do I specify the network interface in the playbook? It's not the standard eth0..1..2 but some mix of alphabet soup like enp6s0 or ens18 which I don't quite get. On an different upgrade, I was able to override this odd naming schemed by adding biosdevname=0 to grub, but this does not seem to have any effect on this new host even after running update-grub and rebooting.

Is there some way to tell ansible to find the primary nic and add a nameserver line in /etc/network/interfaces for these hosts? My current non-working playbook is below:

(updated working example from accepted answer)

- hosts: all
  tasks:
    - name: setup resolv.conf in DMZ9 for Ubuntu 16.04 hosts
      when: ansible_default_ipv4.address is match("192.1.9")
      when: ansible_distribution_release is match ("xenial")
      interfaces_file:
        iface: "{{ ansible_default_ipv4['interface'] }}" 
        option: dns-nameservers
        value: 192.1.9.4 192.1.9.10

Best Answer

ansible_default_ipv4['interface'] contains the name of the interface. I find running

ansible all -i localhost, -m setup -c local

helpful to determine what variables ansible defines.