Fix variable use before definition. (#78500)

* Fix variable use before definition.

* Include mkstemp in exception handler.

Also remove two pointless variable assignments.
pull/78528/head
Matt Clay 2 years ago committed by GitHub
parent 7f69629fa7
commit 09d0df1d87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- Fix potential, but unlikely, cases of variable use before definition.

@ -90,10 +90,10 @@ class ConnectionProcess(object):
self._ansible_playbook_pid = ansible_playbook_pid self._ansible_playbook_pid = ansible_playbook_pid
def start(self, variables): def start(self, variables):
try:
messages = list() messages = list()
result = {} result = {}
try:
messages.append(('vvvv', 'control socket path is %s' % self.socket_path)) messages.append(('vvvv', 'control socket path is %s' % self.socket_path))
# If this is a relative path (~ gets expanded later) then plug the # If this is a relative path (~ gets expanded later) then plug the

@ -745,8 +745,9 @@ class TaskExecutor:
# if we didn't skip this task, use the helpers to evaluate the changed/ # if we didn't skip this task, use the helpers to evaluate the changed/
# failed_when properties # failed_when properties
if 'skipped' not in result: if 'skipped' not in result:
try:
condname = 'changed' condname = 'changed'
try:
_evaluate_changed_when_result(result) _evaluate_changed_when_result(result)
condname = 'failed' condname = 'failed'
_evaluate_failed_when_result(result) _evaluate_failed_when_result(result)

@ -1592,10 +1592,13 @@ def _resolve_depenency_map(
raise AnsibleError("Failed to import resolvelib, check that a supported version is installed") raise AnsibleError("Failed to import resolvelib, check that a supported version is installed")
if not HAS_PACKAGING: if not HAS_PACKAGING:
raise AnsibleError("Failed to import packaging, check that a supported version is installed") raise AnsibleError("Failed to import packaging, check that a supported version is installed")
req = None
try: try:
dist = distribution('ansible-core') dist = distribution('ansible-core')
except Exception: except Exception:
req = None pass
else: else:
req = next((rr for r in (dist.requires or []) if (rr := PkgReq(r)).name == 'resolvelib'), None) req = next((rr for r in (dist.requires or []) if (rr := PkgReq(r)).name == 'resolvelib'), None)
finally: finally:

@ -467,6 +467,9 @@ def write_file(module, dest, content, resp):
""" """
Create temp file and write content to dest file only if content changed Create temp file and write content to dest file only if content changed
""" """
tmpsrc = None
try: try:
fd, tmpsrc = tempfile.mkstemp(dir=module.tmpdir) fd, tmpsrc = tempfile.mkstemp(dir=module.tmpdir)
with os.fdopen(fd, 'wb') as f: with os.fdopen(fd, 'wb') as f:
@ -475,14 +478,11 @@ def write_file(module, dest, content, resp):
else: else:
shutil.copyfileobj(content, f) shutil.copyfileobj(content, f)
except Exception as e: except Exception as e:
if os.path.exists(tmpsrc): if tmpsrc and os.path.exists(tmpsrc):
os.remove(tmpsrc) os.remove(tmpsrc)
msg = format_message("Failed to create temporary content file: %s" % to_native(e), resp) msg = format_message("Failed to create temporary content file: %s" % to_native(e), resp)
module.fail_json(msg=msg, **resp) module.fail_json(msg=msg, **resp)
checksum_src = None
checksum_dest = None
checksum_src = module.sha1(tmpsrc) checksum_src = module.sha1(tmpsrc)
checksum_dest = module.sha1(dest) checksum_dest = module.sha1(dest)

@ -179,6 +179,7 @@ try:
from winrm.protocol import Protocol from winrm.protocol import Protocol
import requests.exceptions import requests.exceptions
HAS_WINRM = True HAS_WINRM = True
WINRM_IMPORT_ERR = None
except ImportError as e: except ImportError as e:
HAS_WINRM = False HAS_WINRM = False
WINRM_IMPORT_ERR = e WINRM_IMPORT_ERR = e
@ -186,6 +187,7 @@ except ImportError as e:
try: try:
import xmltodict import xmltodict
HAS_XMLTODICT = True HAS_XMLTODICT = True
XMLTODICT_IMPORT_ERR = None
except ImportError as e: except ImportError as e:
HAS_XMLTODICT = False HAS_XMLTODICT = False
XMLTODICT_IMPORT_ERR = e XMLTODICT_IMPORT_ERR = e

@ -80,8 +80,9 @@ def _list_plugins_from_paths(ptype, dirs, collection, depth=0):
else: else:
raise AnsibleError('how did you get here?') raise AnsibleError('how did you get here?')
try:
added = False added = False
try:
if path not in ploader._extra_dirs: if path not in ploader._extra_dirs:
ploader.add_directory(path) ploader.add_directory(path)
added = True added = True

Loading…
Cancel
Save