【问题标题】:Chef : chef-client walks code first before executing itChef : chef-client 在执行之前先遍历代码
【发布时间】:2014-06-27 20:46:25
【问题描述】:

在我想要的厨师食谱中

  1. 解压文件
  2. 使用 zip 中的文件之一来迭代内容。

我遇到的问题是,当我运行 chef-client 时,它甚至在步骤 1 中解压缩文件之前都没有说“没有这样的文件或目录”

这里是提供者的代码:

    action :create do
      if @current_resource.exists
        converge_by("Create #{ @current_resource }") do
           unzip('realFileToUnzip','someLocation')  
           do_something_with_file('realFileToOpen')
        end
      end
    end
....

在同一个提供程序文件中,我有一个定义如下

def unzip(fileToUnzip, unzipToLocation)
  bash "unzip" do
    user "root"
    cwd "/tmp"
    code <<-EOH
        unzip  -o #{fileToUnzip} -d #{unzipToLocation}
    EOH
  end
end

这个def也是

def do_something_with_file(fileToConvert)
  ::File.open(fileToConvert, 'r') do |properties_file|
    properties_file.read.each_line do |line|
      puts line
    end
  end
end

似乎 chef-client 在执行之前先遍历代码。所以在遍历文件中是不存在的,因为它在执行解压缩代码之前不会存在。

我怎样才能避免这种情况?

【问题讨论】:

  • 您需要将该代码包装在do_something_with_file 中的ruby_block 资源中。

标签: ruby chef-infra chef-recipe


【解决方案1】:

来自Chef Docs

在 chef-client 期间使用 ruby​​_block 资源执行 Ruby 代码 跑。 ruby_block 资源中的 Ruby 代码与其他 资源收敛期间,而 Ruby 代码在 ruby_block 资源被评估其他资源之前,作为配方 已编译。

然后你必须创建一个ruby_block并将do_something_with_file的代码插入到这个资源中。也许你必须做一些修改。

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 2020-02-14
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多