2017-02-24 32 views
0

我有兩個廚師食譜我想運行在一個明確的順序。首先是安裝配方,然後是配置。紅寶石塊不執行代碼,只有打印輸出

這裏是代碼調用配方:

ruby_block "bowbridge_config" do 
    block do 
     run_context.include_recipe "ids::bowbridge_config" 
    end 
    action :nothing 
    end 

    ruby_block "bowbridge_install" do 
    block do 
     run_context.include_recipe "sap-bowbridge::default" 
    end 
    notifies :run, 'ruby_block[bowbridge_config]', :delayed 
    end 

我成功執行配置配方但當配置配方僅執行打印件被打印前的安裝配方。

配置食譜代碼:

mcaf_lib = find_file "/opt/bowbridge/libAVB*_mcaf.so" 
    Chef::Log.info("==> bowbridge_config mcaf_lib is #{mcaf_lib}. Vsi file is #{vsi_file}") 
    bb_cfg = File.basename(find_file "/opt/bowbridge/bbvsa*.cfg") 
    Chef::Log.info("==> bowbridge_config recipe is triggered") 

# Setup bowbridge config file 
    directory "/etc/bowbridge" do 
    end 
    file "/etc/bowbridge/" + bb_cfg do 
    owner 'root' 
    group 'root' 
    mode 0755 
    content ::File.open("/opt/bowbridge/" + bb_cfg).read 
    action :create 
    end 
    Chef::Log.info("==> bowbridge_config before link creation") 
    link "/lib64/libvsa.so" do 
    to "#{mcaf_lib}" 
    end 

上面的代碼顯示這樣的輸出:

[2017-02-24T11:25:36+00:00] INFO: ruby_block[bowbridge_install] sending run action to ruby_block[bowbridge_config] (delayed) 
[2017-02-24T11:25:36+00:00] INFO: Processing ruby_block[bowbridge_config] action run (ids::default line 82) 
[2017-02-24T11:25:37+00:00] INFO: ==> bowbridge_config recipe is triggered 
[2017-02-24T11:25:37+00:00] INFO: ==> bowbridge_config before link creation 
[2017-02-24T11:25:37+00:00] INFO: ruby_block[bowbridge_config] called 

不需要/ etc /正在創建bowbridge目錄和內部/沒有創建鏈接lib64下。我可能做錯了什麼?從您的實際2個紅寶石塊

+0

如果你不打算給資源添加任何特定的選項,比如目錄'/ etc/bowbridge',你不需要添加do/end塊,只需要保留:'directory'/ etc/bowbridge'' – Navarro

回答

1

我不會推薦使用紅寶石塊這種方式,只需添加食譜的順序,你想要廚師執行它們。在這種情況下:

include_recipe 'sap-bowbridge::default' 
include_recipe 'ids::bowbridge_config' 

現在,如果您需要在執行Chef-client期間獲得一些值,您可以使用懶惰評估。 Chef網站上有plenty of information

舉個例子,看看這個:

link '/lib64/libvsa.so' do 
    to lazy { find_file '/opt/bowbridge/libAVB*_mcaf.so' } 
end 

最後,儘量用簡單的報價' '而非雙" "如果不需要插值或添加變量文本,也與此有關,請用途:

"/etc/bowbridge/#{bb_cfg}" 

不是

"/etc/bowbridge/" + bb_cfg 
+0

這看起來像一個整潔的方法,但我需要在bash模板中稍後使用#{mcaf_lib}。我可以使用「懶惰」分配變量嗎?例如,mcaf_lib lazy {find_file'/opt/bowbridge/libAVB*_mcaf.so'} – Vladimir

+0

這是我在我的評論中粘貼的鏈接中的示例之一。 '模板 '/tmp/canvey_island.txt' 做 源 'canvey_island.txt.erb' 變量( 懶惰{ {:canvey_island => node.run_state [ 'sea_power']} } ) end' – Navarro

+0

謝謝@Navarro。抱歉格式不正確:'bash'Extract vsi.properties'do cwd「#{vsi_tmp}」 變量( lazy {0} {0} {:mcaf_lib => Dir ['/ opt/bowbridge/libAVB * _mcaf.so'] [0]}} ) 代碼<< - EOH 罐子-xvf#{vsi_file} CFG/LINUX/vsi.properties Java的罐子#{vsi_file}信息-V#{mcaf_lib} -cfg CFG/LINUX/vsi.properties> /tmp/vsi.out EOH end'我現在面臨的問題是變量不是公認的方法。我看到它不是可接受的bash模板的一部分。 – Vladimir

1

移至此:

include_recipe "sap-bowbridge::default" 
include_recipe "ids::bowbridge_config" 

你有同樣的效果,廚師榮譽的運行列表順序,並列入順序,所以沒有必要過你的兩個的複雜性ruby_block。

您的實際代碼將在收斂時間運行,由於通知延遲,配置配方在運行中延遲運行。如果這是你的需要,寫一個custom_resource,而不是圍繞運行列表進行調整。

+0

它尊重運行列表順序,但bowbridge_config配方的編譯階段將分配值,例如當前不存在的「bb_cfg」。這個問題在這裏定義https://coderanger.net/two-pass/。代碼現在失敗,因爲bb_cfg不存在,mcaf_lib爲空。 – Vladimir

+0

當然它不存在於ruby_block背景...作爲回答說,如果你需要從以前的配方變量,使用自定義資源(也許懶其屬性來填充值)。請參閱答案中的鏈接。 – Tensibai

+1

(也請不要做加法的文字,在雙引號的文本插在那裏爲:'「文本#{}變量」') – Tensibai