Use a context handler for file handling in paramiko_ssh.Connection. (#65624)

This commit is contained in:
Mads Jensen 2020-01-06 19:09:57 +01:00 committed by ansibot
parent d3e9ef3c35
commit ec34235e2e
2 changed files with 14 additions and 14 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- paramiko_ssh - optimized file handling by using a context manager.

View file

@ -517,25 +517,23 @@ class Connection(ConnectionBase):
path = os.path.expanduser("~/.ssh")
makedirs_safe(path)
f = open(filename, 'w')
with open(filename, 'w') as f:
for hostname, keys in iteritems(self.ssh._host_keys):
for hostname, keys in iteritems(self.ssh._host_keys):
for keytype, key in iteritems(keys):
for keytype, key in iteritems(keys):
# was f.write
added_this_time = getattr(key, '_added_by_ansible_this_time', False)
if not added_this_time:
f.write("%s %s %s\n" % (hostname, keytype, key.get_base64()))
# was f.write
added_this_time = getattr(key, '_added_by_ansible_this_time', False)
if not added_this_time:
f.write("%s %s %s\n" % (hostname, keytype, key.get_base64()))
for hostname, keys in iteritems(self.ssh._host_keys):
for hostname, keys in iteritems(self.ssh._host_keys):
for keytype, key in iteritems(keys):
added_this_time = getattr(key, '_added_by_ansible_this_time', False)
if added_this_time:
f.write("%s %s %s\n" % (hostname, keytype, key.get_base64()))
f.close()
for keytype, key in iteritems(keys):
added_this_time = getattr(key, '_added_by_ansible_this_time', False)
if added_this_time:
f.write("%s %s %s\n" % (hostname, keytype, key.get_base64()))
def reset(self):
self.close()