【问题标题】:Understanding chef cookbook snippet了解厨师食谱片段
【发布时间】:2020-09-15 14:02:49
【问题描述】:

我无法解释 Chef 食谱中的以下代码:

systemd_unit '<service_name>' do      
   action %i[enable start]
end

我从systemd_unit resource 阅读了有关 systemd_unit 的信息。然而,这里的行动是如何确定的呢?我正在尝试将这本食谱转换为 ansible,并想先了解食谱中发生的事情。

另外,作为菜谱的新手,我还想确认:

include_recipe '<cookbook_name>'

提供然后我的理解是,它包括给定食谱中的default.rb,并且不包括该食谱中的其他食谱。如果正确,请告诉我。

【问题讨论】:

    标签: chef-infra cookbook


    【解决方案1】:

    %i[start, enable] 为数组,服务先启动后启用自动启动。

    包含食谱仅包含默认食谱,对于特定食谱使用 include 'cookbook::recipe'

    最好的问候

    【讨论】:

    • 感谢@Psyreactor
    【解决方案2】:

    除了@Psyreactor 的答案之外,还提供了一个扩展的答案。

    Chef recipe 中的操作由 Ruby symbols 表示,如 :create:start 等。当要对同一资源执行多个操作时,它们将作为数组传递。

    所以不要像这样为同一个服务编写两个资源声明:

    # Enable the service
    systemd_unit '<service_name>' do 
      action :enable
    end
    
    # Start the service
    systemd_unit '<service_name>' do 
      action :start
    end
    

    可以写成一个:

    # Enable and start the service
    systemd_unit '<service_name>' do 
      action [ :enable, :start ]
    end
    

    注意:%i 是一种省略: 字符并创建符号数组的方法。 %i(enable, start)[ :enable, :start ] 相同。

    主厨“动作”在 Ansible 中被称为“状态”。因此,对于 Ansible 中的类似服务,您可以:

    systemd:
      name: '<service_name>'
      enabled: yes
      state: 'started'
    

    它包括给定食谱中的default.rb,不包括该食谱中的其他食谱。

    没错。但是,该食谱中的其他食谱未包含在运行中取决于 default.rb

    【讨论】:

    • 感谢您的详细解释。现在确实有意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 2021-11-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多