Skip to main content
Glama

run_ad_hoc

Execute Ansible ad-hoc commands on specified hosts to manage infrastructure operations directly from AI assistants.

Instructions

Run an Ansible ad-hoc command against specified hosts

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
patternYes
moduleNoshell
argsNo
inventoryNo
becomeNo
extra_varsNo

Implementation Reference

  • The handler function that builds and executes the Ansible ad-hoc command using the provided options.
    export async function runAdHoc(options: RunAdHocOptions): Promise<string> {
      const inventoryPath = validateInventoryPath(options.inventory);
      
      // Build command
      let command = `ansible ${options.pattern}`;
      
      // Add module
      command += ` -m ${options.module}`;
      
      // Add module args if specified
      if (options.args) {
        command += ` -a "${options.args}"`;
      }
      
      // Add inventory if specified
      if (inventoryPath) {
        command += ` -i ${inventoryPath}`;
      }
      
      // Add become flag if needed
      if (options.become) {
        command += ' --become';
      }
      
      // Add extra vars if specified
      if (options.extra_vars && Object.keys(options.extra_vars).length > 0) {
        const extraVarsJson = JSON.stringify(options.extra_vars);
        command += ` --extra-vars '${extraVarsJson}'`;
      }
    
      try {
        // Execute command
        const { stdout, stderr } = await execAsync(command);
        return stdout || 'Command executed successfully (no output)';
      } catch (error) {
        // Handle exec error
        const execError = error as { stderr?: string; message: string };
        throw new AnsibleExecutionError(
          `Error running ad-hoc command: ${execError.message}`,
          execError.stderr
        );
      }
    }
  • Zod schema defining the input parameters for the run_ad_hoc tool, including host pattern, module, args, etc.
    export const RunAdHocSchema = z.object({
      pattern: z.string().min(1, 'Host pattern is required'),
      module: z.string().default('shell'),
      args: z.string().optional(),
      inventory: z.string().optional(),
      become: z.boolean().optional(),
      extra_vars: z.record(z.any()).optional(),
    });
    
    export type RunAdHocOptions = z.infer<typeof RunAdHocSchema>;
  • Registration of the 'run_ad_hoc' tool in the toolDefinitions map, associating it with its schema and handler function.
    run_ad_hoc: {
      description: 'Run an Ansible ad-hoc command against specified hosts',
      schema: RunAdHocSchema,
      handler: adHoc.runAdHoc,
    },

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