Skip to main content
Glama

aws_dynamic_inventory

Generate dynamic AWS inventory for Ansible by specifying regions, filtering resources, customizing hostnames, and grouping instances with defined keys. Streamlines infrastructure management through automated updates.

Instructions

Create AWS dynamic inventory

Input Schema

NameRequiredDescriptionDefault
composeNo
filtersNo
hostnamesNo
keyed_groupsNo
regionYes

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "compose": { "additionalProperties": { "type": "string" }, "type": "object" }, "filters": { "additionalProperties": {}, "type": "object" }, "hostnames": { "items": { "type": "string" }, "type": "array" }, "keyed_groups": { "items": { "additionalProperties": false, "properties": { "key": { "type": "string" }, "prefix": { "type": "string" }, "separator": { "type": "string" } }, "required": [ "prefix", "key" ], "type": "object" }, "type": "array" }, "region": { "minLength": 1, "type": "string" } }, "required": [ "region" ], "type": "object" }

Implementation Reference

  • Main handler function that creates an AWS EC2 dynamic inventory YAML using the amazon.aws.aws_ec2 plugin, writes it to a temporary file, runs ansible-inventory --list to display the inventory structure, and tests it with a simple ping playbook.
    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 input schema for the aws_dynamic_inventory tool defining parameters: region (required), filters, hostnames (array), keyed_groups (array of objects with prefix, key, separator), compose (object).
    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() });
  • Registration of the 'aws_dynamic_inventory' tool in the toolDefinitions Record, specifying its description, input schema, and handler function.
    aws_dynamic_inventory: { description: 'Create AWS dynamic inventory', schema: aws.DynamicInventorySchema, handler: aws.dynamicInventoryOperations, },

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-ansible'

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