如何在 Chef 中定义一个函数/操作/...,返回一个可在 not_if 等中使用的值

发布于 2024-12-05 19:54:28 字数 258 浏览 2 评论 0原文

我现在正在学习厨师,我正在尝试以重复配置不会破坏任何内容的方式编写所有内容。

我有一个部署在机器上的服务器,然后加载了一些代码。下次配置时,我想首先测试代码是否已加载。我想以通用的方式来做,因为我在不同的食谱中使用它。 我的想法是定义一个函数/定义/等等。我可以调用测试条件并返回值的函数。我希望我可以在 not_if 子句中使用这个函数/...来执行其他操作。

有没有办法在厨师中使用定义/操作/提供者/...来做到这一点,或者我需要在某个地方添加一些红宝石的东西?

I'm learning chef at the moment and I'm trying to write everything in a way that repeated provisioning doesn't break anything.

I have a server that is deployed on the machine and then there is some code loaded into it. The next time of provisioning I like to test first if the code has been loaded already. And I want to do it in a generic way because I use it in different recipes.
My idea would be to define a function/defintion/etc.. I can call the function which tests the condition and returns a value. My hopes would be that I can use this function/... in a not_if clause for other actions.

Is there a way to do this in chef with a defintion/action/provider/... or would I need to add some rubyish stuff somewhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹淡然 2024-12-12 19:54:28

Chef中的资源都有条件执行。

not_if 和 only_if 语句可以将 shell 命令作为字符串或 ruby​​ 块来确定它们是否应该执行其操作。

user "myuser" do
  not_if "grep myuser /etc/password"
  action :create
end

您可能有一个节点属性并将其用作条件或调用返回 true 或 false 的 ruby​​ 方法。

template "/tmp/somefile" do
  mode "0644"
  source "somefile.erb"
  not_if { node[:some_value] }
end

https://web.archive.org/web/20111120120013/http://wiki.opscode.com/display/chef/Resources#Resources-ConditionalExecution

Resources in Chef all have conditional execution.

The not_if and only_if statements can take a shell command as a string or a ruby block to determine if they should perform their action or not.

user "myuser" do
  not_if "grep myuser /etc/password"
  action :create
end

You might have a node attribute and use that as your conditional or call a ruby method that returns true or false.

template "/tmp/somefile" do
  mode "0644"
  source "somefile.erb"
  not_if { node[:some_value] }
end

https://web.archive.org/web/20111120120013/http://wiki.opscode.com/display/chef/Resources#Resources-ConditionalExecution

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文