From b4a2e9da50d7483c985e8fc2bcfb12db75b437c4 Mon Sep 17 00:00:00 2001 From: Tim Small Date: Sun, 3 Dec 2023 08:41:04 +0000 Subject: [PATCH] Lxd instance not found fix false positives (#7521) * lxd: Add lxc command execution debug statement. * lxd: avoid false positives in "instance not found" detection Due to changes over time in the error message which lxd printed when an instance wasn't found, the detection logic in the lxd connection plugin matched any "not found" string. Unfortunately this also false triggered on other errors e.g. "/usr/bin/python3: not found" from the payload, giving a confusing error message "UNREACHABLE! ... instance not found" to the ansible user. * Update changelog fragment. --------- Co-authored-by: Felix Fontein --- .../lxd-instance-not-found-avoid-false-positives.yml | 2 ++ plugins/connection/lxd.py | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/lxd-instance-not-found-avoid-false-positives.yml diff --git a/changelogs/fragments/lxd-instance-not-found-avoid-false-positives.yml b/changelogs/fragments/lxd-instance-not-found-avoid-false-positives.yml new file mode 100644 index 0000000000..03ac8ee01b --- /dev/null +++ b/changelogs/fragments/lxd-instance-not-found-avoid-false-positives.yml @@ -0,0 +1,2 @@ +minor_changes: + - "lxd connection plugin - tighten the detection logic for lxd ``Instance not found`` errors, to avoid false detection on unrelated errors such as ``/usr/bin/python3: not found`` (https://github.com/ansible-collections/community.general/pull/7521)." diff --git a/plugins/connection/lxd.py b/plugins/connection/lxd.py index 4ad81be399..93e1ab7a46 100644 --- a/plugins/connection/lxd.py +++ b/plugins/connection/lxd.py @@ -101,6 +101,8 @@ class Connection(ConnectionBase): self.get_option("executable"), "-c", cmd ]) + self._display.vvvvv(u"EXEC {0}".format(local_cmd), host=self._host()) + local_cmd = [to_bytes(i, errors='surrogate_or_strict') for i in local_cmd] in_data = to_bytes(in_data, errors='surrogate_or_strict', nonstring='passthru') @@ -110,10 +112,12 @@ class Connection(ConnectionBase): stdout = to_text(stdout) stderr = to_text(stderr) + self._display.vvvvv(u"EXEC lxc output: {0} {1}".format(stdout, stderr), host=self._host()) + if "is not running" in stderr: raise AnsibleConnectionFailure("instance not running: %s" % self._host()) - if "not found" in stderr: + if stderr.strip() == "Error: Instance not found" or stderr.strip() == "error: not found": raise AnsibleConnectionFailure("instance not found: %s" % self._host()) return process.returncode, stdout, stderr