请查看以下代码,使用 Chef 中的 log
资源。
log 'Hello there' do
level :info
notifies :run, "log_to_chat('Hello there')"
end
当我将它传递给函数 log_to_chat
时,有没有办法引用资源 name
(在本例中:'Hello there')。
我想是这样的:
log 'Hello there' do
level :info
notifies :run, "log_to_chat(#{name})"
end
添加我对 log_to_chat 的尝试。
尝试 1:
resource_name :log_to_chat
property :message, kind_of: String, name_property: true
chat_url = 'https://chat.server/abcdef'
action :run do
execute do
command "curl -m 5 -i -X POST -d \"payload={...}\" #{chat_url}"
ignore_failure true
end
end
问题:如何将 :message
参数作为 notifies
行中的一个衬里传递?
notifies :run, "log_to_chat[message]", --pass :message how??--
尝试 2:
module Chat
def log_to_chat(message)
chat_url = 'https://chat.server/abcdef'
action :run do
execute "curl" do
command "curl -m 5 -i -X POST -d \"payload={...}\" #{chat_url}"
ignore_failure true
end
end
end
end
编辑:尝试 2 失败,因为您不能在定义中使用资源
最佳答案
您可以引用name
变量。来自 documentation你可以读到“name
是资源 block 的名称”。请记住,您想使用 block 的名称(在您的情况下是 Hello there
)而不是资源名称(在问题的片段中是 log
)
关于 ruby /Chef : is there a way to refer to the resource 'name' and pass to a function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39038180/