Skip to content

Return non-zero exit from get on query failure#2313

Draft
ideaship wants to merge 1 commit into
mainfrom
get-nonzero-exit-on-query-failure
Draft

Return non-zero exit from get on query failure#2313
ideaship wants to merge 1 commit into
mainfrom
get-nonzero-exit-on-query-failure

Conversation

@ideaship
Copy link
Copy Markdown
Contributor

@ideaship ideaship commented Jun 1, 2026

What

osism get hostvars and osism get hosts logged an error but then returned None when the underlying ansible-inventory subprocess failed. cliff turns a take_action return value of None into exit code 0 (return_code = self.take_action(parsed_args) or 0), so these commands exited successfully even when the inventory query could not be run at all — making them unsafe in set -e scripts and && chains.

Both commands now return 1 on subprocess.CalledProcessError, i.e. when the query itself could not be executed.

Missing host

With the pinned ansible-core (2.18.9 — release repo latest/base.yml), ansible-inventory --host <unknown> does not return an empty result. It refuses the query with ERROR! You must pass a single valid host to --host parameter and exits 5. So osism get hostvars <unknown-host> now exits 1, matching its existing Host not found in inventory. error message.

What stays exit 0 (empty results)

Empty results are deliberately left as success — a query that ran fine but had nothing to show:

  • a host that exists but has no host vars (ansible-inventory prints {}, exits 0)
  • a requested variable/fact absent from an otherwise successfully retrieved data set
  • an inventory that lists no matching hosts

A note on the convention

There is no documented exit-code contract for the osism CLI — the behaviour is emergent and inconsistent across commands. This PR does not attempt to define or enforce one. It only tries to make osism get match what looks like the prevailing convention elsewhere in the CLI: a non-zero exit signals an operational failure (the query could not be run), while an empty result set is treated as a valid, successful answer.

Tests

Adds unit tests for both commands covering the failure path (raised CalledProcessError → exit 1) and the empty-result path (successful query with nothing to show → exit 0).

osism get hostvars and osism get hosts logged an error but then
returned None when the underlying ansible-inventory subprocess
failed. cliff turns a take_action return value of None into exit
code 0 (return_code = self.take_action(parsed_args) or 0), so these
commands exited successfully even though the inventory query could
not be run at all. That makes them unsafe to use in set -e scripts
or && chains, where a failed lookup silently looks like success.

Both commands now return 1 when subprocess.CalledProcessError is
raised, i.e. when the query itself could not be executed.

A missing host is one such failure. With the pinned ansible-core
(2.18.9, release latest/base.yml), ansible-inventory --host on a
host that is not in the inventory does not return an empty result;
it refuses the query with "ERROR! You must pass a single valid host
to --host parameter" and exits 5. osism get hostvars for an unknown
host therefore now exits 1, which matches its existing "Host not
found in inventory." error message.

Empty results are deliberately left as exit 0: a host that exists
but has no host vars (ansible-inventory prints {} and exits 0), a
requested variable or fact that is absent from an otherwise
successfully retrieved data set, or an inventory that lists no
matching hosts. These are valid answers to a query that did run.
This keeps the behaviour consistent with the convention used
elsewhere in the CLI, where a non-zero status signals an operational
failure rather than an empty result set.

Adds unit tests for both commands covering the failure path (a
raised CalledProcessError yields exit code 1) and the empty-result
path (a successful query with nothing to show yields exit code 0).

AI-assisted: Claude Code
Signed-off-by: Roger Luethi <luethi@osism.tech>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

2 participants