aws_dynamic_inventory
Generate dynamic Ansible inventory from AWS resources by region, applying filters, hostname mapping, and grouping for infrastructure automation.
Instructions
Create AWS dynamic inventory
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| region | Yes | ||
| filters | No | ||
| hostnames | No | ||
| keyed_groups | No | ||
| compose | No |
Implementation Reference
- Handler function that generates AWS EC2 dynamic inventory YAML using the aws_ec2 plugin, saves it to a temp file, runs 'ansible-inventory --list' to display structure, and tests with a ping playbook on discovered hosts.export async function dynamicInventoryOperations(args: DynamicInventoryOptions): Promise<string> { await verifyAwsCredentials(); const { region, filters, hostnames, keyed_groups, compose } = args; let inventoryContent = `--- plugin: amazon.aws.aws_ec2 regions: - ${region}`; if (filters) { inventoryContent += ` filters: ${formatYamlParams(filters, 2)}`; // Indent level 2 for filters } if (hostnames && hostnames.length > 0) { inventoryContent += ` hostnames: ${hostnames.map(h => ` - ${JSON.stringify(h)}`).join('\n')}`; } if (keyed_groups && keyed_groups.length > 0) { inventoryContent += ` keyed_groups: ${keyed_groups.map(group => ` - prefix: ${group.prefix}\n key: ${group.key}\n separator: ${group.separator ?? ''}`).join('\n')}`; } if (compose) { inventoryContent += ` compose: ${formatYamlParams(compose, 2)}`; // Indent level 2 for compose } // This operation doesn't run a playbook, it *generates* an inventory file // and then tests it. We'll adapt the helper pattern slightly. let tempDir: string | undefined; try { tempDir = await createTempDirectory('ansible-aws-dyninv'); const inventoryPath = await writeTempFile(tempDir, 'inventory.aws_ec2.yml', inventoryContent); // Create a simple test playbook const testPlaybookContent = `--- - name: Test AWS Dynamic Inventory hosts: all # Target hosts defined by the dynamic inventory gather_facts: no tasks: - name: Ping hosts found by dynamic inventory ansible.builtin.ping:`; const testPlaybookPath = await writeTempFile(tempDir, 'test_playbook.yml', testPlaybookContent); // Execute ansible-inventory --list first to show the structure const listCommand = `ansible-inventory -i ${inventoryPath} --list`; console.error(`Executing: ${listCommand}`); const listResult = await execAsync(listCommand); // Execute the test playbook using the dynamic inventory const runCommand = `ansible-playbook -i ${inventoryPath} ${testPlaybookPath}`; console.error(`Executing: ${runCommand}`); const runResult = await execAsync(runCommand); return `Dynamic Inventory (${inventoryPath}) Content:\n${inventoryContent}\n\nInventory List Output:\n${listResult.stdout}\n\nPlaybook Test Output:\n${runResult.stdout}`; } catch (error: any) { const errorMessage = error.stderr || error.message || 'Unknown error'; throw new AnsibleExecutionError(`Failed dynamic inventory operation: ${errorMessage}`, error.stderr); } finally { if (tempDir) { await cleanupTempDirectory(tempDir); } } }
- Zod schema defining the input parameters for the aws_dynamic_inventory tool: required AWS region, optional filters, hostnames, keyed_groups, and compose options.export const DynamicInventorySchema = z.object({ region: z.string().min(1, 'AWS region is required'), filters: z.record(z.any()).optional(), // Changed hostnames to allow array of strings based on aws.ts usage hostnames: z.array(z.string()).optional(), // Changed keyed_groups to match structure used in aws.ts keyed_groups: z.array(z.object({ prefix: z.string(), key: z.string(), separator: z.string().optional() })).optional(), // Added compose based on usage in aws.ts compose: z.record(z.string()).optional() }); export type DynamicInventoryOptions = z.infer<typeof DynamicInventorySchema>;
- src/sysoperator/index.ts:136-140 (registration)Registers the 'aws_dynamic_inventory' tool in the toolDefinitions map, linking to its description, input schema, and handler function.aws_dynamic_inventory: { description: 'Create AWS dynamic inventory', schema: aws.DynamicInventorySchema, handler: aws.dynamicInventoryOperations, },