Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Chef infra 建议在包含的配方中使用return语句吗?_Chef Infra_Chef Recipe_Chef Solo - Fatal编程技术网

Chef infra 建议在包含的配方中使用return语句吗?

Chef infra 建议在包含的配方中使用return语句吗?,chef-infra,chef-recipe,chef-solo,Chef Infra,Chef Recipe,Chef Solo,我有几本食谱,其中包括其他几本食谱,这取决于每本食谱的需要。 包含的食谱声明了通知其他服务的服务 其中一本包含的烹饪书common\u actions包含在所有其他烹饪书中,因为它包含所有人都通用的操作 include_recipe 'cookbook1' include_recipe 'common_actions' include_recipe 'cookbook2' # Several cookbooks have such includes, but 'common_actions' #

我有几本食谱,其中包括其他几本食谱,这取决于每本食谱的需要。 包含的食谱声明了通知其他服务的服务

其中一本包含的烹饪书
common\u 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\u actions
cookbook中包含一个条件返回语句是否明智,这样就可以强制include cookbook不基于该条件编译/执行?为了这个问题,请考虑任何虚假的情况,如:

if node['IP'] == 'xyz'
    # All including cookbooks should execute only IP is xyz
    return
end
带有这样一个返回语句的cookbook是否只能运行某些cookbook?这样做明智吗


注意:我这样做是因为我不想在所有其他烹饪书中复制粘贴相同的代码。

你可以像这样输入顶级返回值,也可以在include\u配方本身上使用条件。

你可以像这样输入顶级返回值,也可以在include\u配方本身上使用条件。

如果我理解的话正确地说,这不会满足您的要求,因为:

  • 一个菜谱将只包含一次,如果运行列表中有多个菜谱调用
    include_recipe A::B
    ,则菜谱A的菜谱B将只编译一次,连续调用将不起作用(不会复制菜谱资源)
  • return
    语句将结束实际的配方编译,在您的情况下,它将停止烹饪书
    常见动作中配方
    默认值的编译
  • 您可以使用
    node.run\u state
    ,它是仅在运行期间可用的哈希值。
    例如,您可以使用它存储
    命令\u操作中的另一个条件散列

    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
    
    现在,您可以在其他配方中使用这些值来控制其行为,同时仍将条件定义保留在中心位置

    如果
    节点['IP']
    'xyz'
    ,则在应不运行的配方中,您将从以下内容开始配方:

    return if node.run_state['IP_allowed']
    
    return unless node.run_state['IP_allowed']
    
    如果
    节点['IP']
    'xyz'
    ,则应仅在其中一个上运行,您将从以下内容开始配方:

    return if node.run_state['IP_allowed']
    
    return unless node.run_state['IP_allowed']
    
    另一个值可用于记录不同环境中的配方,如下所示:

    log "Message to log" do
      level node.run_state['LoggerLevel']
    end
    

    如果我正确理解了你的意思,这不会满足你的要求,因为:

  • 一个菜谱将只包含一次,如果运行列表中有多个菜谱调用
    include_recipe A::B
    ,则菜谱A的菜谱B将只编译一次,连续调用将不起作用(不会复制菜谱资源)
  • return
    语句将结束实际的配方编译,在您的情况下,它将停止烹饪书
    常见动作中配方
    默认值的编译
  • 您可以使用
    node.run\u state
    ,它是仅在运行期间可用的哈希值。
    例如,您可以使用它存储
    命令\u操作中的另一个条件散列

    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
    
    现在,您可以在其他配方中使用这些值来控制其行为,同时仍将条件定义保留在中心位置

    如果
    节点['IP']
    'xyz'
    ,则在应不运行的配方中,您将从以下内容开始配方:

    return if node.run_state['IP_allowed']
    
    return unless node.run_state['IP_allowed']
    
    如果
    节点['IP']
    'xyz'
    ,则应仅在其中一个上运行,您将从以下内容开始配方:

    return if node.run_state['IP_allowed']
    
    return unless node.run_state['IP_allowed']
    
    另一个值可用于记录不同环境中的配方,如下所示:

    log "Message to log" do
      level node.run_state['LoggerLevel']
    end