如何在厨师中运行 bash 脚本文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27637589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:04:01  来源:igfitidea点击:

How to run a bash script file in chef?

bashfilechef

提问by jmvbxx

I wrote a short bash script and stored it in files/default/bash.sh.

我写了一个简短的 bash 脚本并将其存储在files/default/bash.sh.

How do I link it to get it to run in my main default recipe? It needs to run as sudo because I'm on an ubuntu system.

如何链接它以使其在我的主要默认配方中运行?它需要以 sudo 身份运行,因为我在 ubuntu 系统上。

回答by jmvbxx

After searching high and low I finally found an answer:

经过高低搜索,我终于找到了答案:

cookbook_file "/tmp/lib-installer.sh" do
  source "lib-installer.sh"
  mode 0755
end

execute "install my lib" do
  command "sh /tmp/lib-installer.sh"
end

Thanks to this link!

感谢这个链接

回答by coderanger

You can also include the script directly in your recipe if it isn't too long via the bashresource. By default any program run by Chef uses the same user as Chef is running as, which is usually root already. You can use the userparameter to things like executeand bashto switch to a different user or just explicitly state that it should be root to make things self-documenting a tad.

如果脚本不是太长,您还可以通过bashresource直接将脚本包含在您的配方中。默认情况下,Chef 运行的任何程序都使用与运行 Chef 相同的用户,该用户通常已经是 root。您可以将user参数用于诸如executebash切换到其他用户之类的事情,或者只是明确声明它应该是 root 以使事情自我记录一点。