list_inventory
Retrieve and display Ansible inventory hosts and groups using the specified inventory file for efficient server management.
Instructions
List Ansible inventory hosts and groups
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| inventory | No |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"inventory": {
"type": "string"
}
},
"type": "object"
}
Implementation Reference
- The handler function that runs 'ansible-inventory --list' on the specified inventory path, parses the JSON output if possible, and returns formatted inventory data or raw output. Handles validation and errors.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 list_inventory tool input: optional 'inventory' path string. Includes TypeScript type inference.export const ListInventorySchema = z.object({ inventory: z.string().optional(), }); export type ListInventoryOptions = z.infer<typeof ListInventorySchema>;
- src/sysoperator/index.ts:60-64 (registration)Tool registration in toolDefinitions map, specifying name 'list_inventory', description, schema, and handler reference.list_inventory: { description: 'List Ansible inventory hosts and groups', schema: ListInventorySchema, handler: inventory.listInventory, },