run_playbook
Execute Ansible playbooks using the MCP SysOperator server to manage infrastructure, automate tasks, and customize deployments with additional variables, inventory control, and targeted operations.
Instructions
Run an Ansible playbook
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| extraVars | No | ||
| inventory | No | ||
| limit | No | ||
| playbook | Yes | ||
| tags | No |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"extraVars": {
"additionalProperties": {},
"type": "object"
},
"inventory": {
"type": "string"
},
"limit": {
"type": "string"
},
"playbook": {
"minLength": 1,
"type": "string"
},
"tags": {
"type": "string"
}
},
"required": [
"playbook"
],
"type": "object"
}
Implementation Reference
- The handler function `runPlaybook` that validates paths, builds the ansible-playbook command with options like inventory, extraVars, tags, limit, and executes it using execAsync.export async function runPlaybook(options: RunPlaybookOptions): Promise<string> { const playbookPath = validatePlaybookPath(options.playbook); const inventoryPath = validateInventoryPath(options.inventory); // Build command let command = `ansible-playbook ${playbookPath}`; // Add inventory if specified if (inventoryPath) { command += ` -i ${inventoryPath}`; } // Add extra vars if specified if (options.extraVars && Object.keys(options.extraVars).length > 0) { const extraVarsJson = JSON.stringify(options.extraVars); command += ` --extra-vars '${extraVarsJson}'`; } // Add tags if specified if (options.tags) { command += ` --tags "${options.tags}"`; } // Add limit if specified if (options.limit) { command += ` --limit "${options.limit}"`; } try { // Execute command const { stdout, stderr } = await execAsync(command); return stdout || 'Playbook executed successfully (no output)'; } catch (error) { // Handle exec error const execError = error as { stderr?: string; message: string }; throw new AnsibleExecutionError( `Error running playbook: ${execError.message}`, execError.stderr ); }
- Zod schema `RunPlaybookSchema` defining the input parameters: playbook (required), extraVars, inventory, tags, limit.export const RunPlaybookSchema = z.object({ playbook: z.string().min(1, 'Playbook path is required'), extraVars: z.record(z.any()).optional(), inventory: z.string().optional(), tags: z.string().optional(), limit: z.string().optional(), }); export type RunPlaybookOptions = z.infer<typeof RunPlaybookSchema>;
- src/sysoperator/index.ts:55-59 (registration)Registration of the 'run_playbook' tool in the toolDefinitions map, specifying description, schema, and handler.run_playbook: { description: 'Run an Ansible playbook', schema: RunPlaybookSchema, handler: playbooks.runPlaybook, },