Pep8 fixes for known_hosts.py (#24029)
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
7c1b8da1a1
commit
9456f93ea8
2 changed files with 72 additions and 70 deletions
|
@ -126,7 +126,8 @@ def enforce_state(module, params):
|
||||||
# We will change state if found==True & state!="present"
|
# We will change state if found==True & state!="present"
|
||||||
# or found==False & state=="present"
|
# or found==False & state=="present"
|
||||||
# i.e found XOR (state=="present")
|
# i.e found XOR (state=="present")
|
||||||
#Alternatively, if replace is true (i.e. key present, and we must change it)
|
# Alternatively, if replace is true (i.e. key present, and we must change
|
||||||
|
# it)
|
||||||
if module.check_mode:
|
if module.check_mode:
|
||||||
module.exit_json(changed=replace_or_add or (state == "present") != found,
|
module.exit_json(changed=replace_or_add or (state == "present") != found,
|
||||||
diff=params['diff'])
|
diff=params['diff'])
|
||||||
|
@ -147,8 +148,7 @@ def enforce_state(module, params):
|
||||||
if e.errno == errno.ENOENT:
|
if e.errno == errno.ENOENT:
|
||||||
inf = None
|
inf = None
|
||||||
else:
|
else:
|
||||||
module.fail_json(msg="Failed to read %s: %s" % \
|
module.fail_json(msg="Failed to read %s: %s" % (path, str(e)))
|
||||||
(path,str(e)))
|
|
||||||
try:
|
try:
|
||||||
outf = tempfile.NamedTemporaryFile(mode='w+', dir=os.path.dirname(path))
|
outf = tempfile.NamedTemporaryFile(mode='w+', dir=os.path.dirname(path))
|
||||||
if inf is not None:
|
if inf is not None:
|
||||||
|
@ -163,8 +163,7 @@ def enforce_state(module, params):
|
||||||
module.atomic_move(outf.name, path)
|
module.atomic_move(outf.name, path)
|
||||||
except (IOError, OSError):
|
except (IOError, OSError):
|
||||||
e = get_exception()
|
e = get_exception()
|
||||||
module.fail_json(msg="Failed to write to file %s: %s" % \
|
module.fail_json(msg="Failed to write to file %s: %s" % (path, str(e)))
|
||||||
(path,str(e)))
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
outf.close()
|
outf.close()
|
||||||
|
@ -175,6 +174,7 @@ def enforce_state(module, params):
|
||||||
|
|
||||||
return params
|
return params
|
||||||
|
|
||||||
|
|
||||||
def sanity_check(module, host, key, sshkeygen):
|
def sanity_check(module, host, key, sshkeygen):
|
||||||
'''Check supplied key is sensible
|
'''Check supplied key is sensible
|
||||||
|
|
||||||
|
@ -198,11 +198,11 @@ def sanity_check(module,host,key,sshkeygen):
|
||||||
outf.flush()
|
outf.flush()
|
||||||
except IOError:
|
except IOError:
|
||||||
e = get_exception()
|
e = get_exception()
|
||||||
module.fail_json(msg="Failed to write to temporary file %s: %s" % \
|
module.fail_json(msg="Failed to write to temporary file %s: %s" %
|
||||||
(outf.name, str(e)))
|
(outf.name, str(e)))
|
||||||
rc,stdout,stderr=module.run_command([sshkeygen,'-F',host,
|
|
||||||
'-f',outf.name],
|
sshkeygen_command = [sshkeygen, '-F', host, '-f', outf.name]
|
||||||
check_rc=True)
|
rc, stdout, stderr = module.run_command(sshkeygen_command, check_rc=True)
|
||||||
try:
|
try:
|
||||||
outf.close()
|
outf.close()
|
||||||
except:
|
except:
|
||||||
|
@ -211,6 +211,7 @@ def sanity_check(module,host,key,sshkeygen):
|
||||||
if stdout == '': # host not found
|
if stdout == '': # host not found
|
||||||
module.fail_json(msg="Host parameter does not match hashed host field in supplied key")
|
module.fail_json(msg="Host parameter does not match hashed host field in supplied key")
|
||||||
|
|
||||||
|
|
||||||
def search_for_host_key(module, host, key, hash_host, path, sshkeygen):
|
def search_for_host_key(module, host, key, hash_host, path, sshkeygen):
|
||||||
'''search_for_host_key(module,host,key,path,sshkeygen) -> (found,replace_or_add,found_line)
|
'''search_for_host_key(module,host,key,path,sshkeygen) -> (found,replace_or_add,found_line)
|
||||||
|
|
||||||
|
@ -229,8 +230,7 @@ def search_for_host_key(module, host, key, hash_host, path, sshkeygen):
|
||||||
|
|
||||||
# openssh >=6.4 has changed ssh-keygen behaviour such that it returns
|
# openssh >=6.4 has changed ssh-keygen behaviour such that it returns
|
||||||
# 1 if no host is found, whereas previously it returned 0
|
# 1 if no host is found, whereas previously it returned 0
|
||||||
rc,stdout,stderr=module.run_command(sshkeygen_command,
|
rc, stdout, stderr = module.run_command(sshkeygen_command, check_rc=False)
|
||||||
check_rc = False)
|
|
||||||
if stdout == '' and stderr == '' and (rc == 0 or rc == 1):
|
if stdout == '' and stderr == '' and (rc == 0 or rc == 1):
|
||||||
return False, False, None, key # host not found, no other errors
|
return False, False, None, key # host not found, no other errors
|
||||||
if rc != 0: # something went wrong
|
if rc != 0: # something went wrong
|
||||||
|
@ -275,6 +275,7 @@ def search_for_host_key(module, host, key, hash_host, path, sshkeygen):
|
||||||
# No match found, return found and replace, but no line
|
# No match found, return found and replace, but no line
|
||||||
return True, True, None, key
|
return True, True, None, key
|
||||||
|
|
||||||
|
|
||||||
def normalize_known_hosts_key(key):
|
def normalize_known_hosts_key(key):
|
||||||
'''
|
'''
|
||||||
Transform a key, either taken from a known_host file or provided by the
|
Transform a key, either taken from a known_host file or provided by the
|
||||||
|
@ -299,6 +300,7 @@ def normalize_known_hosts_key(key):
|
||||||
d['key'] = k[2]
|
d['key'] = k[2]
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
||||||
def compute_diff(path, found_line, replace_or_add, state, key):
|
def compute_diff(path, found_line, replace_or_add, state, key):
|
||||||
diff = {
|
diff = {
|
||||||
'before_header': path,
|
'before_header': path,
|
||||||
|
@ -323,6 +325,7 @@ def compute_diff(path, found_line, replace_or_add, state, key):
|
||||||
diff['after'] = ''.join(lines)
|
diff['after'] = ''.join(lines)
|
||||||
return diff
|
return diff
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
|
|
|
@ -729,7 +729,6 @@ lib/ansible/modules/system/hostname.py
|
||||||
lib/ansible/modules/system/iptables.py
|
lib/ansible/modules/system/iptables.py
|
||||||
lib/ansible/modules/system/java_cert.py
|
lib/ansible/modules/system/java_cert.py
|
||||||
lib/ansible/modules/system/kernel_blacklist.py
|
lib/ansible/modules/system/kernel_blacklist.py
|
||||||
lib/ansible/modules/system/known_hosts.py
|
|
||||||
lib/ansible/modules/system/locale_gen.py
|
lib/ansible/modules/system/locale_gen.py
|
||||||
lib/ansible/modules/system/lvg.py
|
lib/ansible/modules/system/lvg.py
|
||||||
lib/ansible/modules/system/lvol.py
|
lib/ansible/modules/system/lvol.py
|
||||||
|
|
Loading…
Reference in a new issue