iT邦幫忙

0

ansible 返回的結果編輯 On Linux

各位先進不好意思,想對ansible 返回的結果去做一些編輯業,試了許久一直無法達成想要的結果。

指令結果如下

ansible -i inventory/hosts computation -m shell -a "systemctl list-units | grep 'HDFS DataNode'"
dotData-computation01 | CHANGED | rc=0 >>
datanode.service                                                          loaded active running   HDFS DataNode

dotData-computation03 | CHANGED | rc=0 >>
datanode.service                                                          loaded active running   HDFS DataNode

dotData-computation05 | CHANGED | rc=0 >>
datanode.service                                                          loaded active running   HDFS DataNode

dotData-computation04 | CHANGED | rc=0 >>
datanode.service                                                          loaded active running   HDFS DataNode

dotData-computation02 | CHANGED | rc=0 >>
datanode.service                                                          loaded active running   HDFS DataNode

想要達成的目標為

  1. 能否將結果中的文字裁切丟進變數中?
    EX:
    ServerName=dotData-computation[伺服器編號],
    ServiceActive=running
    ServiceName=HDFS DataNode
    ...等

  2. 如何針對ServiceActive=running 且 ServiceName=HDFS DataNode去做累加計算?
    EX:
    環境內共有五台伺服器並希望能統計[HDFS DataNode]服務的總數。

    想達成的輸出結果

    dotData-computation[伺服器編號] | running | HDFS DataNode
    dotData-computation[伺服器編號] | running | HDFS DataNode
    .
    .
    .
    HDFS DataNode Total 5

小弟目前編的語法如下:

for line in `ansible -i inventory/hosts computation -m shell -a "systemctl list-units | grep 'HDFS DataNode'" `

do
    eval $(echo $line | awk -F " " '$1~/comp/{print"ServerName="$1}')
    echo $ServerName
   
done   

以上輸出結果並非本人所想像,請各位先進幫幫忙或指點一下小弟,感謝。

尚未有邦友回答

立即登入回答