如何在Chef中使用grep文件名并从中创建目录?

问题描述

我有一个名为“ executables”的目录,其中包含3个二进制文件。 “ x”,“ y”和“ z”。我的“可执行文件”目录包含在我的厨师食谱的“文件文件夹中。

在我的主要食谱中,如何编写代码,以便按如下方式创建3个新目录:

"/workdir/x/5.2/" # where the binary "x" is contained in directory : "/workdir/x/5.2/"
"/workdir/y/5.2/" # where the binary "y" is contained in directory : "/workdir/y/5.2/"
"/workdir/z/5.2/" # # where the binary "z" is contained in directory : "/workdir/z/5.2/"

有没有一种方法可以在for循环内执行。而不是拥有三个不同的“ cookbook_file”资源块?也许有一种方法可以grep二进制名称并从中创建一个目录,然后将该二进制文件放入该目录中。

解决方法

可以使用cookbook_file资源,但是在复制之前必须存在/创建目标目录。我们可以遍历“可执行文件”并创建该目录(如果需要)并复制文件。

考虑食谱的files/default/executables

x
y
z

然后在您的食谱中

%w(
  x
  y
  z
).each do |exe|
  directory "/workdir/#{exe}/5.2" do
    recursive true
  end
    
  cookbook_file "/workdir/#{exe}/5.2/#{exe}" do
    source "executables/#{exe}" 
  end
end

您还可以查看remote_directory resource,它将动态创建目录并复制文件。