2016-03-16 20 views
2

我正試圖在刀/ Ruby環境中執行bash腳本。例如:在ruby/knife環境中執行bash腳本時如何實現冪等性?

cookbook_file "test.sh" do 
    path "/tmp/test.sh" 
    mode "755" 
    action :create 
end 

bash "execute test.sh on #{nodeName}" do 
    code <<-EOH 
    sh test.sh arg1 arg2 
    EOH 
    #only_if { false } 
end 

如何使用only_ifnot_if?所以當我們第二次執行並且「test.sh」的內容沒有改變時,它應該跳過執行。我得到這個:

* cookbook_file[test.sh] action create (up to date) 

,但它仍然執行第二次,第三次......

+0

你的意思是說'test.sh'在每次資源運行時都可能有所不同,並且只有在自上次運行後更改後纔想運行'test.sh'? –

+0

test.sh是一樣的。我只想在它改變時運行。我如何使用only_if/not_if和文件校驗和。或者以其他方式。 –

+0

我也有這個:cookbook_file「test.sh」做 路徑「/tmp/test.sh」 模式「755」 操作:創建 結束 –

回答

4

你不會使用保護,而不是你會使用一個通知:

execute 'run test' do 
    action :nothing 
    command 'bash /tmp/test.sh arg1 arg2' 
end 

cookbook_file "test.sh" do 
    path "/tmp/test.sh" 
    mode "755" 
    notifies :run, 'execute[run test]', :immediately 
end 

通知在資源更新時觸發,所以無論何時cookbook文件更改,它都將運行execute。另外,您希望使用execute而不是bashscript,因爲您正在運行命令而不是內嵌腳本文件。

+0

謝謝。有效。這是很好的答案和很好的解釋。再次非常感謝你。 –