Sometimes it might be a little confusing what exactly ends up in the output of an automation and why the follow up action using that output does not work as you had intended. We’ll share some details and show some neat tricks what to do here.
The AutomationOutput feature in CEM
Inside the Runbook functionality of CEM, you can map parameters. One way to do that, is to use the output of a previous automation to fill the parameter in a subsequent step. An easy example of using this, might be to run a command like
top and then use it, to stop the process claiming the most resources. As newlines in the output of an automation can get in the way, this post will show you how to handle it correctly.
Echo in Scripts
One of the most common ways to produce output from your shell script is to use echo. As shell scripts by default do not produce any output to stdout. But there is a caveat as you can see from the official man page: The echo command displays a full line of text, including a newline character. This newline character is therefore also included in the automation output. If the follow up action within your runbook cannot deal with newline characters, your runbook will most likely fail.
Luckily the solution is quite easy: echo supports a flag to suppress the trailing newline character:
echo -n will only create the output you actually wrote in the script.
Of course you might (or might not) encounter the same issue in other scripting languages such as Perl or Python. Make sure you understand the output methods of your scripting language to see whether or not additional characters are added when sending something to stdout. Usually there is an option to leave out newline characters. In some cases the output without newline is even the default.
Script Automation Target String
The use case where additional whitespace and/or newline characters make the most trouble is possibly the following setup:
- Execute automation #1 which produces an output string of hostnames
- Execute automation #2 which uses the automationOutput mapping from step #1
In this case, if you added an additional newline character at the end of the output for automation #1, the automation #2 will not be able to run correctly. The target string contains an invalid hostname. Our advice is: Take extra care of automation output strings when using them in further steps and/or automations. For the future we plan to implement some quality of life improvements and strip away unnecessary characters from the target string, until then take care!