Recipe syntax error


#1

Hi All,

I am new to Chef and I was trying to lean how attributes work.

I have simple recipe wich calls an attribute into a template. But I am getting an error when I run it:

Recipe:

[ec2-user@Server recipes]$ cat default.rb

Cookbook Name:: temp_test

Recipe:: default

Copyright 2016, YOUR_COMPANY_NAME

All rights reserved - Do Not Redistribute

node["chef][“site”].each do |sitename, data|
template “/home/ec2-user/#{sitename}.txt” do
source "template_test.erb"
mode "0755"
owner "root"
group "ec2-user"source "template_test.erb"
variables(:site_name => #{sitename}, :port => data[‘port’], :ssl => data[‘ssl’])
variables(:recipe => node[‘recipes’][1],
:test => node[‘temp’][‘name’])
end
end

[ec2-user@Server recipes]$

It throws an error when I try to upload the cookbook.

[ec2-user@Server recipes]$ knife cookbook upload temp_test
Uploading temp_test [0.1.0]
FATAL: Cookbook file has a ruby syntax error:
FATAL: /home/ec2-user/chef-repo/cookbooks/temp_test/recipes/default.rb:10: syntax error, unexpected tIDENTIFIER, expecting ']'
FATAL: node["chef][“site”].each do |sitename, data|
FATAL: ^
FATAL: /home/ec2-user/chef-repo/cookbooks/temp_test/recipes/default.rb:12: syntax error, unexpected tSTRING_BEG, expecting keyword_do or ‘{’ or '('
FATAL: source "template_test.erb"
FATAL: ^
FATAL: /home/ec2-user/chef-repo/cookbooks/temp_test/recipes/default.rb:15: syntax error, unexpected tIDENTIFIER, expecting keyword_end
FATAL: group "ec2-user"source "template_test.erb"
FATAL: ^
FATAL: /home/ec2-user/chef-repo/cookbooks/temp_test/recipes/default.rb:19: syntax error, unexpected keyword_end, expecting ‘)’

Any help would be appreciated.


#2

Hi krishnar,

A double quote is missing after chef: node["chef"]


#3

Ignore!!!

It was a typo!!


#4

Thanks zuazo!!!


#5

Ooops!! A new error…

I modified the recipe like this:

[ec2-user@Server recipes]$ cat default.rb

Cookbook Name:: temp_test

Recipe:: default

Copyright 2016, YOUR_COMPANY_NAME

All rights reserved - Do Not Redistribute

node[“chef”][“site”].each do |sitename, data|
template “/home/ec2-user/#{sitename}.txt” do
source "template_test.erb"
mode "0755"
owner "root"
group "ec2-user"
variables(:site_name => #{sitename}, :port => data[‘port’], :ssl => data[‘ssl’] )
end
end

[ec2-user@Server recipes] [ec2-user@Server recipes]
[ec2-user@Server recipes] knife cookbook upload temp_test Uploading temp_test [0.1.0] FATAL: Cookbook file has a ruby syntax error: FATAL: /home/ec2-user/chef-repo/cookbooks/temp_test/recipes/default.rb:17: syntax error, unexpected keyword_end [ec2-user@Server recipes] ^C


#6

The # there converts the rest of that line on a comment, so the parentheses are not closed.

You must use: :site_name => sitename, [...]

The #{whatever} is only used inside strings. For example: "subdomain.#{sitename}" (within double quotes).

I recommend you to use a text editor with syntax highlighting to detect those errors easily.


#7

You should also look into taking some ruby primers, as chef is really just ruby. It’ll help with a lot of these syntax things.


#8

You should share all the necessary information, if you are expecting a precise answer. What is in that node attribute, that you are calling the ‘each’ method. what is the content of the template?

Without these, its gonna be a take more time for people to construct your scenario & respond.