【问题标题】:Command execution in functions in chef-solochef-solo 函数中的命令执行
【发布时间】:2016-02-04 07:09:18
【问题描述】:

我写了一个简单的厨师脚本。 /DirectoryPath 中有一个文件夹 t1。现在我在 /DirectoryPath/t5 中创建一个新文件夹。但是 listDirectory() 方法给出了相同的输出,就好像没有创建新目录一样。我在 MAC 和 Chef 11.4 上

我的脚本如下所示

recipe.rb

def listDirectory() 
  ls = `ls ~/Delete`
  return ls
end

log(listDirectory())

directory '/DirectoryPath/t5' do
  action :create
end

log(listDirectory())

输出

Recipe: test::default
  * log[t1] action write[2015-11-03T15:02:47-08:00] INFO: Processing log[t1] action write
[2015-11-03T15:02:47-08:00] INFO: t1

  * directory[/DirectoryPath/t5] action create[2015-11-03T15:02:47-08:00] INFO: Processing directory[/DirectoryPath/t5] action create (test::default line 10)
[2015-11-03T15:02:47-08:00] INFO: directory[/DirectoryPath/t5] created directory /DirectoryPath/t5

    - create new directory /DirectoryPath/t5
  * log[t1] action write[2015-11-03T15:02:47-08:00] INFO: Processing log[t1] action write
[2015-11-03T15:02:47-08:00] INFO: t1

根据我的说法,第二条日志语句应该显示文件夹 t1 和 t5,但它只显示 t1。即使有一些变化,我能否知道命令返回相同结果的原因可能是什么。我该如何避免呢?

【问题讨论】:

    标签: chef-infra chef-recipe chef-solo


    【解决方案1】:

    这是因为two phases of a chef-client run

    您的调用在编译阶段执行,在directory资源执行之前(在执行阶段)。

    您应该熟悉其他厨师习语,例如 librariesdefinitions。您不应该将 ruby​​ 代码直接嵌入到配方中。

    我进一步建议针对您想要解决的问题发布一个新问题。很确定有一种厨师式的方法来解决它。

    【讨论】:

    • 如果我可以添加指向this question 的链接,Tejay 对这两个 pahse 运行做了很好的解释。
    猜你喜欢
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2015-07-31
    • 2016-12-07
    相关资源
    最近更新 更多