Using Conditional Logic in a Job
Using Conditional Logic in a Job
Generally Available in Runbook Automation 6.3.0
Conditional Logic steps are Generally Available and enabled by default starting in Runbook Automation 6.3.0.
Overview
Conditional Logic steps enable workflows to make decisions during execution based on runtime conditions. Instead of creating multiple jobs for different scenarios or writing complex scripts with if/then logic, you can use Conditional Logic steps to branch your workflow based on:
- Job options (environment, configuration values)
- Prior step outputs (status, state, IDs, or counts exposed by earlier steps)
- Node attributes (OS family, hostname, tags)
- Job context (project name, job name, execution user)
- Data captured from previous steps
This guide provides hands-on examples to help you understand how to use conditional logic effectively.
Prerequisites
- Runbook Automation 6.3.0+ (Conditional Logic is enabled by default). On versions 5.20.0 through 6.2.x, enable the feature flag in
rundeck-config.properties:rundeck.feature.earlyAccessJobConditional.enabled=true - Basic understanding of job workflows and job options
- Workflow strategy set to Sequential, Parallel, or Node First (Ruleset is not supported)
New to Rundeck?
If you're new to Rundeck, we recommend completing the Getting Started Tutorial first. This guide assumes familiarity with creating jobs, adding steps, and using job options.
Understanding Conditional Logic
Conditional Logic steps come in two types:
- Node Conditional Logic: Evaluates conditions independently on each node based on node attributes
- Workflow Conditional Logic: Evaluates conditions once per job execution in a global context
Both types support:
- Multiple conditions with AND logic (all must be true)
- Multiple condition sets with OR logic (any set can be true)
- Eight comparison operators: equals, not equals, greater than, greater than or equal, less than, less than or equal, contains, and matches (regex)
Hands-On Examples
The following examples demonstrate conditional logic in action. Import these jobs to see how conditions work, then run them with different option values to observe the behavior.
Simple Conditional: Version Check
This example demonstrates a basic conditional step that executes only when a specific version is selected.
Use Case: Run deployment steps only for version 5.20
What This Job Does:
- Accepts a
versionoption as input - Uses a Node Conditional Logic step to check if version equals "5.20"
- If the condition is true, executes an echo command
- If the condition is false, skips the substep
Condition Structure:
- Field:
${option.version} - Operator:
equals - Value:
5.20
Job Definition (JSON):
[ {
"defaultTab" : "nodes",
"description" : "Simple example showing a single condition checking a job option value.",
"executionEnabled" : true,
"loglevel" : "INFO",
"name" : "Simple Conditional Example",
"nodeFilterEditable" : false,
"nodefilters" : {
"dispatch" : {
"excludePrecedence" : true,
"keepgoing" : false,
"rankOrder" : "ascending",
"successOnEmptyNodeFilter" : false,
"threadcount" : "1"
},
"filter" : ".*"
},
"nodesSelectedByDefault" : true,
"options" : [ {
"name" : "version",
"label" : "Version",
"description" : "Enter a version number (try 5.20 or 5.19)",
"type" : "text",
"required" : true
} ],
"scheduleEnabled" : true,
"schedules" : [ ],
"sequence" : {
"commands" : [ {
"conditionGroups" : [ [ {
"key" : "${option.version}",
"operator" : "==",
"value" : "5.20"
} ] ],
"description" : "Check if version is 5.20",
"nodeStep" : true,
"subSteps" : [ {
"exec" : "echo 'Running deployment for version 5.20'"
} ],
"type" : "conditional"
} ],
"keepgoing" : false,
"strategy" : "sequential"
}
} ]
How to Use This Example:
- Copy the JSON above and save it as
simple-conditional-example.json - Import the job into your Rundeck project:
- Navigate to the Jobs page
- Click the gear icon and select "Import Job"
- Upload the JSON file
- Run the job and enter
5.20for the version option- The conditional step will evaluate to true
- The echo command will execute
- Run the job again and enter
5.19for the version option- The conditional step will evaluate to false
- The substep will be skipped
- Check the execution log to see the skip message
What to Observe:
- In the Job Editor, expand the conditional step to see the condition configuration
- In execution logs when condition is true: You'll see the echo output
- In execution logs when condition is false: You'll see a message like "Conditional step skipped"
- Enable Debug logging to see detailed condition evaluation
Key Takeaways:
- Fields must use
${}syntax to reference variables - The operator
==is equivalent to "equals" - Substeps only execute when the condition evaluates to true
- Simple conditionals are perfect for single-criteria decisions
Complex Conditionals: Multi-Environment Deployment
This example demonstrates advanced conditional logic using multiple condition sets with OR logic and multiple conditions per set with AND logic.
Use Case: Run deployment steps in specific scenarios:
- Always run in Dev environment, OR
- Run on Windows servers (but skip version 5.20), OR
- Run on Linux servers (but skip version 5.19)
What This Job Does:
- Accepts three options:
environment,os, andversion - Uses a Node Conditional Logic step with three condition sets (OR logic)
- Each condition set can have multiple conditions (AND logic)
- Executes substeps when ANY condition set evaluates to true
Condition Structure:
Condition Set 1: Dev environment
- Field:
${option.environment}Operator:equalsValue:Dev
OR
Condition Set 2: Windows + not version 5.20
- Field:
${option.os}Operator:equalsValue:Windows - AND Field:
${option.version}Operator:not equalsValue:5.20
OR
Condition Set 3: Linux + not version 5.19
- Field:
${option.os}Operator:equalsValue:Linux - AND Field:
${option.version}Operator:not equalsValue:5.19
Job Definition (JSON):
[ {
"defaultTab" : "nodes",
"description" : "Complex example showing multiple condition sets with OR logic and multiple conditions per set with AND logic.",
"executionEnabled" : true,
"loglevel" : "INFO",
"name" : "Complex Conditionals",
"nodeFilterEditable" : false,
"nodefilters" : {
"dispatch" : {
"excludePrecedence" : true,
"keepgoing" : false,
"rankOrder" : "ascending",
"successOnEmptyNodeFilter" : false,
"threadcount" : "1"
},
"filter" : ".*"
},
"nodesSelectedByDefault" : true,
"options" : [ {
"label" : "Version",
"name" : "version",
"description" : "Application version",
"type" : "text",
"required" : true
}, {
"enforced" : true,
"label" : "Environment",
"name" : "environment",
"required" : true,
"type" : "text",
"values" : [ "Prod", "Dev" ],
"valuesListDelimiter" : ","
}, {
"enforced" : true,
"label" : "Operating System",
"name" : "os",
"required" : true,
"type" : "text",
"values" : [ "Linux", "Windows" ],
"valuesListDelimiter" : ","
} ],
"scheduleEnabled" : true,
"schedules" : [ ],
"sequence" : {
"commands" : [ {
"conditionGroups" : [ [ {
"key" : "${option.environment}",
"operator" : "==",
"value" : "Dev"
} ], [ {
"key" : "${option.os}",
"operator" : "==",
"value" : "Windows"
}, {
"key" : "${option.version}",
"operator" : "!=",
"value" : "5.20"
} ], [ {
"key" : "${option.os}",
"operator" : "==",
"value" : "Linux"
}, {
"key" : "${option.version}",
"operator" : "!=",
"value" : "5.19"
} ] ],
"description" : "Check deployment conditions",
"nodeStep" : true,
"subSteps" : [ {
"description" : "Output Variables",
"exec" : "echo 'Deploying on ${option.os} version ${option.version} in ${option.environment}'"
} ],
"type" : "conditional"
} ],
"keepgoing" : false,
"strategy" : "sequential"
}
} ]
How to Use This Example:
- Copy the JSON above and save it as
complex-conditionals-example.json - Import the job into your Rundeck project
- Run the job with different option combinations to see how conditions evaluate
Test Scenarios:
Try these combinations to understand the OR/AND logic:
| Environment | OS | Version | Will Execute? | Why? |
|---|---|---|---|---|
| Dev | Linux | 5.20 | Yes | Matches Set 1 (Dev environment) |
| Dev | Windows | 5.19 | Yes | Matches Set 1 (Dev environment) |
| Prod | Windows | 5.19 | Yes | Matches Set 2 (Windows AND not 5.20) |
| Prod | Windows | 5.20 | No | Set 1 fails (Prod environment), Set 2 fails (version IS 5.20), Set 3 fails (not Linux) |
| Prod | Linux | 5.20 | Yes | Matches Set 3 (Linux AND not 5.19) |
| Prod | Linux | 5.19 | No | Set 1 fails (Prod environment), Set 2 fails (not Windows), Set 3 fails (version IS 5.19) |
What to Observe:
In the Job Editor:
- Expand the conditional step to see all three condition sets
- Notice the OR separator between condition sets
- Notice the AND logic within each set (multiple conditions in Set 2 and Set 3)
In Execution Logs:
- When conditions are met: You'll see the echo output with the selected values
- When conditions are not met: You'll see "Conditional step skipped"
- Enable Debug logging to see detailed evaluation of each condition and condition set
Debug Mode Output:
- Shows each condition being evaluated
- Shows resolved variable values
- Shows pass/fail for each condition
- Shows which condition set (if any) matched
Key Takeaways:
- OR logic lets you define multiple scenarios where the step should execute
- AND logic within a set requires ALL conditions to be true
- Complex conditionals can handle sophisticated business logic without external scripts
- You can combine up to 5 conditions per set and up to 5 condition sets per step
Real-World Use Cases
Environment-Based Workflows
Use conditionals to run different steps based on environment:
Workflow Conditional Logic: ${option.environment} equals "production"
├─ Substep: Send approval notification
├─ Substep: Backup database
└─ Substep: Enable maintenance mode
OS-Specific Commands
Target specific operating systems without changing node filters:
Node Conditional Logic: ${node.osFamily} equals "unix"
└─ Substep: Run Linux-specific commands
Node Conditional Logic: ${node.osFamily} equals "windows"
└─ Substep: Run Windows-specific commands
Version-Aware Deployments
Skip or include steps based on application versions:
Workflow Conditional Logic: ${option.version} greater than "5.0"
└─ Substep: Use new API endpoints
Tag-Based Node Targeting
Execute steps only on nodes with specific tags:
Node Conditional Logic: ${node.tags} contains "database"
└─ Substep: Database maintenance commands
Building Your Own Conditional Jobs
Step-by-Step Guide
Plan Your Conditions
- Identify what should trigger different behavior
- Determine if you need Node or Workflow conditional logic
- Map out your AND/OR logic requirements
Create Job Options
- Add options for values you'll check in conditions
- Use enforced values for dropdown choices
- Make options required if conditions depend on them
Add Conditional Step
- In the Job Editor, click "Add a step"
- Select Node Conditional Logic or Workflow Conditional Logic
- Give the step a descriptive name (a default Step Name is pre-filled automatically)
Configure Conditions
- Add your first condition (Field, Operator, Value)
- Click "Add" to add more conditions to the same set (AND logic)
- Click "Add Condition Set" to create alternate conditions (OR logic)
Add Substeps
- Click "+ Add Condition Step"
- Select and configure steps that run when conditions are met
- Add multiple substeps as needed
Test Thoroughly
- Run with different option values
- Enable Debug logging to see condition evaluation
- Verify substeps execute only when expected
Tips for Effective Conditionals
Condition Design
- Keep it simple: Prefer multiple simple conditions over complex regex
- Use descriptive names: Help others understand what the conditional does
- Test with debug mode: Always verify conditions evaluate as expected
- Document your logic: Add notes in the job description
Common Patterns
Multiple Environments:
Set 1: ${option.environment} equals "dev"
OR
Set 2: ${option.environment} equals "staging"
OS + Tag Combination:
Set 1: ${node.osFamily} equals "unix" AND ${node.tags} contains "web"
OR
Set 2: ${node.osFamily} equals "windows" AND ${node.tags} contains "iis"
Exclude Specific Cases:
${option.environment} not equals "production"
Troubleshooting
Conditions not evaluating correctly?
- Enable Debug logging when running the job
- Verify option names match exactly (case-sensitive)
- Ensure context variable fields use
${}syntax (${option.name}notoption.name), or select prior step outputs from the Prior Steps tab - Check that node attributes exist on target nodes
Substeps not appearing?
- Verify substeps match parent type (Node substeps for Node Conditional, Workflow substeps for Workflow Conditional)
- Check that at least one condition is defined
- Ensure all required fields are filled
Cannot import job?
- Conditional Logic requires JSON or YAML format (XML is not supported)
- Verify the feature flag is not disabled before importing
Advanced Scenarios
Using Outputs from Previous Steps
Conditional Logic can evaluate outputs from earlier steps in two ways:
1. Built-in Prior Step Outputs (6.3.0+): Many bundled step plugins (such as PagerDuty, ServiceNow, Jira, Datadog, AWS, Sensu, and Job State Conditional) expose their outcomes directly in the condition editor:
- Give the earlier step a unique Step Name (e.g.,
Get Incident) - In your Conditional Logic step, click the Field input and select the Prior Steps tab
- Choose the exposed output (e.g.,
pd_incident_status [steps.['Get Incident'].pd_incident_status])
Parallel strategy and prior-step outputs
The Parallel strategy starts top-level steps concurrently. Use Sequential or Node First when a conditional depends on an earlier step's output so that the earlier step finishes before the condition evaluates.
2. Capturing Output with Log Filters: For Command, Script, or custom steps, capture output using log filters:
- Add a step with a Log Filter (e.g., Key Value Data) to capture output
- In a later conditional step, reference the captured data:
${data.variableName}
Example:
Step 1: Command with Key Value Data log filter capturing "status"
Step 2: Workflow Conditional Logic: ${data.status} equals "ready"
└─ Substep: Proceed with deployment
Nested Conditional Logic
You can place a Conditional Logic step one level inside another Conditional Logic step:
- Add a parent Conditional Logic step and define its conditions
- Add another Conditional Logic step as a substep
- Configure the nested conditional's conditions and add regular substeps
- When the parent condition is met, the nested conditional evaluates independently
The nested conditional cannot contain another conditional. Use a Job Reference substep when deeper or reusable branching is required.
Combining with Workflow Strategies
Conditional Logic works with Sequential, Parallel, and Node First strategies (not Ruleset):
- Sequential Strategy (Step First): Evaluates each conditional step across nodes before moving to the next step
- Parallel Strategy: Evaluates conditionals concurrently across nodes
- Node First Strategy: Runs the full workflow on each node before the next node, evaluating conditions as the workflow progresses on that node
Related Documentation
- Conditional Logic Steps Reference - Complete feature documentation
- Job Options - Creating and using job options
- Job Variables - Complete variable reference
- Workflow Strategies - Understanding workflow execution
- Log Filters - Capturing data for use in conditions
- Pass Data Between Steps - Working with variables and log filters
Next Steps
Now that you understand conditional logic basics:
- Experiment with the example jobs - modify conditions and observe behavior
- Combine conditionals with other features like prior step outputs, log filters, and job references
- Build your own conditional workflows for your specific use cases