[英]How to get args for an ansible module written in bash?
我寫了一個 ansible 模塊是 bash。它工作正常,但如果想將 arguments 傳遞給該模塊並在 bash 模塊中讀取它們,我該怎么做。請幫忙
- name: get top processes consuming high cpu
gettopprocesses:
numberofprocesses: 5
在庫中我有 bash 腳本 library/gettopprocesses.sh
#!/bin/bash
TPCPU=$(ps aux --sort -%cpu | head -${numberofprocesses}
echo "{\"changed\": false, "\msg\": {"$TPCPU"}}"
exit 0
我這樣寫你的 bask:你必須添加 source $1 來指定你有 args
#!/bin/bash
source $1
NUMBERPROC=$numberofprocesses
TPCPU=$(ps aux --sort -%cpu | head -${NUMBERPROC})
printf '{"changed": %s, "msg": "%s", "contents": %s}' "false" "$TPCPU" "contents"
exit 0
您可以添加一個測試來檢查是否給出了正確的 arg:
#!/bin/bash
source $1
if [ -z "$numberofprocesses" ]; then
printf '{"failed": true, "msg": "missing required arguments: numberofprocesses"}'
exit 1
fi
NUMBERPROC=$numberofprocesses
TPCPU=$(ps aux --sort -%cpu | head -${NUMBERPROC})
printf '{"changed": %s, "msg": "%s", "contents": %s}' "false" "$TPCPU" "contents"
exit 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.