Skip to main content
Glama
README.md
# locomotive

A self-hosted web app and MCP server for running the Ansible Playbooks in one Git repo: preview, launch,
queue, schedule, and keep a history of every Run.

It replaces a general-purpose Ansible UI with something small and opinionated: one repo, one process, one
SQLite database, and no login page (it sits behind a reverse proxy that supplies the user's identity).

## Features

- **Launch form** with live previews of the hosts and tasks a Run will touch, before it starts
- **Clean Runs** of any remote branch, or **Working Copy Runs** in place, uncommitted changes and all
- **Per-host queue**: two Runs never touch the same host at once
- **Run page** with a task × host grid, recap, live log, Stop and Kill
- **Schedules** (cron) with a Scheduler Ping to a healthchecks URL
- **ntfy notifications** for failed, interrupted, missed and changed Runs
- **MCP server** at `/mcp`, so Claude Code and other agents can preview and start Runs
- **Log retention**: per-Run log cap, gzip, and daily pruning under a disk budget

Every Run's history row is kept forever. Not supported: multiple repos, users with different permissions,
and logging in without a reverse proxy.

## What your Ansible repo needs

locomotive runs Playbooks from the repo at `LOCOMOTIVE_REPO_URL`. That repo needs:

- `playbooks/*.yml`: each top-level file is a Playbook locomotive offers to run
  (hide some with `LOCOMOTIVE_HIDDEN_PLAYBOOKS`)
- Its inventory set by the repo's own Ansible config (locomotive passes no `-i`)
- Optionally `collections/requirements.yml`, installed into a cache per Run
- Optionally a `mise.toml` pinning the Ansible versions (see [Where Ansible comes from](#where-ansible-comes-from))
- Optionally a Description for each Playbook and Tag, shown on the Launch screen:
  - a Playbook's is the block of `#` comment lines at the top of the file (a leading `---` is fine).
    A bare `#` line starts a new paragraph. The first paragraph is the summary shown in the Playbook list.
  - Tags get theirs from `locomotive.yml` at the repo root:

    ```yaml
    tags:
      docker: Install and configure Docker
      hardening: SSH, firewall and sysctl lockdown
    ```

### Where Ansible comes from

Each time a Run or preview prepares its runtime, locomotive checks the root of that checkout (the commit,
or the Working Copy as it is on disk):

- **With a `mise.toml`:** locomotive runs `mise install`, then runs every Ansible tool through
  `mise exec`. The `mise.toml` must pin `ansible-core` and `ansible-runner`. Each commit or branch gets the
  versions it pins. If `mise` isn't installed, the Run fails; locomotive does not fall back to PATH.
- **Without a `mise.toml`:** locomotive uses the `ansible`, `ansible-runner` and `ansible-galaxy` on its
  own PATH, with no install step. If any of them is missing, the Run fails and its reason names the missing
  executables. Every checkout uses the same versions.

The `mise.toml` is a [mise](https://mise.jdx.dev) config at the repo root that pins ansible-core, with
ansible-runner in the same install. Pinning makes a Run reproducible from its commit. A Working Copy whose
`mise.toml` or `collections/requirements.yml` changes while a Run is queued gets its Runtime prepared again
before the Run starts. This minimum file works:

```toml
[tools]
"pipx:ansible-core" = { version = "2.21.0", uvx_args = "--python 3.13 --with-executables-from ansible-runner" }
```

- `pipx:ansible-core` installs ansible-core into an isolated install of its own (mise uses `uvx` for this).
- `--with-executables-from ansible-runner` puts `ansible-runner` in that same install. locomotive
  starts every Run through `ansible-runner`, so it must sit next to ansible-core.

Change `version` to pin a different ansible-core.

Collections from `collections/requirements.yml` are installed the same way on both paths. The Run log
says which path was taken. In a Working Copy, adding or removing `mise.toml` switches paths on the next
Run or preview.

Ansible runs inside locomotive's own process tree, so "PATH" means the container's PATH under Docker and
the machine's PATH on bare metal. The published image ships `mise` but no Ansible. To run a repo without a
`mise.toml` under Docker, extend the image:

```dockerfile
FROM ghcr.io/tedkulp/locomotive:latest
USER root
RUN uv pip install --python /app/.venv/bin/python --no-cache ansible-core ansible-runner
USER 1000:1000
```

On bare metal (`python -m locomotive` outside Docker), install `ansible-core` and `ansible-runner` any way
you like, as long as all three executables are on the PATH locomotive starts with.

## Quick start (Docker)

Requires Docker with Compose.

```sh
cp .env.example .env   # set LOCOMOTIVE_REPO_URL and the three access-guard values
docker compose pull
docker compose up -d
```

This runs the published image, `ghcr.io/tedkulp/locomotive` (`linux/amd64` and `linux/arm64`).
`LOCOMOTIVE_TAG` picks the image tag:

| `LOCOMOTIVE_TAG` | Image |
| --- | --- |
| `latest` (default) | The newest release |
| `0.1.5`, `0.1` | A release, or the newest patch of a minor version |
| `dev` | The tip of `main` (also published as `sha-<short sha>`) |

To build from source instead, run `docker compose up --build`. The local build takes the same image name,
so run `docker compose pull` to go back to the published image.

The app listens on `127.0.0.1:8000` (change with `LOCOMOTIVE_BIND` / `LOCOMOTIVE_HOST_PORT`) and keeps
its data in `./data` (`LOCOMOTIVE_DATA_PATH`). Put it behind a reverse proxy that sets the identity header.

## Configuration

All settings are env vars prefixed `LOCOMOTIVE_`. `.env.example` lists them, and
`src/locomotive/settings.py` is the full source.

| Variable | Purpose |
| --- | --- |
| `REPO_URL` (required) | The Ansible repo to run Playbooks from. Add `REPO_USERNAME` / `REPO_TOKEN` for a private repo |
| `DATA_DIR` (required outside Docker) | Database, mirror, Run dirs, logs and caches |
| `TRUSTED_PROXY_IPS`, `IDENTITY_HEADER`, `ALLOWLIST` | Access guard: which peers may connect and which identities get in. Unset fails closed |
| `DEFAULT_BRANCH` | Branch for clean Runs and Schedules (default `main`) |
| `WORKING_COPY_PATH` | A clone to allow Working Copy Runs in |
| `MCP_TOKENS`, `PUBLIC_HOSTNAME` | MCP clients' hashed bearer tokens, and the hostname they use |
| `NTFY_URL`, `NTFY_TOPIC`, `NTFY_TOKEN` | Notifications (off unless URL and topic are set) |
| `HEALTHCHECKS_URL` | Where the Scheduler Ping goes |
| `TIME_ZONE` | IANA zone for cron and housekeeping (unset: `TZ`, then `/etc/localtime`, then UTC) |
| `LOG_CAP_BYTES`, `LOG_BUDGET_BYTES` | Per-Run log cap (200 MB) and budget for all logs (10 GB) |

## Connecting an MCP client

1. Make a token: `TOKEN=$(openssl rand -hex 32)`
2. Hash it: `printf %s "$TOKEN" | sha256sum`
3. Add `LOCOMOTIVE_MCP_TOKENS=<name>=<sha256>` (comma-separate several) and restart
4. Register it:

   ```sh
   claude mcp add --transport http locomotive https://<public hostname>/mcp --header "Authorization: Bearer $TOKEN"
   ```

`start_run` uses check mode unless called with `confirm: true`. See [docs/behaviour.md](docs/behaviour.md#mcp)
for the tool list.

## Development

Requires [mise](https://mise.jdx.dev) (pins Python 3.13, uv, Node 22 and pnpm in `mise.toml`).

```sh
mise install
uv sync                        # backend deps
uv run pytest                  # backend tests
cd frontend && pnpm install
pnpm typecheck && pnpm build   # frontend checks, built to frontend/dist
```

Run it locally. The access guard needs a trusted peer and an identity, so have your browser (e.g. a header
extension) send `X-User: me`:

```sh
LOCOMOTIVE_DATA_DIR=./data LOCOMOTIVE_REPO_URL=<repo url> LOCOMOTIVE_STATIC_DIR=frontend/dist \
LOCOMOTIVE_TRUSTED_PROXY_IPS=127.0.0.1 LOCOMOTIVE_IDENTITY_HEADER=X-User LOCOMOTIVE_ALLOWLIST=me \
uv run python -m locomotive    # serves http://127.0.0.1:8000
cd frontend && pnpm dev        # optional: Vite dev server proxying /api to :8000
```

### Layout

- `src/locomotive/`: FastAPI app (one process: API, Run supervisor, scheduler), SQLite via SQLAlchemy + Alembic
- `frontend/`: React + Vite + shadcn/ui, built to static files the backend serves
- `tests/`: the seam 1 harness (app in-process over real HTTP, stub toolchain, local bare git remote, fake
  clock, fake HTTP server) and seam 2, `tests/test_smoke_real_ansible.py`: the real preview helper and one real
  `ansible-runner` Run against `localhost`. Seam 2 needs `mise` on PATH (skipped otherwise) and installs its
  Ansible runtime on first use.

## More

- [docs/behaviour.md](docs/behaviour.md): the detailed rules for the queue, Working Copy, previews,
  Schedules, notifications, log retention and MCP
- [docs/describing-an-ansible-repo.md](docs/describing-an-ansible-repo.md): instructions to hand an LLM
  (or follow yourself) to add Playbook and Tag Descriptions to your Ansible repo
- [GLOSSARY.md](GLOSSARY.md): what Run, Playbook, Working Copy, Schedule and Scheduler Ping mean here
- Issues: GitHub Issues on this repo

## License

[MIT](LICENSE)