在包含的配方中使用 return 语句是否可取?
Is it advisable to use a return statement inside an included recipe?
我有几本食谱,其中包括其他几本食谱,具体取决于每本食谱的需要。
包含的说明书声明通知其他服务的服务。
其中一本说明书 common_actions
包含在所有其他说明书中,因为它包含所有人共有的操作。
include_recipe 'cookbook1'
include_recipe 'common_actions'
include_recipe 'cookbook2'
# Several cookbooks have such includes, but 'common_actions'
# is included in almost all the cookbooks.
# cookbook specific conditional logic that should be
# executed only if some condition in 'common_actions' is true
在 common_actions
食谱中包含一个条件 return 语句是否是一个明智的想法,这样它会强制包含的食谱不 compiled/executed 基于该条件?出于这个问题的目的,请考虑任何虚假条件,例如:
if node['IP'] == 'xyz'
# All including cookbooks should execute only IP is xyz
return
end
具有这样 return 语句的食谱是否会导致某些食谱仅 运行?是否可取?
注意:我这样做是因为我不想在所有其他食谱中复制粘贴相同的代码。
您可以像这样放置一个顶级 return,或者您可以对 include_recipe 本身使用条件。
如果我对你的理解正确,这将不会满足你的要求,因为:
- 一个菜谱只会被包含一次,如果运行列表中有多个菜谱调用
include_recipe A::B
那么菜谱A的菜谱B只会被编译一次,连续调用将是no-op(不会复制食谱资源)。
return
语句将结束实际的配方编译,在您的情况下,它将停止食谱 common_actions
. 中配方 default
的编译
您可以使用 node.run_state
,它是仅在 运行 期间可用的哈希。
例如,您可以使用它来存储 command_actions
食谱中的另一个条件散列。
node.run_state['IP_allowed'] = node['IP'] == 'xyz'
# Probabaly a little silly, but that's the easier I can think of
if node.chef_environment == 'Test'
if node['DoDebugLog'] == true
node.run_state['LoggerLevel'] = 'debug'
else
node.run_state['LoggerLevel'] = 'info'
else
node.run_state['LoggerLevel'] = 'warn'
end
现在您可以在其他配方中使用这些值来控制它们的行为,同时仍将条件定义放在中心位置。
如果 node['IP']
是 'xyz'
,在 不应该 运行 的食谱中,您将开始食谱:
return if node.run_state['IP_allowed']
如果 node['IP']
是 'xyz'
,则应该 运行 仅 ,您将从以下食谱开始:
return unless node.run_state['IP_allowed']
另一个值可用于从不同环境中的配方记录,如下所示:
log "Message to log" do
level node.run_state['LoggerLevel']
end
我有几本食谱,其中包括其他几本食谱,具体取决于每本食谱的需要。 包含的说明书声明通知其他服务的服务。
其中一本说明书 common_actions
包含在所有其他说明书中,因为它包含所有人共有的操作。
include_recipe 'cookbook1'
include_recipe 'common_actions'
include_recipe 'cookbook2'
# Several cookbooks have such includes, but 'common_actions'
# is included in almost all the cookbooks.
# cookbook specific conditional logic that should be
# executed only if some condition in 'common_actions' is true
在 common_actions
食谱中包含一个条件 return 语句是否是一个明智的想法,这样它会强制包含的食谱不 compiled/executed 基于该条件?出于这个问题的目的,请考虑任何虚假条件,例如:
if node['IP'] == 'xyz'
# All including cookbooks should execute only IP is xyz
return
end
具有这样 return 语句的食谱是否会导致某些食谱仅 运行?是否可取?
注意:我这样做是因为我不想在所有其他食谱中复制粘贴相同的代码。
您可以像这样放置一个顶级 return,或者您可以对 include_recipe 本身使用条件。
如果我对你的理解正确,这将不会满足你的要求,因为:
- 一个菜谱只会被包含一次,如果运行列表中有多个菜谱调用
include_recipe A::B
那么菜谱A的菜谱B只会被编译一次,连续调用将是no-op(不会复制食谱资源)。 return
语句将结束实际的配方编译,在您的情况下,它将停止食谱common_actions
. 中配方
default
的编译
您可以使用 node.run_state
,它是仅在 运行 期间可用的哈希。
例如,您可以使用它来存储 command_actions
食谱中的另一个条件散列。
node.run_state['IP_allowed'] = node['IP'] == 'xyz'
# Probabaly a little silly, but that's the easier I can think of
if node.chef_environment == 'Test'
if node['DoDebugLog'] == true
node.run_state['LoggerLevel'] = 'debug'
else
node.run_state['LoggerLevel'] = 'info'
else
node.run_state['LoggerLevel'] = 'warn'
end
现在您可以在其他配方中使用这些值来控制它们的行为,同时仍将条件定义放在中心位置。
如果 node['IP']
是 'xyz'
,在 不应该 运行 的食谱中,您将开始食谱:
return if node.run_state['IP_allowed']
如果 node['IP']
是 'xyz'
,则应该 运行 仅 ,您将从以下食谱开始:
return unless node.run_state['IP_allowed']
另一个值可用于从不同环境中的配方记录,如下所示:
log "Message to log" do
level node.run_state['LoggerLevel']
end