Use get_file_lines in hostname module
This commit is contained in:
parent
502270c804
commit
774e563452
1 changed files with 30 additions and 36 deletions
|
@ -67,6 +67,7 @@ from ansible.module_utils.basic import (
|
||||||
)
|
)
|
||||||
from ansible.module_utils.common.sys_info import get_platform_subclass
|
from ansible.module_utils.common.sys_info import get_platform_subclass
|
||||||
from ansible.module_utils.facts.system.service_mgr import ServiceMgrFactCollector
|
from ansible.module_utils.facts.system.service_mgr import ServiceMgrFactCollector
|
||||||
|
from ansible.module_utils.facts.utils import get_file_lines
|
||||||
from ansible.module_utils._text import to_native, to_text
|
from ansible.module_utils._text import to_native, to_text
|
||||||
from ansible.module_utils.six import PY3, text_type
|
from ansible.module_utils.six import PY3, text_type
|
||||||
|
|
||||||
|
@ -244,8 +245,7 @@ class FileStrategy(BaseStrategy):
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(self.FILE, 'r') as f:
|
return get_file_lines(self.FILE)
|
||||||
return f.read().strip()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.module.fail_json(
|
self.module.fail_json(
|
||||||
msg="failed to read hostname: %s" % to_native(e),
|
msg="failed to read hostname: %s" % to_native(e),
|
||||||
|
@ -278,11 +278,10 @@ class RedHatStrategy(BaseStrategy):
|
||||||
|
|
||||||
def get_permanent_hostname(self):
|
def get_permanent_hostname(self):
|
||||||
try:
|
try:
|
||||||
with open(self.NETWORK_FILE, 'rb') as f:
|
for line in get_file_lines(self.NETWORK_FILE):
|
||||||
for line in f.readlines():
|
if line.startswith('HOSTNAME'):
|
||||||
if line.startswith('HOSTNAME'):
|
k, v = line.split('=')
|
||||||
k, v = line.split('=')
|
return v.strip()
|
||||||
return v.strip()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.module.fail_json(
|
self.module.fail_json(
|
||||||
msg="failed to read hostname: %s" % to_native(e),
|
msg="failed to read hostname: %s" % to_native(e),
|
||||||
|
@ -292,13 +291,12 @@ class RedHatStrategy(BaseStrategy):
|
||||||
try:
|
try:
|
||||||
lines = []
|
lines = []
|
||||||
found = False
|
found = False
|
||||||
with open(self.NETWORK_FILE, 'rb') as f:
|
for line in get_file_lines(self.NETWORK_FILE):
|
||||||
for line in f.readlines():
|
if line.startswith('HOSTNAME'):
|
||||||
if line.startswith('HOSTNAME'):
|
lines.append("HOSTNAME=%s\n" % name)
|
||||||
lines.append("HOSTNAME=%s\n" % name)
|
found = True
|
||||||
found = True
|
else:
|
||||||
else:
|
lines.append(line)
|
||||||
lines.append(line)
|
|
||||||
if not found:
|
if not found:
|
||||||
lines.append("HOSTNAME=%s\n" % name)
|
lines.append("HOSTNAME=%s\n" % name)
|
||||||
with open(self.NETWORK_FILE, 'w+') as f:
|
with open(self.NETWORK_FILE, 'w+') as f:
|
||||||
|
@ -388,11 +386,10 @@ class OpenRCStrategy(BaseStrategy):
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(self.FILE, 'r') as f:
|
for line in get_file_lines(self.FILE):
|
||||||
for line in f:
|
line = line.strip()
|
||||||
line = line.strip()
|
if line.startswith('hostname='):
|
||||||
if line.startswith('hostname='):
|
return line[10:].strip('"')
|
||||||
return line[10:].strip('"')
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.module.fail_json(
|
self.module.fail_json(
|
||||||
msg="failed to read hostname: %s" % to_native(e),
|
msg="failed to read hostname: %s" % to_native(e),
|
||||||
|
@ -400,13 +397,12 @@ class OpenRCStrategy(BaseStrategy):
|
||||||
|
|
||||||
def set_permanent_hostname(self, name):
|
def set_permanent_hostname(self, name):
|
||||||
try:
|
try:
|
||||||
with open(self.FILE, 'r') as f:
|
lines = [x.strip() for x in get_file_lines(self.FILE)]
|
||||||
lines = [x.strip() for x in f]
|
|
||||||
|
|
||||||
for i, line in enumerate(lines):
|
for i, line in enumerate(lines):
|
||||||
if line.startswith('hostname='):
|
if line.startswith('hostname='):
|
||||||
lines[i] = 'hostname="%s"' % name
|
lines[i] = 'hostname="%s"' % name
|
||||||
break
|
break
|
||||||
|
|
||||||
with open(self.FILE, 'w') as f:
|
with open(self.FILE, 'w') as f:
|
||||||
f.write('\n'.join(lines) + '\n')
|
f.write('\n'.join(lines) + '\n')
|
||||||
|
@ -491,11 +487,10 @@ class FreeBSDStrategy(BaseStrategy):
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(self.FILE, 'r') as f:
|
for line in get_file_lines(self.FILE):
|
||||||
for line in f:
|
line = line.strip()
|
||||||
line = line.strip()
|
if line.startswith('hostname='):
|
||||||
if line.startswith('hostname='):
|
return line[10:].strip('"')
|
||||||
return line[10:].strip('"')
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.module.fail_json(
|
self.module.fail_json(
|
||||||
msg="failed to read hostname: %s" % to_native(e),
|
msg="failed to read hostname: %s" % to_native(e),
|
||||||
|
@ -504,13 +499,12 @@ class FreeBSDStrategy(BaseStrategy):
|
||||||
def set_permanent_hostname(self, name):
|
def set_permanent_hostname(self, name):
|
||||||
try:
|
try:
|
||||||
if os.path.isfile(self.FILE):
|
if os.path.isfile(self.FILE):
|
||||||
with open(self.FILE, 'r') as f:
|
lines = [x.strip() for x in get_file_lines(self.FILE)]
|
||||||
lines = [x.strip() for x in f]
|
|
||||||
|
|
||||||
for i, line in enumerate(lines):
|
for i, line in enumerate(lines):
|
||||||
if line.startswith('hostname='):
|
if line.startswith('hostname='):
|
||||||
lines[i] = 'hostname="%s"' % name
|
lines[i] = 'hostname="%s"' % name
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
lines = ['hostname="%s"' % name]
|
lines = ['hostname="%s"' % name]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue