4

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
intelfx
  • 134
  • 7
Server Fault
  • 3,454
  • 7
  • 48
  • 88
  • You probably want to look at this question about how to disable interfaces naming scheme https://askubuntu.com/questions/767786/changing-network-interfaces-name-ubuntu-16-04 – AlexD May 11 '18 at 16:17
  • The network interface names are available in the `ansible_interfaces` fact. Keep in mind that `lo` is also listed in that fact. – Michael Hampton May 11 '18 at 16:26
  • @AlexD Thanks. I added `net.ifnames=0` to grub and now I have `eth0` back. – Server Fault May 11 '18 at 17:46

2 Answers2

1

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.

Mark Wagner
  • 17,764
  • 2
  • 30
  • 47
  • So like this?: `iface: "{{ ansible_default_ipv4['interface'] }}"`. I've updated the playbook in my question with this. – Server Fault May 11 '18 at 20:16
0

Just to answer the playbook part of the question for anyone else stumbling through this -- I ended up removing the resolvconf package by hand and was able to forego any twiddling with udev rules. The added net.ifnames=0 boot arg is puzzling, but apparently necessary, in some contexts, for ethX nic naming. I have other 16.04 hosts which were setup months back which do not require this boot arg so something else is apparently involved.

Server Fault
  • 3,454
  • 7
  • 48
  • 88