fix: Improve compatiblity with restrictive shell environments #2073
+160
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #2066.
Description
When running
ansible-navigator collections --mode stdoutin containers with restricted permissions, users encountered two cascading errors:PermissionError: [Errno 1] Operation not permitted: '/bin/sh'AttributeError: 'Action' object has no attribute '_interaction'This prevented collections from being cataloged in environments with SELinux restrictions, security contexts, or other permission limitations.
Root Causes
Primary Bug (PermissionError)
The
catalog_collections.pyscript usedshell=Trueinsubprocess.run()with shell pipes to filteransible-configoutput:This required access to
/bin/sh, which was blocked in restrictive container environments.Secondary Bug (AttributeError)
When the primary error occurred,
notify_failed()tried to display a UI notification:However,
_interactionis not initialized in stdout mode, causing anAttributeErrorthat masked the real issue.Solution
1. Removed Shell Dependency
run_command()to useshell=Falseand pass commands as listsansible-config dump | grep COLLECTIONS_PATHSwith Python-native filteringPermissionErrorandFileNotFoundErrorFiles Changed:
src/ansible_navigator/data/catalog_collections.py(lines 509-532, 603-622)2. Fixed Error Notification in Stdout Mode
notify_failed()to check mode and_interactionavailabilityFiles Changed:
src/ansible_navigator/actions/collections.py(lines 234-244)Testing
Added comprehensive unit tests covering both bugs:
Test Coverage:
test_run_command_without_shell()- Verifies subprocess runs withshell=Falsetest_run_command_handles_permission_error()- Verifies graceful PermissionError handlingtest_retrieve_collections_paths_without_pipe()- Verifies no shell pipes usedtest_retrieve_collections_paths_not_found()- Verifies error handlingtest_notify_failed_in_stdout_mode()- Verifies no AttributeError in stdout modetest_notify_failed_in_interactive_mode()- Verifies UI notifications work in interactive modeImpact
This fix allows
ansible-navigator collectionsto work reliably in: