setevoy
setevoy

Reputation: 4662

Chef: template 'name' from variable?

I have an attribute in attributes/default.rb:

default["host_name"] = "domain.com"

And want to create NGINX's config with hostname from that attribute:

...
template "/etc/nginx/conf.d/#{@host_name}.conf" do
  source 'domain.conf.erb'
  owner 'root'
  group 'root'
  mode '0644'
end
...

But during build - Chef can't see host_name:

 ...
 [14:18:28][Step 1/1] Recipe: nginx_proxy::default
 [14:18:28][Step 1/1]   * template[/etc/nginx/conf.d/.conf] action create
 [14:18:28][Step 1/1]     - create new file /etc/nginx/conf.d/.conf
 [14:18:28][Step 1/1]     - update content in file /etc/nginx/conf.d/.conf rom none to cc9a26
 ...

What's wrong here? Can it be achieved at all?

Chef docs don't say anything about "dynamic" name for template.

Upvotes: 0

Views: 792

Answers (1)

StephenKing
StephenKing

Reputation: 37630

You need to use the node object to access its attributes. What you are try to accessing with @host_name is a local variable.

The following should work:

template "/etc/nginx/conf.d/#{node['host_name']}.conf" do
  source 'domain.conf.erb'
  owner 'root'
  group 'root'
  mode '0644'
end

Upvotes: 2

Related Questions