我在Rundeck有一个上级工作,并且在步骤中,我有多个参考工作(这些工作是使用“工作参考-为每个节点执行另一个工作”创建的),并且在内部每个参考工作都有不同的任务。
有没有办法让我获得父作业的“步骤”状态(通过或失败)?
这样做的目的是生成报告并附加到邮件中,该邮件将包含每个步骤的成功或失败。
答案 0 :(得分:2)
您可能已经注意到,Rundeck只在Job Reference Step工作流程(这是设计使然)上执行Parent Job的工作,在这种情况下,我们将不得不对Rundeck进行“尝试”。
我们需要执行子作业,为此,我们必须使它们分别运行,捕获每个执行的状态并将其放在模板Markdown file中,该模板可以在通知中发送作为email template。
要调用每个作业,我们将创建一个“假父作业”,该作业将单独运行每个子作业(使用API通过cURL)并保存结果(使用jq提取结果)值)在Markdown file步骤中的inline-script上,这是脚本:
# script that obtains individual child jobs and uses it to a mail notification
# https://stackoverflow.com/questions/64590927/how-to-get-steps-status-of-a-rundeck-job-to-a-file
#####################################################
# rundeck instance values
rdeck_server="your_rundeck_host" #rundeck hostname
rdeck_port="4440" # rundeck tcp port
rdeck_api="36" # rundeck api version
jobid="9c667cb5-7f93-4c01-99b0-3249e75887e4 1c861d46-17a3-45ee-954d-74c6d7b597a0 ae9e455e-ca7a-440e-9ab4-bfd25827b287" # space separated child jobs id's
token="bmqlGuhEcekSyNtWAwuizER4YoJBgZdI" # user token
#####################################################
# "prudential" time between actions (in seconds)
pt="2"
#####################################################
# clean the file
echo "" > myfile.md
#####################################################
# add a header to a file
mydate=$(date +'%m/%d/%Y')
echo "# $mydate report" >> myfile.md
#####################################################
# 1) run the job via API and store the execution ID.
# 2) takes the execution ID and store job status via API
for myid in $jobid # iterate over jobs id's
do
# sleep
sleep $pt
# save the execution id (to get the status later) on a variable named "execid"
execid=$(curl -s -H accept:application/json --location --request POST "http://$rdeck_server:$rdeck_port/api/$rdeck_api/job/$myid/run?authtoken=$token" | jq -r '.id')
# sleep
sleep $pt
# save the status on a variable named "status"
status=$(curl -s --location --request GET "http://$rdeck_server:$rdeck_port/api/$rdeck_api/execution/$execid/state?authtoken=$token" | jq -r '.executionState')
# put the status on a file
echo "* job $myid is $status" >> myfile.md
# rundeck friendly output message
echo "job $myid is done, status: $status"
done
在工作定义中,它看起来像这样:
<joblist>
<job>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>eb4826bc-cc49-46b5-9aff-351afb529197</id>
<loglevel>INFO</loglevel>
<name>FakeParent</name>
<nodeFilterEditable>false</nodeFilterEditable>
<notification>
<onfailure>
<email attachType='file' recipients='it@example.net' subject='info' />
</onfailure>
<onsuccess>
<email attachType='file' recipients='it@example.net' subject='info' />
</onsuccess>
</notification>
<notifyAvgDurationThreshold />
<plugins />
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<exec>echo "starting..."</exec>
</command>
<command>
<fileExtension>.sh</fileExtension>
<script><![CDATA[# script that obtains individual child jobs and uses it to a mail notification
# https://stackoverflow.com/questions/64590927/how-to-get-steps-status-of-a-rundeck-job-to-a-file
#####################################################
# rundeck instance values
rdeck_server="your_rundeck_host" #rundeck hostname
rdeck_port="4440" # rundeck tcp port
rdeck_api="36" # rundeck api version
jobid="9c667cb5-7f93-4c01-99b0-3249e75887e4 1c861d46-17a3-45ee-954d-74c6d7b597a0 ae9e455e-ca7a-440e-9ab4-bfd25827b287" # space separated child jobs id's
token="bmqlGuhEcekSyNtWAwuizER4YoJBgZdI" # user token
#####################################################
# "prudential" time between actions (in seconds)
pt="2"
#####################################################
# clean the file
echo "" > myfile.md
#####################################################
# add a header to a file
mydate=$(date +'%m/%d/%Y')
echo "# $mydate report" >> myfile.md
#####################################################
# 1) run the job via API and store the execution ID.
# 2) takes the execution ID and store job status via API
for myid in $jobid # iterate over jobs id's
do
# sleep
sleep $pt
# save the execution id (to get the status later) on a variable named "execid"
execid=$(curl -s -H accept:application/json --location --request POST "http://$rdeck_server:$rdeck_port/api/$rdeck_api/job/$myid/run?authtoken=$token" | jq -r '.id')
# sleep
sleep $pt
# save the status on a variable named "status"
status=$(curl -s --location --request GET "http://$rdeck_server:$rdeck_port/api/$rdeck_api/execution/$execid/state?authtoken=$token" | jq -r '.executionState')
# put the status on a file
echo "* job $myid is $status" >> myfile.md
# rundeck friendly output message
echo "job $myid is done, status: $status"
done]]></script>
<scriptargs />
<scriptinterpreter>/bin/bash</scriptinterpreter>
</command>
<command>
<exec>echo "done!"</exec>
</command>
</sequence>
<uuid>eb4826bc-cc49-46b5-9aff-351afb529197</uuid>
</job>
</joblist>
如果选中“通知”部分,您会注意到,如果正确执行或失败,它将发送一封电子邮件。您需要配置Rundeck,以便它可以发送电子邮件。我保留了对其进行配置的步骤:
停止您的Rundeck服务。
在rundeck-config.properties
文件上添加电子邮件配置(通常在/etc/rundeck
路径下):
# e-mail notification settings
grails.mail.host=your-smtp-host.com
grails.mail.port=25
grails.mail.username=your-username
grails.mail.password=yourpassword
更多信息here。
rundeck-config.properties
文件上添加特定于您的作业的模板配置(检查第3行,是作业脚本生成的文件路径):# project and job specific
rundeck.mail.YOUR-PROJECT-NAME.YOUR-JOB-NAME.template.subject=your-subject-string
rundeck.mail.YOUR-PROJECT-NAME.YOUR-JOB-NAME.template.file=/path/to/your/myfile.md
rundeck.mail.YOUR-PROJECT-NAME.YOUR-JOB-NAME.template.log.formatted=true # (if true, prefix log lines with context information)
执行工作时,您会在individual execution中看到inbox和报告。