R. Bogaveev
R. Bogaveev

Reputation: 301

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?

Upvotes: 0

Views: 1221

Answers (2)

higuita
higuita

Reputation: 2315

You have the enterprise plugin that do that: https://docs.rundeck.com/docs/manual/workflow-steps/loop-plugins.html

but i workaround via a inline script, that will call the rundeck api. You need a user token for doing the rundeck api call.

In this example, i can restart one consumer or a list of multiple consumers, chosen by the user

#!/bin/bash

runjob() {
    # Consumer restart
    curl -s -f --header 'X-Rundeck-Auth-Token: $TOKEN' \
        -X POST \
        localhost/api/18/job/57...(replace this block with your job_id)...6b/run \
        -H 'Content-Type: application/json' \
        -d '{
                "options": {
                "environment": "@option.environment@",
                "consumer": "'$1'"
            }
        }'
}

if [[ "@option.consumer@" == *","* ]]; then
    for i in $( echo "@option.consumer@" | sed 's/,/ /g' ); do
        echo $i
        runjob $i
    done
else
    echo "@option.consumer@"
    runjob "@option.consumer@"
fi

Upvotes: 1

MegaDrive68k
MegaDrive68k

Reputation: 4325

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

[email protected]@
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.

Upvotes: 1

Related Questions