在Ruby/Chef中,如果“如果”和唯一的_if;

发布于 2025-02-11 18:51:53 字数 341 浏览 2 评论 0 原文

在厨师(因此,Ruby)中,我看到了两种宣布有条件的方式

resource 'foo' do
  echo "Ubuntu"
end if node['platform'] == 'ubuntu'

,而

resource 'foo' do
  echo "Ubuntu"
  only_if node['platform'] == 'ubuntu'
end

这些有效地做同样的事情吗?在官方文档中,似乎“只有_if”是首选的方式,我找不到许多“结束”的示例,但只是好奇他们是否(看起来像他们看起来一样)条件是正确的)。

谢谢!

In chef (therefor, ruby), I've seen two ways of declaring conditionals

resource 'foo' do
  echo "Ubuntu"
end if node['platform'] == 'ubuntu'

and

resource 'foo' do
  echo "Ubuntu"
  only_if node['platform'] == 'ubuntu'
end

Don't these effectively do the same thing? In the official docs, it seems "only_if" is the preferred way, and I can't find many examples of the "end if", but just curious if they, as they seem, do the same thing (execute the block only if the conditional is true).

Thanks!

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

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

发布评论

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

评论(2

∝单色的世界 2025-02-18 18:51:53

Chef Rourcut Guard Recus vs.表达后的基本

guard scrause 这是厨师DSL的一部分。但是,做...如果是ruby 修饰符控制表达式(有时称为后条件)应用于块,该块以相同的功能,以正常的 ruby​​ if/then then语句 works works。请注意,即使在要评估的表达式之后放置了 ,但仍会对后条件进行评估 first

唯一的视为特定于厨师的资源语句。另一种只是句法糖,由Ruby的解释器支持,您引用的示例(假设它在Ruby Resource Block外部在厨师中起作用;我不费心地测试它)与在更为标准的IF-IF-IF-这样的陈述:

if node['platform'] == 'ubuntu'
  resource 'foo' do
    echo "Ubuntu"
  end 
end

大多数厨师资源都应遵循当前的样式指南和DSL功能,但是后条件在惯用性红宝石中非常普遍,因为它们强调了表达方式而不是条件,并且因为它们允许更简短的代码。

Chef Resource Guard Clauses vs. Expression Post-Conditions

only_if is a guard clause that's part of the Chef DSL. However, do...end if is a Ruby modifier control expression (sometimes called a post-condition) applied to a block that functions the same way a normal Ruby if/then statement works. Note that even though the if is placed after the expression to be evaluated, the post-condition is still evaluated first.

Think of only_if as a Chef-specific resource statement. The other is just syntactic sugar supported by Ruby's interpreter, and the example you cited (assuming it works in Chef outside a Ruby resource block; I didn't bother to test it) is the same as writing the block inside a more standard if-statement like this one:

if node['platform'] == 'ubuntu'
  resource 'foo' do
    echo "Ubuntu"
  end 
end

Most Chef resources should follow the current style guides and DSL features, but post-conditions are very common in idiomatic Ruby because they emphasize the expression rather than the conditional and because they allow for more brevity of code.

倒数 2025-02-18 18:51:53

如果在收敛时表现出 结束时, bealts_if 有微妙的区别(在厨师speak中)。简而言之,当厨师 - client 启动时,它会编译食谱并创建一个将在节点上收敛的资源集合。

为了举例来说,假设我们有一个食谱> Code> cookbook1 在食谱中只有1个资源。当我们在下面的方案中运行此类食谱时:

方案1

使用 do ..结束如果

当条件不匹配时,将资源从汇编中删除。因此,将没有资源可以运行。当 node ['platform'] 不是 ubuntu 时,从Chef-Client运行的示例输出。

Compiling Cookbooks...
Converging 0 resources

方案2

使用唯一保护

资源保留在集合中,但是当 node ['platform'] 不是 ubuntu

Compiling Cookbooks...
Converging 1 resources
Recipe: cookbook1::default
  * resource[foo] action run (skipped due to only_if)

简而言之,如果条件将在“编译”阶段本身过程中运行,例如,例如。厨师资源在“融合”阶段运行。请参阅 Infra Client Client Documentation 有关详细信息。

There is a subtle difference in how only_if and end if behave when a node is converged (in Chef speak). In simple terms, when chef-client starts, it compiles the cookbooks and creates a collection of resources that will converge on the node.

For the sake of example, let's say we have a cookbook cookbook1 with only 1 resource in the recipe. When we run such cookbook in below scenarios:

Scenario 1:

Using do .. end if:

The resource is removed from the compilation when the condition is not matched. So there will be no resources to run. Example output from chef-client run when node['platform'] is not ubuntu.

Compiling Cookbooks...
Converging 0 resources

Scenario 2:

Using only_if guard

The resource remains in the collection, but it is skipped when node['platform'] is not ubuntu.

Compiling Cookbooks...
Converging 1 resources
Recipe: cookbook1::default
  * resource[foo] action run (skipped due to only_if)

In short, pure Ruby code, such as if conditions will run during "compile" phase itself. Chef resources run during the "converge" phase. See the Chef Infra Client documentation for details.

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