如何在厨师中定义和调用数组?

时间:2017-05-01 12:02:45

标签: chef chef-recipe

以下是我用来复制许多文件的资源。

['contacts_nagios2.cfg','generic-host_nagios2.cfg','generic-service_nagios2.cfg','hostgroups_nagios2.cfg','services_nagios2.cfg','timeperiods_nagios2.cfg'].each do |file|
  cookbook_file "/etc/nagios3/conf.d/#{file}" do
    source  "#{file}"
    mode '0644'
  end
end

我们可以定义 [' contacts_nagios2.cfg',' generic-host_nagios2.cfg',' generic-service_nagios2.cfg',&#39 ; hostgroups_nagios2.cfg',' services_nagios2.cfg',' timeperiods_nagios2.cfg'] 作为同一食谱中的属性?

1 个答案:

答案 0 :(得分:3)

这将创建一个Array类型的属性,可以在配方文件中定义:

node.default['nagios_config_files']=['contacts_nagios2.cfg','generic-host_nagios2.cfg','generic-service_nagios2.cfg','hostgroups_nagios2.cfg','services_nagios2.cfg','timeperiods_nagios2.cfg']

如果你想添加,你可以稍后再做:

node.default['nagios_config_files']|=['more_configs.cfg','even_more_configs.cfg']

(注意'='符号前的管道'|'。

然后您可以像以前一样使用该属性:

node['nagios_config_files'].each do |file|
  cookbook_file "/etc/nagios3/conf.d/#{file}" do
    source  "#{file}"
    mode '0644'
  end
end