Get nameservers from resolv.conf with Ruby


I’m a big user of Chef, but was bummed when I couldn’t find any resolver information in Ohai data. I needed to make some changes to an nginx config to use resolver, but wanted to use the resolver that’s already on the host (since nginx doesn’t inherit that for dynamic backends). Anyway, after a lot of doc reading that led me nowhere, I dug into the source of resolv.rb, and tried about 100 different things until I found something that worked:

This will give you an array of all your nameservers in resolv.conf, like:

In Chef, you can easily iterate over this and use as needed. If you want just the nameservers (without the ports), then you can use:

I hope that helps!

Leave a comment

Your email address will not be published. Required fields are marked *