mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 09:23:56 +00:00
Merge pull request #22403 from hvnsweeting/enh-host-module-when-missing-hostfile
create host file if it does not exist
This commit is contained in:
commit
8f0f5ae3f5
@ -23,6 +23,17 @@ def __get_hosts_filename():
|
|||||||
return __salt__['config.option']('hosts.file')
|
return __salt__['config.option']('hosts.file')
|
||||||
|
|
||||||
|
|
||||||
|
def _get_or_create_hostfile():
|
||||||
|
'''
|
||||||
|
Wrapper of __get_hosts_filename but create host file if it
|
||||||
|
does not exist.
|
||||||
|
'''
|
||||||
|
hfn = __get_hosts_filename()
|
||||||
|
if not os.path.exists(hfn):
|
||||||
|
salt.utils.fopen(hfn, 'w').close()
|
||||||
|
return hfn
|
||||||
|
|
||||||
|
|
||||||
def _list_hosts():
|
def _list_hosts():
|
||||||
'''
|
'''
|
||||||
Return the hosts found in the hosts file in as an OrderedDict
|
Return the hosts found in the hosts file in as an OrderedDict
|
||||||
@ -124,7 +135,7 @@ def set_host(ip, alias):
|
|||||||
|
|
||||||
salt '*' hosts.set_host <ip> <alias>
|
salt '*' hosts.set_host <ip> <alias>
|
||||||
'''
|
'''
|
||||||
hfn = __get_hosts_filename()
|
hfn = _get_or_create_hostfile()
|
||||||
ovr = False
|
ovr = False
|
||||||
if not os.path.isfile(hfn):
|
if not os.path.isfile(hfn):
|
||||||
return False
|
return False
|
||||||
@ -165,7 +176,7 @@ def rm_host(ip, alias):
|
|||||||
'''
|
'''
|
||||||
if not has_pair(ip, alias):
|
if not has_pair(ip, alias):
|
||||||
return True
|
return True
|
||||||
hfn = __get_hosts_filename()
|
hfn = _get_or_create_hostfile()
|
||||||
lines = salt.utils.fopen(hfn).readlines()
|
lines = salt.utils.fopen(hfn).readlines()
|
||||||
for ind in range(len(lines)):
|
for ind in range(len(lines)):
|
||||||
tmpline = lines[ind].strip()
|
tmpline = lines[ind].strip()
|
||||||
@ -202,7 +213,7 @@ def add_host(ip, alias):
|
|||||||
|
|
||||||
salt '*' hosts.add_host <ip> <alias>
|
salt '*' hosts.add_host <ip> <alias>
|
||||||
'''
|
'''
|
||||||
hfn = __get_hosts_filename()
|
hfn = _get_or_create_hostfile()
|
||||||
if not os.path.isfile(hfn):
|
if not os.path.isfile(hfn):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -231,7 +242,7 @@ def _write_hosts(hosts):
|
|||||||
)
|
)
|
||||||
lines.append(line)
|
lines.append(line)
|
||||||
|
|
||||||
hfn = __get_hosts_filename()
|
hfn = _get_or_create_hostfile()
|
||||||
with salt.utils.fopen(hfn, 'w+') as ofile:
|
with salt.utils.fopen(hfn, 'w+') as ofile:
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if line.strip():
|
if line.strip():
|
||||||
|
Loading…
Reference in New Issue
Block a user