Sekhar
Sekhar

Reputation: 549

How to define and call an array in chef?

Below is the resource I am using to copy many files.

['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

Can we define ['contacts_nagios2.cfg','generic-host_nagios2.cfg','generic-service_nagios2.cfg','hostgroups_nagios2.cfg','services_nagios2.cfg','timeperiods_nagios2.cfg'] as an attribute within the same recipe?

Upvotes: 3

Views: 3344

Answers (1)

chakatz
chakatz

Reputation: 312

This will create an attribute of type Array and can be defined within the recipe file:

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']

and if you want to add to that you can later do:

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

(Note the pipe '|' before the '=' sign.)

Then you can use the attribute like before:

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

Upvotes: 5

Related Questions