ruby /Chef : is there a way to refer to the resource 'name' and pass to a function?

标签 ruby chef-infra cookbook recipe

请查看以下代码,使用 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/

相关文章:

ruby - Ruby 1.9 中的 ARGF.class 是什么?

ruby-on-rails - 从 3.5 升级到 Paperclip 4.1 时没有 validates_attachment_file_name

ruby - Chef Recipe - 仅当用户资源执行了manage_home时如何运行资源

python - 只允许名称参数的函数

chef-recipe - 如何使用 opscode chef 和数据库 cookbook 命令查询执行

ruby-on-rails - 返回 false,则无法将项目添加到 :through 的 has_many 中

ruby - 如何在 Ruby 中的方法之间传递变量?

Vagrant 供应,Chef 错误 : You must specify at least one cookbook repo path

chef-infra - Chef - Foodcritic FC019 误报

tomcat - 执行[等待 tomcat7] Action 运行,