在Ruby/Chef中,如果“如果”和唯一的_if;
在厨师(因此,Ruby)中,我看到了两种宣布有条件的方式
resource 'foo' do
echo "Ubuntu"
end if node['platform'] == 'ubuntu'
,而
resource 'foo' do
echo "Ubuntu"
only_if node['platform'] == 'ubuntu'
end
这些有效地做同样的事情吗?在官方文档中,似乎“只有_if”是首选的方式,我找不到许多“结束”的示例,但只是好奇他们是否(看起来像他们看起来一样)条件是正确的)。
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Chef Rourcut Guard Recus vs.表达后的基本
guard scrause 这是厨师DSL的一部分。但是,
,但仍会对后条件进行评估 first 。
做...如果
是ruby 修饰符控制表达式(有时称为后条件)应用于块,该块以相同的功能,以正常的 ruby if/then then语句 works works。请注意,即使在要评估的表达式之后放置了将
唯一的
视为特定于厨师的资源语句。另一种只是句法糖,由Ruby的解释器支持,您引用的示例(假设它在Ruby Resource Block外部在厨师中起作用;我不费心地测试它)与在更为标准的IF-IF-IF-这样的陈述:大多数厨师资源都应遵循当前的样式指南和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 theif
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: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.
如果在收敛时表现出 结束时,
bealts_if
有微妙的区别(在厨师speak中)。简而言之,当
厨师 - client
启动时,它会编译食谱并创建一个将在节点上收敛的资源集合。为了举例来说,假设我们有一个食谱> Code> cookbook1 在食谱中只有1个资源。当我们在下面的方案中运行此类食谱时:
方案1 :
使用
do ..结束如果
:当条件不匹配时,将资源从汇编中删除。因此,将没有资源可以运行。当
node ['platform']
不是ubuntu
时,从Chef-Client运行的示例输出。方案2 :
使用
唯一
保护资源保留在集合中,但是当
node ['platform']
不是ubuntu
。简而言之,如果条件将在“编译”阶段本身过程中运行,例如
,例如
。厨师资源在“融合”阶段运行。请参阅 Infra Client Client Documentation 有关详细信息。
There is a subtle difference in how
only_if
andend if
behave when a node is converged (in Chef speak). In simple terms, whenchef-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 notubuntu
.Scenario 2:
Using
only_if
guardThe resource remains in the collection, but it is skipped when
node['platform']
is notubuntu
.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.