了解厨师食谱片段

时间:2020-09-14 22:23:56

标签: chef-infra cookbook

我无法解释《厨师食谱》中的以下代码:

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

我从systemd_unit resource中了解到systemd_unit。但是,这里如何确定行动?我正在尝试将此食谱转换为ansible,并想首先了解食谱中发生的事情。

此外,我还是菜鸟新手,我还想确认是否:

include_recipe '<cookbook_name>'
如果提供

,那么我的理解是,它包括给定食谱中的default.rb,并且不包含该食谱中的其他食谱。如果正确的话,请告诉我。

2 个答案:

答案 0 :(得分:2)

%i [start,enable]是一个数组,服务首先启动,然后启用以自动启动。

包含食谱仅包含默认食谱,对于特定食谱,请使用“ cookbook :: recipe”

最好的问候

答案 1 :(得分:1)

除了@Psyreactor的答案以外,还提供扩展的答案。

Chef食谱中的动作由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

相关问题