Skip to main content

Pass captured Key-Value Data into Notifications


Pass captured Key-Value Data into Notifications

Tips

Though these instructions reference Rundeck, they can also be applied to environments running the Enterprise versions of the product, PagerDuty Runbook Automationopen in new window or Process Automationopen in new window.

Overview

Job notifications are messages triggered by a job event. Notifications can occur based on different job events or statuses via email or by calling a webhook. Rundeck users commonly add job results or job key-value data on the notification result to store the job result in the service that receives the notification. In this article, we will show you how to capture useful data and add it to any job notification. The HTTP Notification Step plugin is required to follow this example. The plugin is available hereopen in new window and instructions to install it in Rundeck are hereopen in new window.

Step-by-step example

Creating a Global Variable

  1. Create a new job and add a command step that just displays all environment variables with the env command.
  2. Add a Key Value Data log filter to capture the user name.
  3. For the pattern, enter the following regex ^(USER)\s*=\s*(.+)$ Also, check the Log Data box.
  4. Create a new global variable using Key-Value Data. 5.Add ${data.USER*} on the Value text field, “export” on the Group text field, and “myglobal” in the "Name" text field.
  5. To test the exported variable, add a new step to print it: echo ${export.mydata.Save it and run it to test, the result should be the Key Value Data content.

Using the Variable in notifications

  1. The exported variable is usable on any notification. Edit the job by clicking on the "Notifications'' tab.
  2. Click on "+ Add Notification" in the "On Success" section.
  3. In the "Notification Type" section put the HTTP URL to send the test exported variable content. You can test this using your own unique url from this site: https://webhook.siteopen in new window (Go there in your browser to get one). On the "HTTP Method" select "POST" and put the exported variable in the "Body" section in this format ${export.myglobal}.
  4. Run the job and see the exported variable content from the HTTP service (containing the Key Data variable) on the test site.

Job Definition Example (in YAML format) to import and test.

- defaultTab: nodes
 description: ''
 executionEnabled: true
 id: ea07f41a-71b4-4ed9-91fb-6113de996e48
 loglevel: INFO
 name: TestJob
 nodeFilterEditable: false
 notification:
   onsuccess:
     plugin:
       configuration:
         authentication: None
         body: ${export.myglobal}
         contentType: application/json
         method: POST
         remoteUrl: https://any/webhook/url
         timeout: '30000'
       type: HttpNotification
 notifyAvgDurationThreshold: null
 plugins:
   ExecutionLifecycle: {}
 scheduleEnabled: true
 schedules: []
 sequence:
   commands:
   - exec: env
     plugins:
       LogFilter:
       - config:
           invalidKeyPattern: \s|\$|\{|\}|\\
           logData: 'true'
           regex: ^(USER)\s*=\s*(.+)$
         type: key-value-data
   - configuration:
       export: myglobal
       group: export
       value: ${data.USER*}
     nodeStep: false
     type: export-var
   - exec: echo ${export.myglobal}
   keepgoing: false
   strategy: node-first
 uuid: ea07f41a-71b4-4ed9-91fb-6113de996e48

Resources