From eb1fb415768e48530a06ab1508ed1c6b2b6a5f68 Mon Sep 17 00:00:00 2001 From: Reed Loden Date: Sun, 28 Sep 2014 17:24:16 -0700 Subject: [PATCH] Add several DNS-related facts by parsing /etc/resolv.conf Facts include nameservers, domain, search path, sortlist, and options. --- lib/ansible/module_utils/facts.py | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/ansible/module_utils/facts.py b/lib/ansible/module_utils/facts.py index de158d4343c..855a470a083 100644 --- a/lib/ansible/module_utils/facts.py +++ b/lib/ansible/module_utils/facts.py @@ -151,6 +151,7 @@ class Facts(object): self.get_user_facts() self.get_local_facts() self.get_env_facts() + self.get_dns_facts() def populate(self): return self.facts @@ -632,6 +633,37 @@ class Facts(object): for k,v in os.environ.iteritems(): self.facts['env'][k] = v + def get_dns_facts(self): + self.facts['dns'] = {} + for line in get_file_lines('/etc/resolv.conf'): + if line.startswith('#') or line.startswith(';') or line.strip() == '': + continue + tokens = line.split() + if len(tokens) == 0: + continue + if tokens[0] == 'nameserver': + self.facts['dns']['nameservers'] = [] + for nameserver in tokens[1:]: + self.facts['dns']['nameservers'].append(nameserver) + elif tokens[0] == 'domain': + self.facts['dns']['domain'] = tokens[1] + elif tokens[0] == 'search': + self.facts['dns']['search'] = [] + for suffix in tokens[1:]: + self.facts['dns']['search'].append(suffix) + elif tokens[0] == 'sortlist': + self.facts['dns']['sortlist'] = [] + for address in tokens[1:]: + self.facts['dns']['sortlist'].append(address) + elif tokens[0] == 'options': + self.facts['dns']['options'] = {} + for option in tokens[1:]: + option_tokens = option.split(':', 1) + if len(option_tokens) == 0: + continue + val = len(option_tokens) == 2 and option_tokens[1] or True + self.facts['dns']['options'][option_tokens[0]] = val + class Hardware(Facts): """ This is a generic Hardware subclass of Facts. This should be further