Skip to content

Commit 4195cbb

Browse files
authored
incus_connection: Improve error handling (#10349)
Related to #10344 This tweaks the error handling logic to work with more versions of Incus as well as catching some of the project and instance access errors. The full context (instance name, project name and remote name) is now included so that the user can easily diagnose access problems. Signed-off-by: Stéphane Graber <[email protected]>
1 parent 7a4448d commit 4195cbb

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- incus connection plugin - fix error handling to return more useful Ansible errors to the user (https://github.com/ansible-collections/community.general/issues/10344, https://github.com/ansible-collections/community.general/pull/10349).

plugins/connection/incus.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,35 @@ def exec_command(self, cmd, in_data=None, sudoable=True):
155155
stdout = to_text(stdout)
156156
stderr = to_text(stderr)
157157

158-
if stderr == "Error: Instance is not running.\n":
159-
raise AnsibleConnectionFailure(f"instance not running: {self._instance()}")
158+
if stderr.startswith("Error: ") and stderr.rstrip().endswith(
159+
": Instance is not running"
160+
):
161+
raise AnsibleConnectionFailure(
162+
f"instance not running: {self._instance()} (remote={self.get_option('remote')}, project={self.get_option('project')})"
163+
)
164+
165+
if stderr.startswith("Error: ") and stderr.rstrip().endswith(
166+
": Instance not found"
167+
):
168+
raise AnsibleConnectionFailure(
169+
f"instance not found: {self._instance()} (remote={self.get_option('remote')}, project={self.get_option('project')})"
170+
)
160171

161-
if stderr == "Error: Instance not found\n":
162-
raise AnsibleConnectionFailure(f"instance not found: {self._instance()}")
172+
if (
173+
stderr.startswith("Error: ")
174+
and ": User does not have permission " in stderr
175+
):
176+
raise AnsibleConnectionFailure(
177+
f"instance access denied: {self._instance()} (remote={self.get_option('remote')}, project={self.get_option('project')})"
178+
)
179+
180+
if (
181+
stderr.startswith("Error: ")
182+
and ": User does not have entitlement " in stderr
183+
):
184+
raise AnsibleConnectionFailure(
185+
f"instance access denied: {self._instance()} (remote={self.get_option('remote')}, project={self.get_option('project')})"
186+
)
163187

164188
return process.returncode, stdout, stderr
165189

0 commit comments

Comments
 (0)