Debian Codegraph @

lib/ansible/module_utils/facts/system/dns.py

raw
1 # This file is part of Ansible
2 #
3 # Ansible is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # Ansible is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
15
16 from __future__ import annotations
17
18 import typing as t
19
20 from ansible.module_utils.facts.utils import get_file_content
21
22 from ansible.module_utils.facts.collector import BaseFactCollector
23
24
25 class DnsFactCollector(BaseFactCollector):
26 name = 'dns'
27 _fact_ids = set() # type: t.Set[str]
28
29 def collect(self, module=None, collected_facts=None):
30 dns_facts = {}
31
32 # TODO: flatten
33 dns_facts['dns'] = {}
34
35 for line in get_file_content('/etc/resolv.conf', '').splitlines():
36 if line.startswith('#') or line.startswith(';') or line.strip() == '':
37 continue
38 tokens = line.split()
39 if len(tokens) == 0:
40 continue
41 if tokens[0] == 'nameserver':
42 if 'nameservers' not in dns_facts['dns']:
43 dns_facts['dns']['nameservers'] = []
44 for nameserver in tokens[1:]:
45 dns_facts['dns']['nameservers'].append(nameserver)
46 elif tokens[0] == 'domain':
47 if len(tokens) > 1:
48 dns_facts['dns']['domain'] = tokens[1]
49 elif tokens[0] == 'search':
50 dns_facts['dns']['search'] = []
51 for suffix in tokens[1:]:
52 dns_facts['dns']['search'].append(suffix)
53 elif tokens[0] == 'sortlist':
54 dns_facts['dns']['sortlist'] = []
55 for address in tokens[1:]:
56 dns_facts['dns']['sortlist'].append(address)
57 elif tokens[0] == 'options':
58 dns_facts['dns']['options'] = {}
59 if len(tokens) > 1:
60 for option in tokens[1:]:
61 option_tokens = option.split(':', 1)
62 if len(option_tokens) == 0:
63 continue
64 val = len(option_tokens) == 2 and option_tokens[1] or True
65 dns_facts['dns']['options'][option_tokens[0]] = val
66
67 return dns_facts