Running Lemano from scripts and agents
A script, a CI job or a coding agent drives Lemano without a terminal. It branches on exit codes and on JSON, not on wording. This guide lists what it can rely on.
JSON output
Every command that prints a document takes -o json (and -o yaml):
target list, target show, state list, state show, state pull,
diff, history, discover, baseline, verify, summary,
secret list, version, revert, and lemano ansible check, import
and export. The document is one JSON object on standard output, and it
opens with its format version (the declaration state pull writes, a
Spec, has its apiVersion instead):
$ lemano version -o json
{
"formatVersion": "1.0",
"engine": "v0.1.0",
...
}
formatVersion has a major and a minor number. A new field raises the
minor number; a field removed or changed in meaning raises the major
number. A reader checks the major number and ignores the fields it does
not know. A release prints its version in engine, such as v0.1.0; a
build from source prints dev.
Errors
With -o json, a failure prints one JSON object on standard error, and
none of the error: and hint: lines:
{"formatVersion":"1.0","error":{"reason":"LockHeld","exitCode":5,"message":"web-07 is locked by another command","hint":"if that command is no longer running, release the lock: lemano unlock web-07 --force","target":"web-07"}}| Field | Meaning |
|---|---|
reason | what failed, as a word to test: Invalid (a wrong command line), TrialPending, UnfinishedTrial, NoUnfinishedTrial, NotFound, Unreachable, HostKeyUnknown, HostKeyChanged, HostKeyUnexpected, IdentityChanged, LockHeld, Integrity, Partial, ConfirmationRequired, Canceled, Internal, and the others of the API |
exitCode | the exit code of the command |
message | the explanation, for people; never a secret |
hint | what to do next, when there is something; left out otherwise |
target | the target concerned, when there is one; left out otherwise |
details | the facts behind some reasons, as data, so a script never parses the message; left out otherwise (see below) |
A revert or target end-trial that is refused for a trial says why in
the reason, with its facts in details, and exits 2 like any request it
cannot accept:
reason | details |
|---|---|
TrialPending | generation of the trial and endsAt, when the host undoes it: discover the target after that, then revert again |
UnfinishedTrial | trials, the IDs of the trials left on the host: finish one with lemano target end-trial NAME --undo or --keep (--trial ID when there are several) |
NoUnfinishedTrial | none: end-trial found nothing to finish |
{"formatVersion":"1.0","error":{"reason":"UnfinishedTrial","exitCode":2,"message":"invalid request: an unfinished trial is on web-07 (3f9a...); finish it first with: lemano target end-trial web-07 --undo or --keep","target":"web-07","details":{"trials":["3f9a..."]}}}A wrong command line is an error object too, even when it fails before the command runs (an unknown flag):
{"formatVersion":"1.0","error":{"reason":"Invalid","exitCode":2,"message":"unknown flag: --frobnicate","hint":"Run 'lemano --help' for usage."}}Read the last line of standard error. Log lines (a warning about an ssh configuration, for example) go to standard error too, before the failure; the error object is always written last, after everything else, and is one line.
When a command reports each target in its own document (discover does:
results[].error), standard error holds one object for the whole run: the
error of the target that failed, or a count when several did. Without
-o json the output is the text it always was.
-o yaml prints documents in YAML; a failure is still printed as text.
Exit codes
See exit codes. The ones a script
most often tests: 0 success, 2 a wrong command line, 3 a target
unreachable, 4 a host identity problem, 5 a lock held, 6 a history that
does not verify, 7 a
partial result, 8 a change (with --detailed-exitcode), 9 an approval
missing. Treat any code you do not know as a failure.
Nothing waits for a terminal
No command waits on a question when there is no terminal to ask on: it fails at once with a reason. The questions are:
| Question | Without a terminal |
|---|---|
revert asks to approve | shows its plan and fails with exit 9, below |
revert on a trial asks to keep the changes | fails before writing unless --auto-confirm or --no-trial is given |
lemano ansible codify --write asks to approve | fails before any work, exit 2: give --auto-approve, or --out FILE for a patch |
secret set asks for the value | fails, exit 2: give the value on standard input with --from-stdin |
--password-prompt, password: prompt | the target is not reached, exit 3: give the password as env:, file: or secret: |
lemano ansible export --with-secrets asks for the vault password | fails, exit 2: give --vault-password-file |
A value meant to come from standard input, such as secret set --from-stdin,
keeps working.
Reverting: show, then approve
lemano revert is the one command that writes on a server. Without a
terminal to ask on, or with -o json, and without --expect-plan,
--auto-approve or --dry-run, it does what --dry-run does: it reads
the host, prints the plan on standard output in the chosen format, writes
nothing on the server and records no revert (the fresh reading is recorded
like any --dry-run’s), and then fails with exit code 9 and the reason
ConfirmationRequired. The hint holds the command that approves the plan
shown: yours plus --expect-plan DIGEST.
Every plan carries a digest, plan.planDigest in -o json and -o yaml
and a Plan digest: line in text: a keyed hash of what this revert would
do on the host (the target, the generation, the options that change the
writes, and for each change its path or key, its action, and the hashes,
mode and owner of the file before and after). It says nothing about the
content of a file, and it does not change when something else changes on
the host: a service restarting, a package upgrade or a file the revert
does not touch leave it alone.
$ lemano revert web-07 --to 12 /etc/ssh/sshd_config -o json > plan.json
$ echo $?
9
Standard output holds the plan (plan.changes lists each file and kernel
parameter with its content diff and its mode), and standard error holds:
{"formatVersion":"1.0","error":{"reason":"ConfirmationRequired","exitCode":9,"message":"the revert of web-07 to generation 12 needs approval and there is no one to ask: nothing was written","hint":"run this command to approve the changes shown above; it reads the server again and applies this plan only if it is unchanged: lemano revert web-07 --to 12 /etc/ssh/sshd_config -o json --expect-plan hmac-sha256:3f0c5e1a9b7d4c2e8a6f0b1d3c5e7a9b2d4f6081a3c5e7092b4d6f8a0c2e49a1 (the revert runs as a trial: add --auto-confirm to keep the changes once the fresh login worked, or --no-trial)","target":"web-07"}}Read the plan, then run the command of the hint. Passing --expect-plan
is the approval of the plan you were shown, and no --auto-approve is
needed (giving both is a usage error). The run reads the server again and
builds its plan:
- with the digest you gave, it applies that plan; a file that changed during the run is still refused just before it is written (exit 7), as in any revert;
- with another digest (something the revert touches changed since the preview), it writes nothing, prints the new plan and fails again with exit 9; the hint has the command for the new plan. Show the new plan before approving it;
- with nothing left to revert, it says so and exits 0.
--auto-approve accepts whatever plan the run builds, so use it only in a
script that accepts any plan. The second part of the hint appears when the
plan runs as a trial, which a revert of a file that controls access to the
host does: without a terminal a trial still needs --auto-confirm or
--no-trial. A plan with nothing to revert needs no approval and exits 0.
lemano ansible revert does the same and names its own command in the
hint.