How to get the current Nodes where the Job is running

I'm developing a job and the user can choose in which nodes can run, so the Node Filter is open to the convenient of the user.

When the job is starting I need to do a calculation based in the number of nodes chosed by the user, exist a way to get this number?

Regards,

Alejandro L

Upvotes: 1

Views: 1073

Answers (3)

Learning4Ever
Learning4Ever

Reputation: 1

I know this is a bit old question but I can tell how I solved a similar requirement in Rundeck version 4.17. Note: maybe it does not fit your need exactly but maybe it will help someone.

I needed to run a Workflow locally, but the user should be able to select specific Nodes as targets. So I added some Workflow steps but configured the job to be dispatched to Nodes. This configuration enables the user to select Nodes from Rundeck inventory but the Workflow steps are run locally on the same server. In one (or several) of the steps I needed to get all the nodes the user selected, and I used job.filter variable to get the names. This worked well but as @Sergey and @MegaDrive68k noted, this doesn't work when target nodes are selected with a filter as .* or a tag. Sure you can then call the API from a script via curl as @MegaDrive68k explains here or run rd command line tool to query the API for the nodes matching the used filter, but if you add a Node Step with a Remote Command or Inline Script you can set a global variable and then on your Workflow Steps you can get access to it. Node Steps run before the workflow steps, so:

  1. Add as first step a Run Remote Command Node Step or Run Inline Script Node Step.
  2. Echo the node name or hostname with a pattern to capture it later:
  • For Run Remote Command: echo "RUNDECK:DATA:MY_NODES=${node.hostname}"
  • For Run Inline Script: echo "RUNDECK:DATA:[email protected]@"
  1. Add a Key Value Data Log Filter for this step (with default regex pattern). You will get a variable named MY_NODES inside data context.
  2. Edit the Workflow Step script that needs the nodes list and get the value for all the nodes: mySelectedNodes=${data.MY_NODES*}. It will get all the values for all the nodes concatenated with comma.
  3. Now you can work with this list, split it or pass it to some other script or step, read as an array as @Sergey commented, etc.

The problem with this approach is that if a Node can't be reached then you will not know if it was part of the targets selected by the user, given that the Node steps have to be run on the unreachable node and therefore you won't get it's name/hostname

Maybe it's not the more elegant solution, but I hope it will help someone.

Upvotes: 0

Sergey Pleshakov
Sergey Pleshakov

Reputation: 8948

a workaround to take would be using job.filter variable

so if you do @job.filter@

it returns a string with the list of nodes like us-east-1-0,us-east-1-1,us-east-1-2

if you save it as a string, and then split the string on ',' then you get an array of nodes:

IFS=',' read -r -a array <<< "$string"

and then you can get the number of nodes by echo ${#array[@]}

Note

as @MegaDrive68k mentioned this won't work if use select all node with the use of filter .*

Upvotes: 0

MegaDrive68k
MegaDrive68k

Reputation: 4325

By design, that information is only available after the execution, so, a good approach is to call the job via API (in a script step) and with the execution ID number (available in the API call output) you can list and count the nodes, e.g:

#!/bin/bash

nodes=$(curl -s -X GET "http://localhost:4440/api/41/execution/16" \
--header "Accept: application/json" \
--header "X-Rundeck-Auth-Token: your_user_token" \
 | jq -r '.successfulNodes | . []')

number_of_nodes=$(echo $nodes | wc -w)

echo "Number of nodes: $number_of_nodes"

This example needs jq to extract the nodes from the API response.

Anyway, your request sounds good for an enhancement request, please suggest that here.

Upvotes: 1

Related Questions