Rundeck sharing variables across job steps

14,787

Solution 1

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.

Solution 2

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)

Solution 3

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.

Share:
14,787
Naveen Karnam
Author by

Naveen Karnam

Pragmatic Programming, Java, HTML, Spring, XML, SOA, Cassandra, Data modelling, JavaScript, Design and Architecture.

Updated on June 05, 2022

Comments

  • Naveen Karnam
    Naveen Karnam almost 2 years

    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?

  • Leo Prince
    Leo Prince over 6 years
    nice plugin..!!
  • Naveen Karnam
    Naveen Karnam over 6 years
    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.
  • DustWolf
    DustWolf over 3 years
    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.
  • Indrajeet Gour
    Indrajeet Gour about 3 years