Exec
Run a command in a container.
Runs docker exec, and answers what the command wrote to standard output and to standard error, and its exit code. A command that exits with a code other than 0 is an answer, not a failure. It changes what the command changes, which osapi cannot tell, so it reports changed: true. Its permission is docker:exec, which only the admin role gets, apart from the docker:execute of start and stop. An agent with agent.command.enabled false refuses it, as it refuses a command.
osapi client node docker exec [flags]
Examples
# Run a command
osapi client node docker exec --target web-01 --name osapitest --command echo --command hello
Result
Run a command, on one host:
$ osapi client node docker exec --target web-01 --name osapitest --command echo --command hello Results: HOSTNAME STATUS STDOUT STDERR EXIT CODE web-01 changed hello 0 1 host: 1 changed job 550e8400-e29b-41d4-a716-446655440000 trace 26e448c9dbe1c4a8d216a98c675079c9
On each host, where one cannot run it:
$ osapi client node docker exec --target _all --name osapitest --command echo --command hello Results: HOSTNAME STATUS STDOUT STDERR EXIT CODE web-01 changed hello 0 web-02 changed hello 0 3 hosts: 2 changed, 1 skipped job 550e8400-e29b-41d4-a716-446655440000 trace 26e448c9dbe1c4a8d216a98c675079c9 Details: mac-01 skipped operation not supported on this OS family
With --json, the API's response:
{
"job_id": "550e8400-e29b-41d4-a716-446655440000",
"results": [
{
"changed": true,
"exit_code": 0,
"hostname": "web-01",
"status": "ok",
"stderr": "",
"stdout": "hello\n"
}
]
}
With the default settings, each example does this on the host:
| Example | On the host |
|---|---|
| Run a command | runs docker exec osapitest echo hello |
The command fails in these cases:
| Call | Flags | Fails with |
|---|---|---|
| An empty command | --name osapitest | min |
| No command | --name osapitest | required |
| A name that is an option | --name -f --command true | container_id |
| A working directory that is not absolute | --name osapitest --command pwd --working-dir tmp | startswith |
Permission
It needs the docker:exec permission. osapi controller role get --name <role>
shows what a role grants.
Flags
| Flag | Description | Default |
|---|---|---|
--command | The command and its arguments (required) | |
--env | Environment variables for the command (secrets: NAME=secret://<vault>/<key>) | |
--name | The container's name or ID (required) | |
-T, --target | Target: _all, _any, hostname, or label (group:web.dev) | _all |
--working-dir | The directory the command runs in |
Global flags
| Flag | Description | Default |
|---|---|---|
-d, --debug | Enable or disable debug mode | |
-j, --json | Enable JSON output | |
-f, --osapi-file | Path to config file; without one at the default path, osapi runs on the defaults | /etc/osapi/osapi.yaml |
--url | Base URL the CLI client connects to (controller.client.url) | https://localhost:8080 |
Output tells how the result is printed, and what --json
gives.