voltas
voltas

Reputation: 563

Chef/Ruby terminologies

I have started with Chef and going thru the documentation (templates: https://docs.chef.io/resource_template.html and and in multiple places I see the below format i.e 2 or 3 square brackets one after another. I have no prior Ruby experience so I'm not sure whether it's a Ruby stuff or particular to do with Chef.

What exactly we're achieving with that construct ? A basic example would do.

For example w.r.t the below, what exactly those symbol does because on some places it has been declared with symbols and some don't.

node.default['nginx']['remote_ip_var'] = 'remote_addr'
node.default['nginx']['authorized_ips'] = ['127.0.0.1/32']

:server_options =>     node[:site][:matching_node][:server][:options],
:proxy_options =>      node[:site][:matching_node][:proxy][:options

I went thru the "Ruby in twenty minutes" documentation to get an overview before starting with Chef but couldn't get any information what I'm seeking

Any help would be really helpful.

template '/etc/sudoers' do
  source 'sudoers.erb'
  variables(sudoers_groups: node['authorization']['sudo']['groups'],
            sudoers_users: node['authorization']['sudo']['users'])
end

node.default['nginx']['remote_ip_var'] = 'remote_addr'
node.default['nginx']['authorized_ips'] = ['127.0.0.1/32']

template "#{node[:matching_node][:dir]}/sites-available/site_proxy.conf" do
  source 'site_proxy.matching_node.conf.erb'
  variables(
    :ssl_certificate =>    "#{node[:matching_node][:dir]}/shared/certificates/site_proxy.crt",
    :server_options =>     node[:site][:matching_node][:server][:options],
    :proxy_options =>      node[:site][:matching_node][:proxy][:options]
  )
end

Upvotes: 0

Views: 28

Answers (1)

coderanger
coderanger

Reputation: 54181

With normal Ruby Hashes, you would correct in assuming that x['foo'] and x[:foo] are separate keys. However for simplicity, Chef's node attribute objects convert all keys to strings so you can use either syntax equally. We recommend (and our linter tool will help enforce) that you use strings, but some people prefer the visual style of symbols.

Upvotes: 1

Related Questions