Overview

Date: 2026-05-11

Time: 15:43

Overview

This is a comprehensive example/demo script showing how to use FTL2's local module execution system (ftl2.ftlmodules). It demonstrates both the synchronous direct-call API (calling module functions like ftlfile, ftlcopy, ftlcommand, ftlshell directly) and the async executor API (execute, executebatch, executeonhosts, run). The script emphasizes FTL2's 250x+ speedup over Ansible's subprocess-based module execution by running modules in-process.

Usage Patterns

Direct synchronous module calls — simplest way to use FTL modules:


from ftl2.ftl_modules import ftl_file, ftl_copy, ftl_command, ftl_shell

# File operations (create, touch, delete)
result = ftl_file(path="/tmp/mydir", state="directory")
result = ftl_file(path="/tmp/myfile", state="touch")
result = ftl_file(path="/tmp/myfile", state="absent")

# Copy with optional backup
result = ftl_copy(src="/tmp/a.txt", dest="/tmp/b.txt", backup=True)

# Command execution (no shell interpretation)
result = ftl_command(cmd="echo hello", chdir="/tmp", creates="/tmp/marker")

# Shell execution (supports pipes, env vars)
result = ftl_shell(cmd="echo $HOME | wc -c")

Async executor API — for concurrent and multi-host execution:


from ftl2.ftl_modules import execute, execute_batch, execute_on_hosts, run, LocalHost

# Single module execution
result = await execute("file", {"path": "/tmp/test", "state": "touch"})
# result.success, result.changed, result.module, result.used_ftl

# Convenience wrapper
result = await run("command", cmd="date")

# Batch concurrent execution
tasks = [
    ("file", {"path": "/tmp/f1", "state": "touch"}, None),
    ("command", {"cmd": "echo hi"}, None),
]
results = await execute_batch(tasks)

# Parallel execution across hosts
hosts = [LocalHost(name=f"worker-{i}") for i in range(5)]
results = await execute_on_hosts(hosts, "command", {"cmd": "echo hello"})

API and Configuration

Direct module functions return plain dicts with keys: changed, path/src/dest/stdout/rc, state, mode, backup_file.

Executor API returns result objects with attributes: success, changed, module, used_ftl, output (dict), error, host.

Key parameters by module:

Key Behaviors

Relationships