Skip to main content

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:

ExampleOn the host
Run a commandruns docker exec osapitest echo hello

The command fails in these cases:

CallFlagsFails with
An empty command--name osapitestmin
No command--name osapitestrequired
A name that is an option--name -f --command truecontainer_id
A working directory that is not absolute--name osapitest --command pwd --working-dir tmpstartswith

Permission​

It needs the docker:exec permission. osapi controller role get --name <role> shows what a role grants.

Flags​

FlagDescriptionDefault
--commandThe command and its arguments (required)
--envEnvironment variables for the command (secrets: NAME=secret://<vault>/<key>)
--nameThe container's name or ID (required)
-T, --targetTarget: _all, _any, hostname, or label (group:web.dev)_all
--working-dirThe directory the command runs in

Global flags​

FlagDescriptionDefault
-d, --debugEnable or disable debug mode
-j, --jsonEnable JSON output
-f, --osapi-filePath to config file; without one at the default path, osapi runs on the defaults/etc/osapi/osapi.yaml
--urlBase URL the CLI client connects to (controller.client.url)https://localhost:8080

Output tells how the result is printed, and what --json gives.