Skip to content

Fix: scope input_inventories name lookup to organization (awx.awx.inventory) - #16630

Open
maroofsangi wants to merge 9 commits into
ansible:develfrom
maroofsangi:fix/inventory-input-inventories-org-scope
Open

maroofsangi wants to merge 9 commits into
ansible:develfrom
maroofsangi:fix/inventory-input-inventories-org-scope

Conversation

@maroofsangi

@maroofsangi maroofsangi commented Aug 31, 2026

Copy link
Copy Markdown

SUMMARY

Fixes #16393 — the awx.awx.inventory module fails when a constructed inventory's input_inventories references an inventory name that also exists in another organization.

Root cause

resolve_name_to_id() in controller_api.py calls get_exactly_one() with no scoping data, so a name search across the whole /api/v2/inventories/ endpoint (not scoped to any organization) can return more than one match:

Request to /api/v2/inventories/?name=my-inventory returned 2 items, expected 1

This happens even though the constructed inventory itself is correctly scoped by organization elsewhere in the same module (module.get_one('inventories', name_or_id=name, **{'data': {'organization': org_id}})).

Fix

  1. controller_api.pyresolve_name_to_id() now accepts an optional data kwarg and forwards it to get_exactly_one()/get_one(), the same way get_one() already supports scoped lookups elsewhere in this file. This is fully backward compatible: every other call site in the collection (26 of them, checked) calls resolve_name_to_id(endpoint, name_or_id) with no data kwarg, so this is a pure additive change.
  2. inventory.py — the input_inventories resolution loop now passes data={'organization': org_id} so the lookup is scoped to the same organization as the constructed inventory, matching the pattern already used for the inventory name lookup a few lines above.
  3. Added a regression test (test_constructed_inventory_input_inventories_scoped_to_organization) that creates two organizations each with an inventory of the same name, and verifies a constructed inventory in one organization resolves input_inventories to its own organization's inventory rather than failing on an ambiguous match.

ISSUE TYPE

  • Bug Fix Pull Request

COMPONENT NAME

  • inventory

ADDITIONAL INFORMATION

Before this fix, per the original issue, the only workaround was passing inventory IDs instead of names in input_inventories. This fix makes names work the same way organization scoping already works for the primary inventory lookup.

I traced the root cause against the current devel branch (verified resolve_name_to_id's only caller list before changing its signature) before writing the fix. Happy to adjust the approach if there's a preferred pattern I'm missing — this is my first contribution to AWX.

Summary by CodeRabbit

  • Bug Fixes

    • Constructed inventory input lookups are now limited to the inventory’s organization.
    • Prevented incorrect or ambiguous matches when inventories in different organizations share the same name.
    • Improved name-based lookups when additional search criteria are supplied.
  • Documentation

    • Clarified that input inventory names are resolved within the relevant organization.
    • Added guidance on lookup behavior and supported search criteria.

… filter

Adds an optional `data` kwarg to `resolve_name_to_id()` so callers can
scope name lookups (e.g. by organization) the same way `get_one()`
already supports. Fully backward compatible — every existing caller
passes only (endpoint, name_or_id).

Fixes ansible#16393

Signed-off-by: Maroof Ahmed <93856682+maroofsangi@users.noreply.github.com>
The input_inventories loop called resolve_name_to_id('inventories', item)
without any organization scope, so a name that exists in more than one
organization made the lookup ambiguous and the module failed with
"Request to /api/v2/inventories/?name=X returned 2 items, expected 1",
even though the constructed inventory itself was correctly scoped by org.

Fixes ansible#16393

Signed-off-by: Maroof Ahmed <93856682+maroofsangi@users.noreply.github.com>
Covers ansible#16393: a constructed
inventory referencing an input inventory by name that also exists in
a different organization should resolve to the input inventory in its
own organization, not fail with an ambiguous-match error.

Signed-off-by: Maroof Ahmed <93856882+maroofsangi@users.noreply.github.com>
@github-actions github-actions Bot added component:awx_collection issues related to the collection for controlling AWX community labels Aug 31, 2026
@coderabbitai

coderabbitai Bot commented Aug 31, 2026

Copy link
Copy Markdown

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Team

Run ID: e04bd972-2e3c-493b-bde7-eebbd2b5b11c

📥 Commits

Reviewing files that changed from the base of the PR and between d3d7f16 and d7ca76e.

📒 Files selected for processing (2)
  • awx_collection/plugins/module_utils/controller_api.py
  • awx_collection/plugins/modules/inventory.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • awx_collection/plugins/modules/inventory.py
  • awx_collection/plugins/module_utils/controller_api.py

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The change adds optional query data to resolve_name_to_id. Constructed inventory input lookups now filter inventories by organization. A regression test covers duplicate inventory names across organizations.

Changes

Constructed inventory input scoping

Layer / File(s) Summary
Query-aware name resolution
awx_collection/plugins/module_utils/controller_api.py
resolve_name_to_id accepts optional query data and forwards it to get_exactly_one.
Organization-filtered input inventories
awx_collection/plugins/modules/inventory.py, awx_collection/test/awx/test_inventory.py
input_inventories lookups pass the constructed inventory organization as a filter. Documentation describes the scope. Tests cover duplicate inventory names across organizations.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to d7ca7

Organization-scoped name resolution prevents ambiguous inventory references without introducing a merge-blocking risk; no actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 62.50% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the primary change: scoping input_inventories name lookups to the organization.
Linked Issues check ✅ Passed The changes satisfy issue #16393 by adding a lookup data filter, passing the constructed inventory organization, and testing duplicate inventory names across organizations.
Out of Scope Changes check ✅ Passed The implementation, documentation update, and regression test are directly related to the organization-scoped input_inventories lookup fix.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

Documents the new optional `data` parameter and existing behavior.

Signed-off-by: Maroof Ahmed <93856682+maroofsangi@users.noreply.github.com>
Signed-off-by: Maroof Ahmed <93856682+maroofsangi@users.noreply.github.com>
Signed-off-by: Maroof Ahmed <93856682+maroofsangi@users.noreply.github.com>
Signed-off-by: Maroof Ahmed <93856682+maroofsangi@users.noreply.github.com>
Signed-off-by: Maroof Ahmed <93856682+maroofsangi@users.noreply.github.com>
Signed-off-by: Maroof Ahmed <93856682+maroofsangi@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community component:awx_collection issues related to the collection for controlling AWX

Projects

None yet

Development

Successfully merging this pull request may close these issues.

awx.awx.inventory module input_inventories parameter fails when inventory names are duplicated across organisations

1 participant