【问题标题】:Chef - execute vs bash resourceChef - 执行 vs bash 资源
【发布时间】:2016-04-21 16:17:55
【问题描述】:

我使用了execute 资源或bash 资源。

两者都达到相同的结果:

bash 'Execute my script' do 
  user 'root'
  cwd  '/mydir'
  code <<-EOH
    ./myscript.sh
  EOH
end

execute 'Execute my script' do 
  user    'root'
  cwd     '/mydir'
  command './myscript.sh'
end

我看到的唯一区别是 bash 实际上创建了一个 shell 脚本(名为 /tmp/chef-script#{date}{#id}),其中写入了 code

executebash 资源之间使用 Chef 执行 shell 脚本的最佳做法是什么?

【问题讨论】:

    标签: ruby chef-infra recipe


    【解决方案1】:

    对于单个脚本,请使用 executebash 资源用于在配方代码中包含内联脚本内容。

    【讨论】:

      【解决方案2】:

      在 bash & 执行块中,我们需要编写代码来捕获错误,就好像您添加了多个命令并且厨师会获取最后一个命令的状态。 更清楚地说 - 当 Bash/execute 块只有一个命令时,厨师会发现问题,如果下一个命令成功,那么它将采用最后一个命令状态。

      bash 'Execute my script' do 
        user 'root'
        cwd  '/mydir'
        code <<-EOH
          ./myscript.sh
          ls srini ##This will fail
          ls  ## this will be successful
        EOH
      end
      
      execute 'Execute my script' do 
        user    'root'
        cwd     '/mydir'
        command './myscript.sh'
        command 'ls srini' #will fail
        command 'ls' # will be successful
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多