我在廚師食譜中有以下哈希,它創建了一個目錄/s
node['fnb_base_directory']['directory_name'].map do |directory_name, dir|
next if directory_name.empty?
directory directory_name do
owner dir['owner']
group dir['group']
mode dir['mode']
recursive dir['recursive']
action dir['action']
only_if "getent passwd #{dir['owner']}" && "getent group #{dir['group']}"
end
end
我只希望廚師嘗試基于此警衛創建目錄:
only_if "getent passwd #{dir['owner']}" && "getent group #{dir['group']}"
所以這基本上意味著在嘗試創建目錄之前必須存在theuser和 the 。group
問題似乎是,當廚師解釋這一點時,我看到它只遵守其中一個陳述句,即只檢查group存在,然后繼續嘗試創建目錄,但會因為用戶不存在而失敗。見下文解釋:
directory("/opt/test_dir_creation") do
action [:create]
default_guard_interpreter :default
declared_type :directory
cookbook_name "fnb_base_wrapper"
recipe_name "fnb_base_directory"
recursive false
owner "nonexistent_user"
group "opc"
mode "0755"
only_if "getent group opc"
end
失敗:
directory[/opt/test_dir_creation] action create
* cannot determine user id for 'nonexistent_user', does the user exist on this system?
================================================================================
Error executing action `create` on resource 'directory[/opt/test_dir_creation]'
================================================================================
Chef::Exceptions::UserIDNotFound
--------------------------------
cannot determine user id for 'nonexistent_user', does the user exist on this system?
用戶尚不存在的原因是因為該用戶是在另一個食譜中創建的,其優先級不如我們的基本廚師(創建目錄)高,因此為什么在創建用戶之前完成目錄創建。
然后將在第二次會聚時創建目錄,然后用戶將在那里存在,然后僅繼續創建目錄。
供參考。我使用getent的是因為我們在服務器上使用 AD,所以它可能并不總是靜態用戶/組,而是駐留在 AD 中的用戶/組。
我也檢查了這個問題: Using multiple conditions in Chef only_if guard
它對我沒有幫助。
您的幫助、指導和建議將不勝感激。
uj5u.com熱心網友回復:
嘗試洗掉中間的引號,以便包括 && 條件在內的整個運算式在 shell 中運行。
only_if "getent passwd #{dir['owner']} && getent group #{dir['group']}"
Chef 接受字串 shell 命令或 ruby?? 塊
only_if "some shell commands which are possibly in a pipeline return 0"
only_if {
return true if <condition1> && <condition2>
return true if <condition3> || <condition4>
return false
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/431893.html
上一篇:表單助手,嵌套資源,不設定父資源
