Reputation: 21
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
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:
echo "RUNDECK:DATA:MY_NODES=${node.hostname}"
echo "RUNDECK:DATA:[email protected]@"
mySelectedNodes=${data.MY_NODES*}
. It will get all the values for all the nodes concatenated with comma.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
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[@]}
as @MegaDrive68k mentioned this won't work if use select all node with the use of filter .*
Upvotes: 0
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