Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

network config grains: DNS servers #26860

Closed
notpeter opened this issue Sep 2, 2015 · 5 comments
Closed

network config grains: DNS servers #26860

notpeter opened this issue Sep 2, 2015 · 5 comments
Labels
Feature new functionality including changes to functionality and code refactors, etc. Grains P3 Priority 3 Platform Relates to OS, containers, platform-based utilities like FS, system based apps
Milestone

Comments

@notpeter
Copy link
Contributor

notpeter commented Sep 2, 2015

It'd be really nice if salt exposed a dns_server grain, maybe with the simple form of:

dns_servers:
  - 10.0.0.2
  - 8.8.8.8

On unixes this this can be sourced from /etc/resolv.conf, with one line per name server with one of these two variants:
nameserver 8.8.8.8
nameserver 2001:4860:4860::8844 (still quite rare, even in ipv6 deployments)

On windows the FIXED_INFO struct returned from GetNetworkParams includes the list of currently configured DNS servers.

@notpeter notpeter changed the title network config grains: DNS servers and more network config grains: DNS servers Sep 2, 2015
@GreatSnoopy
Copy link

You can write your own grain to do that. Start for example, from :

#!/usr/bin/env python

'''
    Very basic grain to return dns configuration.
    It only works in *nix
    It does not yet work with "new style" desktop configurations that use a local instance of
    dnsmasq (it will return 127.0.0.1 as the nameserver)
'''

from __future__ import absolute_import
import re
import salt.utils




def dns_configuration():
    grains={}
    if salt.utils.is_windows():
        return grains
    else:
        try:
            with salt.utils.fopen('/etc/resolv.conf', 'r') as _fh:
                relevant_lines=[ l.strip() for l in _fh.readlines() if not  re.match('^\s*#.*$',l) ]
                dns_servers=[ matches.group(1)  for matches in  [ re.match('^\s*nameserver\s+([.\w]+).*',s) for s in relevant_lines ] if matches ]
                search_domains_lines=[l for l in relevant_lines if re.match('^\s*search\s+\w+.*',l)]
                if len(search_domains_lines) > 0:
                    search_domains=re.split('\s+',search_domains_lines[-1:][0])[1:]
                else:
                    search_domains=[]
        except IOError:
            return grains
    grains['dns_servers']=dns_servers
    grains['dns_search_domains']=search_domains
    return grains

@bersace
Copy link
Contributor

bersace commented Sep 3, 2015

👍 for resolv.conf in grains :-)

@justinta justinta added Feature new functionality including changes to functionality and code refactors, etc. Grains Platform Relates to OS, containers, platform-based utilities like FS, system based apps P3 Priority 3 labels Sep 3, 2015
@justinta
Copy link

justinta commented Sep 3, 2015

@notpeter Thanks for the feature request!

@justinta justinta added this to the Approved milestone Sep 3, 2015
@silenius silenius mentioned this issue Oct 2, 2015
@silenius
Copy link
Contributor

silenius commented Oct 5, 2015

This ticket can be closed, Grains DNS have been added in #27621 :)

@notpeter
Copy link
Contributor Author

notpeter commented Apr 26, 2016

Thanks all, especially @silenius! Looking at the code, the dns grain in 2016.3 will be a dict with the following keys:

dns:
  nameservers
  ip4_nameservers
  ip6_nameservers
  domain
  search

Perfect!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature new functionality including changes to functionality and code refactors, etc. Grains P3 Priority 3 Platform Relates to OS, containers, platform-based utilities like FS, system based apps
Projects
None yet
Development

No branches or pull requests

5 participants