Skip to content
Permalink
Browse files

[debops.resolvconf] Clean up fact script

  • Loading branch information...
drybjed committed Sep 30, 2019
1 parent 9afbc2a commit c3ddf6f3f374ff3af24d48947e5ce32301086362
Showing with 16 additions and 20 deletions.
  1. +16 −20 ansible/roles/debops.resolvconf/templates/etc/ansible/facts.d/resolvconf.fact.j2
@@ -14,9 +14,9 @@ def cmd_exists(cmd):
)


def read_resolv_file(file):
def read_resolv_file(resolv_file):
try:
with open(file, 'r') as f:
with open(resolv_file, 'r') as f:
nameservers = []
domain = ''
search = []
@@ -56,31 +56,27 @@ if search:

# Get the upstream resolver configuration
upstream_nameservers = []
resolvconf_files = []
resolvconf_path = '/run/resolvconf/interface'
systemdresolved_path = '/run/systemd/resolve/resolv.conf'
if os.path.isdir(resolvconf_path):
resolvconf_files = ([f for f in os.listdir(resolvconf_path)
resolvconf_files = ([os.path.join(resolvconf_path, f)
for f in os.listdir(resolvconf_path)
if os.path.isfile(os.path.join(resolvconf_path, f))])

for element in resolvconf_files:
try:
with open(os.path.join(resolvconf_path, element), 'r') as f:
for line in f:
if line.startswith('nameserver'):
server = line.strip().split()[1]
if not server.startswith('127.'):
upstream_nameservers.append(server)
if os.path.isfile(systemdresolved_path):
resolvconf_files.append(systemdresolved_path)

except Exception:
pass
for element in resolvconf_files:
try:
nameservers, domain, search = read_resolv_file(element)

# Get the upstream resolvers from systemd-resolved
systemdresolved_path = '/run/systemd/resolve/resolv.conf'
if os.path.isfile(systemdresolved_path):
nameservers, domain, search = read_resolv_file(systemdresolved_path)
for server in nameservers:
if not server.startswith('127.'):
upstream_nameservers.append(server)

for server in nameservers:
if not server.startswith('127.'):
upstream_nameservers.append(server)
except Exception:
pass

# If nameservers contains a localhost address, and we've found some
# upstream nameservers, then output the upstream nameservers.

0 comments on commit c3ddf6f

Please sign in to comment.
You can’t perform that action at this time.