Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm looking at the http://docs.opscode.com/chef/resources.html#execute and under "Execute" in "Execute a command using a template", they call template with parenthesis around the argument. Just a handful of statements earlier, they call execute sans parenthesis. For the rest of the document, template is called without parenthesis. Is there a reason for this inconsistency?

execute "forward_ipv4" do
  command "echo > /proc/.../ipv4/ip_forward"
  action :nothing
end

template( "/etc/file_name.conf" ) do
  source "routing/file_name.conf.erb"
  notifies :run, 'execute[forward_ipv4]', :delayed
end
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

I've never seen that syntax/style before until you pointed it out. It's completely unnecessary and confusing to use parentheses in that situation. Just specifying the template and the filename is sufficient.

share|improve this answer
    
Hmm, I guess I'll go bring this inconsistency in documentation up in their issues tracker then. Thanks! –  crazysim Jan 24 '13 at 18:47
    
And done: github.com/opscode/chef-docs/issues/26 –  crazysim Jan 24 '13 at 19:03
add comment

Chef recipes are written in Ruby, and the DSL uses #method_missing for defining resources in recipes.

Thus, each resource is a method, which takes a single argument (a string, the name), and a block (the parameters).

Parentheses are optional around the arguments in Ruby, and there is much written about whether to use them on the internet. While it doesn't matter whether you use them in Chef resources, most often they are omitted.

However, if you wish to write a resource on a single line, you'll need parentheses:

execute "echo Hello" { action :nothing }

SyntaxError: (irb#1):1: syntax error, unexpected '{', expecting $end
execute "echo Hello" { action :nothing }
                      ^

execute("echo Hello") { action :nothing }

 => <execute[echo Hello] @name: "echo Hello" @noop: nil @before: nil
 @params: {} @provider: nil @allowed_actions: [:nothing, :run]
 @action: [:nothing] @updated: false @updated_by_last_action: false
 @supports: {} @ignore_failure: false @retries: 0 @retry_delay: 2
 @source_line: "(irb#1):2:in `irb_binding'" @elapsed_time: 0
 @resource_name: :execute @command: "echo Hello" @backup: 5 @creates:
 nil @cwd: nil @environment: nil @group: nil @path: nil @returns: 0
 @timeout: nil @user: nil @umask: nil @cookbook_name: nil
 @recipe_name: nil>

(action nothing here is just to show an example)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.