Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions osism/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from osism.tasks import reconciler, openstack
from osism import utils
from osism.utils.inventory import get_inventory_path
from osism.utils.inventory import get_hosts_from_inventory, get_inventory_path
from osism.services.listener import BaremetalEvents
from osism.services.websocket_manager import websocket_manager
from osism.services.event_bridge import event_bridge
Expand Down Expand Up @@ -766,9 +766,8 @@ async def get_inventory_hosts(limit: Optional[str] = None) -> HostsResponse:

data = json.loads(result.stdout)
logger.debug(f"Inventory data keys: {list(data.keys())}")
hosts = list(data.get("_meta", {}).get("hostvars", {}).keys())
hosts = get_hosts_from_inventory(data)
logger.debug(f"Found {len(hosts)} hosts in inventory")
hosts.sort()

return HostsResponse(hosts=hosts, count=len(hosts))
except subprocess.TimeoutExpired:
Expand Down Expand Up @@ -1071,7 +1070,7 @@ async def search_inventory(
)

inventory_data = json.loads(result.stdout)
all_hosts = list(inventory_data.get("_meta", {}).get("hostvars", {}).keys())
all_hosts = get_hosts_from_inventory(inventory_data)

# Filter hosts by pattern
if host_regex:
Expand Down
4 changes: 2 additions & 2 deletions osism/commands/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from osism.tasks import Config
from osism.utils import redis
from osism.utils.inventory import get_inventory_path
from osism.utils.inventory import get_hosts_from_inventory, get_inventory_path


class VersionsManager(Command):
Expand Down Expand Up @@ -249,7 +249,7 @@ def take_action(self, parsed_args):
data = json.loads(result)
table = []

for host in data["_meta"]["hostvars"]:
for host in get_hosts_from_inventory(data):
table.append([host])

if table:
Expand Down
8 changes: 4 additions & 4 deletions osism/commands/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from osism import settings
from osism.commands.console import resolve_host_with_fallback
from osism.utils.inventory import get_inventory_path
from osism.utils.inventory import get_hosts_from_inventory, get_inventory_path
from osism.utils.ssh import ensure_known_hosts_file, KNOWN_HOSTS_PATH


Expand Down Expand Up @@ -56,7 +56,7 @@ def take_action(self, parsed_args):
return

data = json.loads(result.stdout)
hosts = sorted(data.get("_meta", {}).get("hostvars", {}).keys())
hosts = get_hosts_from_inventory(data)

if not hosts:
logger.error("No hosts found in inventory.")
Expand Down Expand Up @@ -194,7 +194,7 @@ def take_action(self, parsed_args):
return

data = json.loads(result.stdout)
hosts = sorted(data.get("_meta", {}).get("hostvars", {}).keys())
hosts = get_hosts_from_inventory(data)

if not hosts:
logger.error("No hosts found in inventory.")
Expand Down Expand Up @@ -341,7 +341,7 @@ def take_action(self, parsed_args):
return

data = json.loads(result.stdout)
hosts = sorted(data.get("_meta", {}).get("hostvars", {}).keys())
hosts = get_hosts_from_inventory(data)

if not hosts:
logger.error("No hosts found in inventory.")
Expand Down
15 changes: 15 additions & 0 deletions osism/utils/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,18 @@ def get_inventory_path(base_path: str, prefer_minified: bool = True) -> str:
logger.debug(f"Using minified inventory: {minified_path}")
return minified_path
return base_path


def get_hosts_from_inventory(data: dict) -> list:
"""Extract host names from ansible-inventory --list JSON output.

The minified inventory does not populate _meta.hostvars (since hosts
have no variables), so we also collect hosts from group listings.
"""
hosts = set(data.get("_meta", {}).get("hostvars", {}).keys())
for key, value in data.items():
if key == "_meta":
continue
if isinstance(value, dict) and "hosts" in value:
hosts.update(value["hosts"])
return sorted(hosts)
9 changes: 2 additions & 7 deletions osism/utils/rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from osism.tasks.conductor.utils import get_vault
from osism.utils import redis
from osism.utils.inventory import get_inventory_path
from osism.utils.inventory import get_hosts_from_inventory, get_inventory_path


def get_rabbitmq_node_addresses():
Expand All @@ -30,12 +30,7 @@ def get_rabbitmq_node_addresses():
)
inventory = json.loads(result)

# Get hosts from _meta.hostvars (contains all hosts matching the limit)
if "_meta" not in inventory or "hostvars" not in inventory["_meta"]:
logger.error("Invalid inventory format: _meta.hostvars not found")
return None

rabbitmq_hosts = list(inventory["_meta"]["hostvars"].keys())
rabbitmq_hosts = get_hosts_from_inventory(inventory)
if not rabbitmq_hosts:
logger.error("No hosts found in rabbitmq group")
return None
Expand Down