Using cloud-init to change resolv.conf

GAURAV GARG picture GAURAV GARG · Feb 11, 2018 · Viewed 7.4k times · Source

I want my setup of openstack to work such that when I boot a new instance, 8.8.8.8 should be added to dns-nameservers.

This is my old /etc/resolv.conf (in the new VM which was spawned in openstack)-

nameserver 10.0.0.2
search openstacklocal

And this is the new resolv.conf that I want -

nameserver 8.8.8.8
nameserver 10.0.0.2
search openstacklocal

I followed this tutorial, and I have added the necessary info. of resolv conf to my config file(/etc/cloud/cloud.cfg) of cloud-init -

manage_resolv_conf: true

resolv_conf:
  nameservers: ['8.8.4.4', '8.8.8.8']
  searchdomains:
    - foo.example.com
    - bar.example.com
  domain: example.com
  options:
    rotate: true
    timeout: 1

These changes are made in /etc/cloud/cloud.cfg file of the openstack host. However, the changes don't seem to get reflected.

Any suggestions?

Answer

nicolasochem picture nicolasochem · Sep 26, 2018

It will not work because cloud-init networking configuration occurs too early in the boot process.

See cloud-init stages: https://cloudinit.readthedocs.io/en/latest/topics/boot.html

Network configuration is done in the "Local" stage, but the user-data from Openstack is only downloaded at the "Config" stage after network is up. At this stage, the network configuration is ignored.

Instead, you need to edit networking files then bring interfaces up by passing commands to cloud-init with runcmd.