Use get_file_lines in hostname module

This commit is contained in:
Alexander Sowitzki 2021-05-27 13:03:01 +02:00 committed by Alexander Sowitzki
parent 502270c804
commit 774e563452

View file

@ -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.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.six import PY3, text_type
@ -244,8 +245,7 @@ class FileStrategy(BaseStrategy):
return ''
try:
with open(self.FILE, 'r') as f:
return f.read().strip()
return get_file_lines(self.FILE)
except Exception as e:
self.module.fail_json(
msg="failed to read hostname: %s" % to_native(e),
@ -278,8 +278,7 @@ class RedHatStrategy(BaseStrategy):
def get_permanent_hostname(self):
try:
with open(self.NETWORK_FILE, 'rb') as f:
for line in f.readlines():
for line in get_file_lines(self.NETWORK_FILE):
if line.startswith('HOSTNAME'):
k, v = line.split('=')
return v.strip()
@ -292,8 +291,7 @@ class RedHatStrategy(BaseStrategy):
try:
lines = []
found = False
with open(self.NETWORK_FILE, 'rb') as f:
for line in f.readlines():
for line in get_file_lines(self.NETWORK_FILE):
if line.startswith('HOSTNAME'):
lines.append("HOSTNAME=%s\n" % name)
found = True
@ -388,8 +386,7 @@ class OpenRCStrategy(BaseStrategy):
return ''
try:
with open(self.FILE, 'r') as f:
for line in f:
for line in get_file_lines(self.FILE):
line = line.strip()
if line.startswith('hostname='):
return line[10:].strip('"')
@ -400,8 +397,7 @@ class OpenRCStrategy(BaseStrategy):
def set_permanent_hostname(self, name):
try:
with open(self.FILE, 'r') as f:
lines = [x.strip() for x in f]
lines = [x.strip() for x in get_file_lines(self.FILE)]
for i, line in enumerate(lines):
if line.startswith('hostname='):
@ -491,8 +487,7 @@ class FreeBSDStrategy(BaseStrategy):
return ''
try:
with open(self.FILE, 'r') as f:
for line in f:
for line in get_file_lines(self.FILE):
line = line.strip()
if line.startswith('hostname='):
return line[10:].strip('"')
@ -504,8 +499,7 @@ class FreeBSDStrategy(BaseStrategy):
def set_permanent_hostname(self, name):
try:
if os.path.isfile(self.FILE):
with open(self.FILE, 'r') as f:
lines = [x.strip() for x in f]
lines = [x.strip() for x in get_file_lines(self.FILE)]
for i, line in enumerate(lines):
if line.startswith('hostname='):