Skip to main content
Glama

list_inventory

View Ansible inventory hosts and groups to manage infrastructure configurations and deployments.

Instructions

List Ansible inventory hosts and groups

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
inventoryNo

Implementation Reference

  • The handler function that runs 'ansible-inventory --list' to list hosts and groups, formats as pretty JSON, with error handling.
    export async function listInventory(options: ListInventoryOptions): Promise<string> {
      const inventoryPath = validateInventoryPath(options.inventory);
      
      // Build command
      let command = 'ansible-inventory';
      
      // Add inventory if specified
      if (inventoryPath) {
        command += ` -i ${inventoryPath}`;
      }
      
      command += ' --list';
    
      try {
        // Execute command
        const { stdout, stderr } = await execAsync(command);
        
        try {
          // Try to parse as JSON for better formatting
          const inventory = JSON.parse(stdout);
          return JSON.stringify(inventory, null, 2);
        } catch {
          // Fall back to raw output if can't parse as JSON
          return stdout || 'No inventory data returned';
        }
      } catch (error) {
        // Handle exec error
        const execError = error as { stderr?: string; message: string };
        throw new AnsibleExecutionError(
          `Error listing inventory: ${execError.message}`,
          execError.stderr
        );
      }
    }
  • Zod schema for input validation of the list_inventory tool, accepting an optional inventory path.
    export const ListInventorySchema = z.object({
      inventory: z.string().optional(),
    });
    
    export type ListInventoryOptions = z.infer<typeof ListInventorySchema>;
  • Tool registration in the toolDefinitions object, specifying name, description, schema, and handler reference.
    list_inventory: {
      description: 'List Ansible inventory hosts and groups',
      schema: ListInventorySchema,
      handler: inventory.listInventory,
    },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/tarnover/mcp-sysoperator'

If you have feedback or need assistance with the MCP directory API, please join our Discord server