Update /etc/resolv.conf file

Greetings Pro’s

we have like 300+ Linux Redhat machines running on AWS. we have a requirement to update New Entries for /etc/resolv.conf with new DNS entries by deleting the old ones is it possible to perform this job using chef…

I tried using hosts-file cookbook from chef-supermarket to update /etc/hosts and it was successfully completed , is there any quickbook available for this task? or do i need to update manually by deleting old entries and add the new ones?

below is example for my requirement

Vi /etc/resolv.conf

BEFORE
generated by /sbin/dhclient-script
nameserver 10…x.x.x.1
nameserver 10.x.x.x.2
nameserver 10.x.108.x.

AFTER
[root@abc .ssh]# cat /etc/resolv.conf
; generated by /sbin/dhclient-script
nameserver 10.0.x.A
nameserver 10.0.x.B
nameserver 10.x.108.x

Appreciate for quick response

Hey prash,

I have exactly this problem, however this isn’t a Chef solvable problem really. This is best handled with cloud-init.

What I’ve done is add resolv_conf to the list of cloud_init_modules at the bottom of the list, to ensure that DHCP has enough time to finish it’s execution, and make /etc/resolv.conf immutable. We use the following cloud init directives:

manage_resolv_conf = true
resolv_conf:
  nameservers:
    - 10.x.x.a
    - 10.x.x.b
    - 10.x.x.c
  searchdomains:
    - domain.tld
    - ec2.internal
  domain: domain.tld
  options:
    timeout: 1

runcmd:
  - [ cloud-init-per, once, resolv_chattr, chattr, +i, /etc/resolv.conf ]

Of course you can use what you feel is best, however if your chef server is internal and it relies on internal DNS to access it, you’ll lose access upon network restart or VM reboot.

This of course requires a cloud-init enabled AMI. YMMV depending on if you’re using Debian or Red Hat family OSes, however the above works for me as a “let cloud-init set up what’s right, and prevent /etc/resolv.conf from being changed later”.

If you need to change the DNS later, you’d have to make the file mutable, make the change, then make it immutable again.

To run this user-data at-time, you’d be manually invoking the change with a mutable file. You can run the resolv_conf module specifically with cloud-init single -n resolv_conf --frequency always. Be sure though to make that /etc/resolv.conf file immutable otherwise DHCP will un-do your changes.

Not sure if you have tried to use [resolvconf cookbook](https://supermarket.chef.io/cookbooks/resolvconf) from supermarket.