condition_from_command
Execute a command and create result conditions depending on its exit code.
⚙️ Compatible targets: Linux, Windows
Parameters
Name | Documentation |
---|---|
condition | The condition name. This parameter is required. |
command | The command to run. This parameter is required. |
true_codes | List of codes that produce a true status separated with commas (ex: 1,2,5). This parameter is required. |
false_codes | List of codes that produce a false status separated with commas (ex: 3,4,6). This parameter is required. |
Outcome conditions
You need to replace ${condition}
with its actual canonified value.
- ✅ Ok:
condition_from_command_${condition}_ok
- ☑️ Already compliant:
condition_from_command_${condition}_kept
- 🟨 Repaired:
condition_from_command_${condition}_repaired
- ☑️ Already compliant:
- ❌ Error:
condition_from_command_${condition}_error
Example
method: condition_from_command
params:
command: VALUE
condition: VALUE
true_codes: VALUE
false_codes: VALUE
Documentation
This method executes a command, and defines a ${condition}_true
or a
${condition}_false
condition depending on the result of the command:
- If the exit code is in the "True codes" list, this will produce a
kept outcome and a
${condition}_true
condition, - If the exit code is in the "False codes" list, this will produce a
kept outcome and a
${condition}_false
condition, - If the exit code is not in "True codes" nor in "False codes", or if
the command can not be found, it will produce an
error outcome and
and no condition from
${condition}
The created condition is global to the agent.
Windows
On Windows nodes, the exit code is taken from the LASTEXITCODE
which is defined either by:
- The exit code of a binary execution (when the command a call to an exe)
- The return code of a Powershell script
Direct Powershell execution will almost always return 0 as LASTEXITCODE
value, meaning that you have to execute either a binary or a Powershell
script to control the return code.
Example:
If you run a command /bin/check_network_status
that output code 0, 1 or 2 in
case of correct configuration, and 18 or 52 in case of invalid configuration,
and you want to define a condition based on its execution result,
you can use:
condition_from_command("network_correctly_defined", "/bin/check_network_status", "0,1,2", "18,52")
-
If the command exits with 0, 1 or 2, then it will define the conditions
network_correctly_defined_true
,condition_from_command_network_correctly_defined_kept
,condition_from_command_network_correctly_defined_reached
,
-
If the command exits 18, 52, then it will define the conditions
network_correctly_defined_false
,condition_from_command_network_correctly_defined_kept
,condition_from_command_network_correctly_defined_reached
-
If the command exits any other code or is not found, then it will define the conditions
condition_from_command_network_correctly_defined_error
,condition_from_command_network_correctly_defined_reached
Notes:
-
In audit mode, this method will still execute the command passed in parameter. Which means that you should only pass non system-impacting commands to this method.
-
Rudder will automatically "canonify" the given Condition prefix at execution time, which means that all non
[a-zA-Z0-9_]
characters will be replaced by an underscore.