Below is a minimal, self-contained automation you can paste into Cortex XSOAR (type = Python).
Save it as RunSubPlaybookAdhoc (or any name you like) and mark it as “Run from War-Room”.
The script:
- Receives the sub-playbook name as an argument (
playbook_name).
- Adds that playbook as an ad-hoc task to the current incident’s work-plan.
- Immediately executes the newly-created task.
from CommonServerPython import *
def main():
pb_name = demisto.args().get('playbook_name')
if not pb_name:
return_error('playbook_name argument is required.')
incident_id = demisto.incident()['id']
# 1. Add the playbook as an ad-hoc task
add_task_res = demisto.executeCommand(
'addTask',
{
'incidentId': incident_id,
'name': f'Ad-hoc: {pb_name}',
'type': 'playbook',
'playbookName': pb_name,
'section': '' # empty = bottom of the work-plan
}
)
if isError(add_task_res):
return_error(f'Failed to add task: {get_error(add_task_res)}')
# The addTask command returns the newly-created task ID
task_id = add_task_res[0]['EntryContext']['Task']['id']
# 2. Run the task
run_res = demisto.executeCommand(
'executeTask',
{
'incidentId': incident_id,
'taskId': task_id
}
)
if isError(run_res):
return_error(f'Task created but failed to execute: {get_error(run_res)}')
return_results(f'Sub-playbook "{pb_name}" added and started (task ID {task_id}).')
if __name__ in ('__main__', '__builtin__', 'builtins'):
main()
Usage from the War-Room
!RunSubPlaybookAdhoc playbook_name="My_Sub_Playbook_Name"
Notes
- The automation uses two built-in server commands (
addTask and executeTask) that are available in every XSOAR tenant.
- The task is appended to the bottom of the work-plan (section = empty string).
- If you want the task in a specific section, pass the section name in the
section parameter.