Search code examples
rundeck

Rundeck pass common parameters/variable to the flow


Consider I have one main flow execution job, I will have many more like this in the future.

I will have few backend flow jobs which will be used as reference jobs in the main flow execution job which I mentioned above.

Things I need to accomplish:

  1. Pass a few command-line arguments in the main flow job for later reference jobs to use
  2. Also, if reference jobs generate few more variables that should be available for later reference steps to use.

I did not find some very clear documents with the example on the Rundeck site, I would really appreciate it if someone could help on this.

#Update:

After making the text option with the list as plain text, which I able to use in any new step which I add like inline script or command.

But when I wanted to use the same options in Job reference from workflow steps, even after passing the command line argument I could able to get that in my python script.

enter image description here

I passed the argument both way but could not able to get the value of the option in the reference job. As mentioned like this: -source_details ${option.source_details}

and like this as well ${option.source_details}

The child job which I and referring to here is calling the one python script whereas I was checking the command line argument with sys.argv, which is not giving anything extra except the actual python filename.

Correct me if I am missing something here.


Solution

  • To do that, you need to pass options as arguments, for example, the child job uses its own options to use as arguments.

    I leave an example:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='age' value='32' />
            <option name='name' value='Alice' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <id>bf45b9bd-f8f4-4f00-8aaf-86572b637e05</id>
        <loglevel>INFO</loglevel>
        <name>ChildJob</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <fileExtension>.sh</fileExtension>
            <script><![CDATA[echo "Username: @option.name@";
    echo "Age: @option.age@";]]></script>
            <scriptargs />
            <scriptinterpreter>/bin/bash</scriptinterpreter>
          </command>
        </sequence>
        <uuid>bf45b9bd-f8f4-4f00-8aaf-86572b637e05</uuid>
      </job>
    </joblist>
    

    And the parent job can pass another options to override child job options:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='age' value='25' />
            <option name='name' value='Bob' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <id>9fa68cd8-5bb0-4341-be32-f58c372cb765</id>
        <loglevel>INFO</loglevel>
        <name>Parent</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <jobref name='ChildJob' nodeStep='true'>
              <arg line='-name ${option.name} -age ${option.age}' />
              <uuid>bf45b9bd-f8f4-4f00-8aaf-86572b637e05</uuid>
            </jobref>
          </command>
        </sequence>
        <uuid>9fa68cd8-5bb0-4341-be32-f58c372cb765</uuid>
      </job>
    </joblist>
    

    In few words: you can use options in your jobs (to "receive" from parent job or to use itself), and keep your values across your workflows.

    Update with a python3 example

    The concept is the same, in the child job you can create some options to receive the values from the parent job, take a look.

    Child Job:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='arg1' value='one' />
            <option name='arg2' value='two' />
            <option name='arg3' value='three' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <id>159e14d6-29e2-4fe9-b9b3-b1621d59843d</id>
        <loglevel>INFO</loglevel>
        <name>PythonChildJob</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <fileExtension>.py</fileExtension>
            <script><![CDATA[#!/usr/bin/python3
    
    import sys
    
    print ('Number of arguments:', len(sys.argv), 'arguments.')
    print ('Argument List:', str(sys.argv))]]></script>
            <scriptargs>${option.arg1} ${option.arg2} ${option.arg3}</scriptargs>
            <scriptinterpreter>/usr/bin/python3.8</scriptinterpreter>
          </command>
        </sequence>
        <uuid>159e14d6-29e2-4fe9-b9b3-b1621d59843d</uuid>
      </job>
    </joblist>
    

    And the parent job, you can pass options or just strings like -arg1 hello -arg2 from -arg3 mars

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='opt1' value='hello' />
            <option name='opt2' value='entire' />
            <option name='opt3' value='world' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <executionEnabled>true</executionEnabled>
        <id>1442dbb7-55e3-45c0-af4b-a58ce5c07582</id>
        <loglevel>INFO</loglevel>
        <name>ParentJob</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <plugins />
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <jobref name='PythonChildJob' nodeStep='true'>
              <arg line='-arg1 ${option.opt1} -arg2 ${option.opt2} -arg3 ${option.opt3}' />
              <uuid>159e14d6-29e2-4fe9-b9b3-b1621d59843d</uuid>
            </jobref>
          </command>
        </sequence>
        <uuid>1442dbb7-55e3-45c0-af4b-a58ce5c07582</uuid>
      </job>
    </joblist>