Rundeck - flow execution with error handler - rundeck

Default error handler works in a way, if any job sequence does not end with the non-zero status it went into error handler flow.
I wanted to implement the flow, if the API endpoint returns 204, I need to perform some dependent operation.
I was thinking of doing it using an error handler but how I can deal with the return status 204 as it just handles non-zero status flow in the error handler.

On Rundeck Enterprise you can do it using the Ruleset Strategy (for example, catch the exit code and put it on a data option to decides if runs or not another job), take a look at this.
On the Community version you can do it scripting, manage the behavior on an script step in the following way (on my example i want to detect only the 127 error, defined on an option):
A job definition example:
<joblist>
<job>
<context>
<options preserveOrder='true'>
<option name='code_to_catch' value='127' />
</options>
</context>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>ab58e8e3-78f8-4a60-b6fc-4ad1167aa3a4</id>
<loglevel>INFO</loglevel>
<name>HelloWorld</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<fileExtension>.sh</fileExtension>
<script><![CDATA[date-foo-bar # intentional error
mycode=$(printf '%d\n' $?)
case $mycode in
#option.code_to_catch#)
echo "Executing actions for #option.code_to_catch# error"
## also, you can run any job using RD-CLI (https://rundeck.github.io/rundeck-cli/)
## or Rundeck API (https://docs.rundeck.com/docs/api/rundeck-api.html)
;;
0)
echo "all ok!"
exit 0
;;
esac
]]></script>
<scriptargs />
<scriptinterpreter>/bin/bash</scriptinterpreter>
</command>
</sequence>
<uuid>ab58e8e3-78f8-4a60-b6fc-4ad1167aa3a4</uuid>
</job>
</joblist>
As you see, you can use the inline script to caught the error code and later run another job using RD-CLI if you like.

Related

Local json file to fill "allowed values" option in Rundeck

I have a list of options in json format residing within Rundeck's server which I would like to use for filling out the "Allowed values" parameter in a new option.
The documentation states: "It must be accessible via HTTP(S) or on the local disk for the Rundeck server.", but sticking the path of the file fails the option generation and I could not see any example of it.
Would someone have an idea?
Thanks in advance
You need to use file:\ to access the file locally (at "Allowed values"), take a look at this example:
video.json file:
[
{"name":"youtube", "value":"youtube.com"},
{"name":"twitch", "value":"twitch.tv"}
]
job definition file (here how to import to your Rundeck instance for testing):
<joblist>
<job>
<context>
<options preserveOrder='true'>
<option enforcedvalues='true' name='video_service' valuesUrl='file:/path/to/video.json' />
</options>
</context>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>4be0ef08-889a-4366-ba29-0b0302fb5391</id>
<loglevel>INFO</loglevel>
<name>AllowedValues</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<exec>echo "video platform: ${option.video_service}"</exec>
</command>
</sequence>
<uuid>4be0ef08-889a-4366-ba29-0b0302fb5391</uuid>
</job>
</joblist>
Here how the job looks and here the result.

How can I run a job on Rundeck for each user in the list?

I want to create a scheduled job on Rundeck. Rundeck Job runs the script that does some staff for a single user I have in my list.
So job has an input parameter - username. I need to run that job for all users in my list, but not to process it in one job execution. I need a separate job execution for each user.
Providing an example to make it more clear:
For instance, I have 3 users: user1, user2, user3.
I have a job that does some processing for that users and running it with parameter (unsername).
I need to create a scheduled job that will be run for each user.
1. Running job with user1 parameter
2. Running job with user2 parameter
3. Running job with user3 parameter
Is there anyway to do that?
You can design a workflow with a list option to use on any step (like command, inline-script or script), I did an example with a list option (users) and example inline-script that do some action for each user, the job is scheduled.
<joblist>
<job>
<context>
<options preserveOrder='true'>
<option name='users' required='true' value='alice,bob,charlie,david' />
</options>
</context>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>e62a25a0-b67c-4bb7-8721-c96b7e4cb9f5</id>
<loglevel>INFO</loglevel>
<name>JobEXAMPLE</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<schedule>
<month month='*' />
<time hour='12' minute='0' seconds='0' />
<weekday day='*' />
<year year='*' />
</schedule>
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<fileExtension>.sh</fileExtension>
<script><![CDATA[#!/bin/bash
myusers=#option.users#
Field_Separator=$IFS
# set comma as the internal field separator for the string list
IFS=,
for value in $myusers;
do
echo "action for $value"
done
IFS=$Field_Separator]]></script>
<scriptargs />
<scriptinterpreter>/bin/bash</scriptinterpreter>
</command>
</sequence>
<uuid>e62a25a0-b67c-4bb7-8721-c96b7e4cb9f5</uuid>
</job>
</joblist>
Here the result.
Maybe this interest you. Also, data values work in this case.

Rundeck multiple job executions, queued or discarded?

