【发布时间】:2016-08-22 16:44:31
【问题描述】:
请参阅以下代码,使用 chef 中的 log 资源。
log 'Hello there' do
level :info
notifies :run, "log_to_chat('Hello there')"
end
当我将资源name 传递给函数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 无效,因为您不能在定义中使用资源
【问题讨论】:
标签: ruby chef-infra cookbook recipe