How do I capture output from one Rundeck step to b

2020-04-08 13:25发布

问题:

I'm attempting to build, launch, and link a set of docker containers using Rundeck. In short (for those not familiar with docker), when an image is launched, it returns a container ID. I would like to use this container ID in the launching of subsequent jobs.

When run from the command line, it would look something like this (example only!!):

# docker run -Pd 23ABCD45
34DEF123
# docker run -Pd --link 34DEF123:host1 ABC123EF
321CB456

(note the use of the first return value in the second command line)

At this point, there would be two containers running. The second would be linked to the first by the --link option, and it would be addressable using the hostname host1 from inside the second container. To be fair, docker generates (or may be given) a specific container name which can be used in place of the container id. I would prefer to use the container ID to avoid the hassle of having to create/track unique names.

I would like to be able to capture the output of the first command (the container ID) so that it can be reused in the second command. Is this possible?

Edit: These images are being used for testing immediately following a "docker build" (which also outputs a similar ID I would like to include in my chain) and might be followed by "docker rm" and "docker rmi" commands, so there are a number of uses for capturing this type of output and carrying it through a related set of operations. This is not just about launching/linking containers.

回答1:

There is no direct Rundeck implementation that allows you pass an output from one job to another job as an input, but there are work around I've tried in the past, and I've settled on the second approach.

1. Use a file to pass data

  1. Save the ID/output into a tmp file in first job
  2. Second job read that file

Things might go wrong since you depend on a file, but good code can improve.

2. Call two jobs using Rundeck CLI from another job

This is the approach I am using.

JobA printout two random numbers.

echo $RANDOM;echo $RANDOM

JobB print out the second random produced from JobA which is passed as an option "number"

echo "$RD_OPTION_NUMBER is the number JobB received"

JobC calls first job, save last line to a variable and pass it to JobB

#!/bin/bash
OUTPUT_FROM_JOB_A=`run -f --id <ID of JobA> | tail -n 1`
run -f --id <ID of JobB> -- -number $OUTPUT_FROM_JOB_A

Output:

[5394] execution status: succeeded
Job execution started:
[5395] JobB <https://hostname:4443/project/Project/execution/show/5395>
6186 is the number JobB received
[5395] execution status: succeeded

This is just primitive code sample. you can do alot with python subprocess or just use bash.