Rundeck docs for creating a job,
https://docs.rundeck.com/docs/manual/creating-jobs.html#creating-a-job
says:
Multiple Executions
By default, a job runs as a "Single Execution" -- it can only have a single execution running at a time. This is useful if the steps the Job performs might be interfered with if another separate process was also performing them on the same Node(s).
However, in some cases it is useful to allow a Job to be executed more than once simultaneously.
You can make a job allow "Multiple Executions" by toggling the value to Yes in the Job editor field shown below:
my questions:
For the default "Single Execution" mode, what happens to subsequent simultaneous job runs? Are they discarded or queued?
For "Multiple Execution" mode, what happens to subsequent simultaneous job runs that exceed the "Max number of multiple executions"? Are they discarded or queued?
Thanks for any guidance.
You can test with a simple job definition like this:
<joblist>
<job>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>72fe54d5-26da-4cd4-a487-0955bd3b7f67</id>
<loglevel>INFO</loglevel>
<name>HelloWorld</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<exec>sleep 15; echo "hello"</exec>
</command>
</sequence>
<uuid>72fe54d5-26da-4cd4-a487-0955bd3b7f67</uuid>
</job>
</joblist>
In case of execute again the job you can see this error.
So, if you modify the job to allowing "two multiple execution" you can see that works normally with 2 executions of the same job:
<joblist>
<job>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>72fe54d5-26da-4cd4-a487-0955bd3b7f67</id>
<loglevel>INFO</loglevel>
<maxMultipleExecutions>2</maxMultipleExecutions>
<multipleExecutions>true</multipleExecutions>
<name>HelloWorld</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<exec>sleep 15; echo "hello"</exec>
</command>
</sequence>
<uuid>72fe54d5-26da-4cd4-a487-0955bd3b7f67</uuid>
</job>
</joblist>
But if you execute three times the same job, you can see the same error of first scenario.
Short answer: if you exceed the default value or defined value, the execution is discarded.

Job Conditional Reports Previous Job "Never Run"

I have a Job that calls other jobs as sort of a main run-file. I execute a preliminary check, which sets some values on a management server that I have to ensure I don't get any erroneous alerts due to the updates being run.
The preliminary job executes fine, but the conditional that comes right after it, ALWAYS outputs the status of:
{{UUID}} is NOT RUNNING AND previously NEVER. Expected NOT RUNNING AND 'Succeeded'
No matter what I do, I can't figure out if either I'm doing something wrong or if I just have something misconfigured, or if the conditionals are just broken. This is a small install for my homelab of about 10 servers, so I'm running with the default database back end, not MySQL or something else, so I don't know if that's an issue.
My Rundeck detail
Rundeck version: Rundeck 3.2.8-20200608
Install type: deb
OS Name/version: Ubuntu 18.04
DB Type/version: H2? (whatever the default is)
Verify the job state conditional expectation, I leave an example on 3.2.8 that works. Basically Job Step Conditional expects another job last execution as successfully, anything different of that makes the condition fails.
HelloWorld job:
<joblist>
<job>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<group>test1</group>
<id>0bdd6fe5-addb-4051-b072-bb4430130a80</id>
<loglevel>INFO</loglevel>
<name>HelloWorld</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<exec>echo "hello world"</exec>
</command>
</sequence>
<uuid>0bdd6fe5-addb-4051-b072-bb4430130a80</uuid>
</job>
</joblist>
CheckerJob (With a Job Step Conditional step, expects that HelloWorld job with a successful execution):
<joblist>
<job>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<group>test1</group>
<id>c6f2be75-81e8-4f8e-ba35-234d5c8a97d6</id>
<loglevel>INFO</loglevel>
<name>CheckerJob</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<exec>echo "starting..."</exec>
</command>
<command>
<step-plugin type='job-state-conditional'>
<configuration>
<entry key='condition' value='Equals' />
<entry key='executionState' value='Succeeded' />
<entry key='fail' value='true' />
<entry key='halt' value='true' />
<entry key='jobName' value='test1/HelloWorld' />
<entry key='jobProject' value='${job.project}' />
<entry key='jobUUID' value='0bdd6fe5-addb-4051-b072-bb4430130a80' />
<entry key='running' value='false' />
</configuration>
</step-plugin>
</command>
</sequence>
<uuid>c6f2be75-81e8-4f8e-ba35-234d5c8a97d6</uuid>
</job>
</joblist>
If the job never runs before.
If the job runs before (Job State Conditional expectation).
Another way
If you want to run Job State conditional in the same parent job, you need an individual execution of the "child" job, the way to do that is executing that job "externally" (using RD-CLI or API, I leave an example):
HelloWord:
<joblist>
<job>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>dd33434e-5b6e-40e1-88b0-e9b5b52a3801</id>
<loglevel>INFO</loglevel>
<name>HelloWorld</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<exec>echo "hello world!"</exec>
</command>
</sequence>
<uuid>dd33434e-5b6e-40e1-88b0-e9b5b52a3801</uuid>
</job>
</joblist>
CheckerFakeParentJob (Executes HelloWorld vi RD-CLI and evaluate that execution later):
<joblist>
<job>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>a90ec790-b856-4f00-8b66-2f37da1d00cb</id>
<loglevel>INFO</loglevel>
<name>CheckerFakeParentJob</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<exec>echo "starting"</exec>
</command>
<command>
<fileExtension>.sh</fileExtension>
<script><![CDATA[# print a message
echo "running HelloWorld job"
# run the hello world job via RD-CLI to get the individual execution and evaluate later
rd run -j HelloWorld -p ProjectEXAMPLE
# some time depending of job
sleep 3
# print a message
echo "done"]]></script>
<scriptargs />
<scriptinterpreter>/bin/bash</scriptinterpreter>
</command>
<command>
<step-plugin type='job-state-conditional'>
<configuration>
<entry key='condition' value='Equals' />
<entry key='executionState' value='Succeeded' />
<entry key='fail' value='true' />
<entry key='halt' value='true' />
<entry key='jobUUID' value='dd33434e-5b6e-40e1-88b0-e9b5b52a3801' />
<entry key='running' value='false' />
</configuration>
</step-plugin>
</command>
</sequence>
<uuid>a90ec790-b856-4f00-8b66-2f37da1d00cb</uuid>
</job>
</joblist>
Result here.
You have more information here.
EDIT: If you're referencing a Job Reference Step based workflow, you must take the parent job status (even the child job is executed through parent job).

