问题描述
我有一个带有密钥对的 JSON,我想在作业执行期间动态访问 Rundeck Options 中的值。 对于 shell 脚本,我们可以执行 $RD_OPTIONS_。 同样,我可以在 JSON 文件中使用某种格式吗?
解决方法
只需在 inline-script 步骤中使用 @option.myoption@
。
您需要一个工具用于内联脚本步骤来操作 Rundeck 上的 JSON 文件。我用 JQ 做了一个例子。或者,您可以使用 bash script-fu 来达到相同的目标。
例如,使用这个 JSON 文件:
{
"books": [{
"fear_of_the_dark": {
"author": "John Doe","genre": "Mistery"
}
}]
}
使用以下 jq 调用更新文件:
直接在终端中测试
jq '.books[].fear_of_the_dark += { "ISBN" : "9999" }' myjson.json
在 Rundeck 内联脚本上
echo "$(jq ''.books[].fear_of_the_dark += { "ISBN" : "@option.isbn@" }'' myjson.json)" > myjson.json
检查内联脚本作业的外观(检查 here 以了解如何将作业定义导入 Rundeck 实例)。
- defaultTab: nodes
description: ''
executionEnabled: true
id: d8f1c0e7-a7c6-43d4-91d9-25331cc06560
loglevel: INFO
name: JQTest
nodeFilterEditable: false
options:
- label: isbn number
name: isbn
required: true
plugins:
ExecutionLifecycle: null
scheduleEnabled: true
sequence:
commands:
- description: original file content
exec: cat myjson.json
- description: pass the option and save the content to the json file
fileExtension: .sh
interpreterArgsQuoted: false
script: 'echo "$(jq ''.books[].fear_of_the_dark += { "ISBN" : "@option.isbn@"
}'' myjson.json)" > myjson.json'
scriptInterpreter: /bin/bash
- description: modified file content (after jq)
exec: cat myjson.json
keepgoing: false
strategy: node-first
uuid: d8f1c0e7-a7c6-43d4-91d9-25331cc06560
最后,检查 result。