Configuration-Management

Cfengine 根據變數值執行動作

  • October 18, 2013

在 cfengine 中,我有一個設置為命令輸出的變數。假設變數 myoutput 設置為“hi world”。如何根據 myoutput 的內容執行命令。

我想做這樣的事情(sudo cfengine 程式碼):

bundle agent test
{

   vars:
       "myoutput" string => execresult("echo 'hi world';","noshell");

   commands:
       myoutput=="hi world"::
           "/usr/bin/php myaction.php";  
}

有一個 strcmp() 函式:https ://cfengine.com/archive/manuals/cf3-Reference#Function-strcmp

試試這個:

bundle agent test
{
vars:
   "expected" string => "hi world";
   "myoutput" string => execresult("/bin/echo 'hi world'","noshell");

classes:
"equal" expression => strcmp($(myoutput),$(expected));

reports:
 equal::
   "output is AS expected: $(myoutput)";
    # do other stuff                                                                                                                                     
}

如果 execresult() 的輸出與預期的輸出相同,這將設置一個“等於”類。

引用自:https://serverfault.com/questions/546725