如何在 Chef 中定义一个函数/操作/...,返回一个可在 not_if 等中使用的值
我现在正在学习厨师,我正在尝试以重复配置不会破坏任何内容的方式编写所有内容。
我有一个部署在机器上的服务器,然后加载了一些代码。下次配置时,我想首先测试代码是否已加载。我想以通用的方式来做,因为我在不同的食谱中使用它。 我的想法是定义一个函数/定义/等等。我可以调用测试条件并返回值的函数。我希望我可以在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Chef中的资源都有条件执行。
not_if 和 only_if 语句可以将 shell 命令作为字符串或 ruby 块来确定它们是否应该执行其操作。
您可能有一个节点属性并将其用作条件或调用返回 true 或 false 的 ruby 方法。
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.
You might have a node attribute and use that as your conditional or call a ruby method that returns true or false.
https://web.archive.org/web/20111120120013/http://wiki.opscode.com/display/chef/Resources#Resources-ConditionalExecution