Rundeck sharing variables across job steps
Asked Answered
R

3

5

I want to share a variable across rundeck job steps.

  1. Initialized a job option "target_files"
  2. Set the variable on STEP 1.

    RD_OPTION_TARGET_FILES=some bash command
    echo $RD_OPTION_TARGET_FILES
    The value is printed here.

  3. Read the variable from STEP 2.
    echo $RD_OPTION_TARGET_FILES

Step 3 doesn't recognize the variable set in STEP 1.
What's a good way of doing this on rundeck other than using environment variables?

Royer answered 9/5, 2016 at 7:40 Comment(3)
where's your code? how you have tried so far?Tammany
@Jahid, STEP 1 is an inline bash script. And STEP 2 is another.Royer
they are both likely to be run in seperate processes, so the environment of one will not be copied to the environment of the other. You will need to add the RD_OPTION_TARGET_FILES variable to the environment once it is set in step 1 and before you run step 2. I don't know rundeck, but imagine there is a fairly easy way to achieve this. In pure bash you would use export to export the variable into the environment.Rifling
D
9

The detailed procedure from RUNDECK 2.9+:

1) set the values - three methods:

1.a) use a "global variable" workflow step type e.g. fill in: Group:="export", Name:="varOne", Value:="hello"

1.b) add to the workflow a "global log filter" (the Data Capture Plugin cited by 'Amos' here) which takes a regular expression that is evaluated on job step log outputs. For instance with a job step command like:

   echo "CaptureThis:varTwo=world"

and a global log filter pattern like:

   "CaptureThis:(.*?)=(.*)" 

('Name Data' field not needed unless you supply a single capturing group in the pattern)

1.c) use a workflow Data Step to define multiple variables explicitly. Example contents:

varThree=foo
varFour=bar

2) get the values back:

you must use the syntax ${ctx.name} in command strings and args, and @ctx.name@ within INLINE scripts. In our example, with a job step command or inline script line like:

echo "values : @export.varOne@, @data.varTwo@, @stub.varThree@, @stub.varFour@"

you'll echo the four values.

The context is implicitly 'data' for method 1.b and 'stub' for method 1.c.

Note that a data step is quite limitative! It only allows to benefit from @stub.name@ notations within inline scripts. Value substitution is not performed in remote files, and notations like ${stub.name} are not available in job step command strings or arguments.

Dorfman answered 9/11, 2018 at 15:23 Comment(1)
It's worth noting that to use a variable in a workflow, you must specifiy which node it comes from ${data.mvvariable@mynode}. ${data.myvariable*} also works.Lynnell
A
4

After Rundeck 2.9, there is a Data Capture Plugin to allow pass data between job steps.

The plugin is contained in the Rundeck application by default.

Data capture plugin to match a regular expression in a step’s log output and pass the values to later steps

Details see Data Capture/Data Passing between steps (Published: 03 Aug 2017)

Acarology answered 20/9, 2017 at 7:59 Comment(3)
nice plugin..!!Briquet
There are 2 parts to this. (1) Variable/data sharing across steps - Data plugin solves this issue. (2) In a flow control scenario, based on a run scenario - output of an instruction inside a step, set/reset a subsequent switch (step). I'm sure there are some other supported handles on rundeck, which I need to explore more.Royer
more relevant link - rundeck.com/blog/output-processing-data-passing-previewCondom
B
2

Nearly there are no ways in job Inline scripts other than 1, exporting the value to env or 2, writing the value to a 3rd file at step1 and step2 reading it from there.

If you are using "Scriptfile or URL" method, may be you can execute step2 script with in script1 as a work around.. like

Script1
#!/bin/bash
. ./script2

In the above case script2 will execute in the same session as script1 so the variables and values are preserved.

EDIT Earlier there was no such options but later there are available plugins. Hence check Amos's answer.

Briquet answered 12/5, 2016 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.