如何将 Rundeck 作业的步骤状态获取到文件 [英] How to get steps status of a Rundeck job to a file

查看:38
本文介绍了如何将 Rundeck 作业的步骤状态获取到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rundeck 中有一个父作业,并且在步骤中我有多个参考作业(使用作业参考 - 为每个节点执行另一个作业"创建),并且这些参考作业中的每一个在内部都有不同的任务.

I have a Parent job in Rundeck and in steps I have multiple reference jobs (that are created using "Job Reference - Execute another Job for each Node") and internally each of these reference jobs has different tasks.

有没有办法将父作业的步骤状态(通过或失败)获取到文件中?

Is there as way I can get Steps status (Pass or Fail) of the Parent job's to a file?

这样做的目的是生成一份报告并附在一封邮件中,其中包含每一步的成功或失败.

The purpose of this is to generate a report and attach to a mail which will have the success or failure of each step.

推荐答案

你可能已经注意到,Rundeck 只在 Job Reference Step 工作流中执行 Parent Job(这是设计使然),在这种情况下,我们将不得不玩"有点用 Rundeck.

As you may have noticed, Rundeck only takes the Parent Job execution on Job Reference Step workflows (this is by design), in this case, we will have to "play" a bit with Rundeck.

我们需要子作业的执行,为此,我们必须让它们单独运行,捕获每个执行的状态并将其放置在模板中 Markdown 文件,可以作为 电子邮件模板.

We need the executions of the child jobs, for that, we will have to make them run individually, capture the state of each of these executions and place it in a template Markdown file which can be sent in a notification as an email template.

要调用每个作业,我们将创建一个假父作业";它单独运行每个子作业(通过 API 使用 cURL)并保存结果(使用 jq 以提取值)在 内联脚本中的 >Markdown 文件 步骤,这是脚本:

To call each job we will create a "fake parent job" which runs each child jobs individually (via API using cURL) and save the result (using jq to extract the values) on an Markdown file in an inline-script step, this is the 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

在工作定义中,它看起来像这样:

And within a Job Definition it would look like this:

<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 以便它可以发送电子邮件.我留下了配置它的步骤:

If you check the "notifications" section you will notice that it will send an email if it executes correctly or if it fails. You need to configure Rundeck so that it can send emails. I leave the steps to configure it:

  1. 停止您的 Rundeck 服务.

  1. Stop your Rundeck service.

rundeck-config.properties 文件中添加电子邮件配置(通常在 /etc/rundeck 路径):

Add the e-mail configuration on the rundeck-config.properties file (usually at /etc/rundeck path):

# e-mail notification settings
grails.mail.host=your-smtp-host.com
grails.mail.port=25
grails.mail.username=your-username
grails.mail.password=yourpassword

更多信息此处.

  1. rundeck-config.properties 文件中添加特定于您的作业的模板配置(检查第 3 行,是作业脚本生成的文件路径):
  1. Add the template configuration, specific for your job, also on rundeck-config.properties file (check the line 3, is the file path generated by the job script):

# 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)

  1. 启动您的 rundeck 服务.

在执行作业的那一刻,您将看到个人执行收件箱.

At the moment of executing your job, you will see the individual execution and the report in the inbox.

这篇关于如何将 Rundeck 作业的步骤状态获取到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