How to get the current Nodes where the Job is running

Viewed 108

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

2 Answers

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.

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 .*

Related