Skip to content
Permalink
Browse files

[debops.resolvconf] Get upstream nameservers when using systemd-resolved

Fixes #1078
  • Loading branch information...
DouglasHeriot committed Sep 30, 2019
1 parent 86a3dc2 commit 92161147e90488db6626fc387caaccaee44bfec9
Showing with 42 additions and 27 deletions.
  1. +42 −27 ansible/roles/debops.resolvconf/templates/etc/ansible/facts.d/resolvconf.fact.j2
@@ -13,6 +13,26 @@ def cmd_exists(cmd):
for path in os.environ["PATH"].split(os.pathsep)
)

def read_resolv_file(file):
try:
with open(file, 'r') as f:
nameservers = []
domain = ''
search = []
for line in f:
if line.startswith('nameserver'):
nameservers.append(line.strip().split()[1])
elif line.startswith('domain'):
domain = line.strip().split()[1]
elif line.startswith('search'):
for element in line.strip().split()[1:]:
search.append(element.rstrip('.'))

return nameservers, domain, search

except Exception:
pass


output = loads('''{{ {"configured": True,
"deploy_state": resolvconf__deploy_state
@@ -24,38 +44,22 @@ if not output['installed']:
output['deploy_state'] = 'absent'

# Get the primary resolver configuration
try:
with open('/etc/resolv.conf', 'r') as f:
nameservers = []
domain = ''
search = []
for line in f:
if line.startswith('nameserver'):
nameservers.append(line.strip().split()[1])
elif line.startswith('domain'):
domain = line.strip().split()[1]
elif line.startswith('search'):
for element in line.strip().split()[1:]:
search.append(element.rstrip('.'))

if nameservers:
output['nameservers'] = nameservers
if domain:
output['domain'] = domain
if search:
output['search'] = search

except Exception:
pass
nameservers, domain, search = read_resolv_file('/etc/resolv.conf')

if nameservers:
output['nameservers'] = nameservers
if domain:
output['domain'] = domain
if search:
output['search'] = search

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

upstream_nameservers = []

for element in resolvconf_files:
try:
with open(os.path.join(resolvconf_path, element), 'r') as f:
@@ -68,7 +72,18 @@ if os.path.isdir(resolvconf_path):
except Exception:
pass

if '127.0.0.1' in output['nameservers'] and upstream_nameservers:
output['upstream_nameservers'] = upstream_nameservers
# 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)


# 127.0.0.53 is the stub address used by systemd-resolved
if ('127.0.0.1' in output['nameservers'] or '127.0.0.53' in output['nameservers']) and upstream_nameservers:
output['upstream_nameservers'] = upstream_nameservers

print(dumps(output, sort_keys=True, indent=4))

0 comments on commit 9216114

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