Rundeck - Run Steps dynamically based on input Options

I'd like to run number of steps based on different input Options.
For example, I've a Job that runs a ./script.sh with year as input param:
./script.sh <year>
the idea is to set a list of "years" as Options params (something like 2018, 2019, 2020) and run a script.sh for each year and each script.sh <year> should be run as a single Step.
step_1: ./script.sh 2018
step_2: ./script.sh 2019
step_3: ./script.sh 2020
step_n: ./script.sh n
Is there a way to do that?
Thank you.
UPDATE 2020-04-22
Expected on Rundeck where each Command has one year:
I did this example, using an option that passes it to an inline script that iterates using #option.myoption# (and referencing the "external" script step as argument).
<joblist>
<job>
<context>
<options preserveOrder='true'>
<option name='theyear' value='2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020' />
</options>
</context>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>a72d4d32-d4fe-4774-b18c-723d18842c5e</id>
<loglevel>INFO</loglevel>
<name>WhatYearIs</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<fileExtension>.sh</fileExtension>
<script><![CDATA[#!/bin/bash
DataList=" #option.theyear# "
Field_Separator=$IFS
# set comma as internal field separator for the string list
IFS=,
for val in $DataList;
do
# echo $val
bash /path/to/your/script/year.sh $val
done
IFS=$Field_Separator]]></script>
<scriptargs />
<scriptinterpreter>/bin/bash</scriptinterpreter>
</command>
</sequence>
<uuid>a72d4d32-d4fe-4774-b18c-723d18842c5e</uuid>
</job>
</joblist>
The bash script to test:
#!/bin/bash
# print_args.sh
echo "Current year is:" "$#"
# You could pass all arguments to another program like this
# myProgram "$#"
UPDATE: You cannot add steps dynamically on a job meanwhile runs, but, another way to do that is basically a modification of the first suggestion, the difference is that we call another job through rd-cli tool on inline-script that calls (and pass the year as an option) to execute as an individual step.
WhatYearIs Job (As parent job).
<joblist>
<job>
<context>
<options preserveOrder='true'>
<option name='theyear' value='2018,2019,2020' />
</options>
</context>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<group>SO</group>
<id>a72d4d32-d4fe-4774-b18c-723d18842c5e</id>
<loglevel>INFO</loglevel>
<name>WhatYearIs</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<script><![CDATA[#!/bin/bash
DataList=" #option.theyear# "
Field_Separator=$IFS
# set comma as internal field separator for the string list
IFS=,
for val in $DataList;
do
# echo $val
sleep 3
rd run -p ProjectEXAMPLE -j WhatYearChild -- -childyear $val
done
IFS=$Field_Separator]]></script>
<scriptargs />
</command>
</sequence>
<uuid>a72d4d32-d4fe-4774-b18c-723d18842c5e</uuid>
</job>
</joblist>
WhatYearChild (As child job that executes the script).
<joblist>
<job>
<context>
<options preserveOrder='true'>
<option name='childyear' />
</options>
</context>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<group>SO</group>
<id>76c15c6d-be45-4540-8f44-8823ed2d2483</id>
<loglevel>INFO</loglevel>
<name>WhatYearChild</name>
<nodeFilterEditable>false</nodeFilterEditable>
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<script><![CDATA[echo "Current year is: #option.childyear#"]]></script>
<scriptargs />
</command>
</sequence>
<uuid>76c15c6d-be45-4540-8f44-8823ed2d2483</uuid>
</job>
</joblist>
With this result (technically is an individual step of each iteration).