在厨师资源文档中,为什么他们在执行例子中的模板资源名称周围有括号?

我正在查看http://docs.opscode.com/chef/resources.html#execute并在“使用模板执行命令”中的“执行”下面,他们使用围绕参数的括号调用模板。 之前只有less数几个声明,他们称之为执行sans括号。 对于文档的其余部分,模板被调用时没有括号。 是否有这种不一致的原因?

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 

我从来没有见过这种语法/风格,直到你指出。 在这种情况下使用括号是完全没有必要和困惑的。 只要指定模板和文件名就足够了。

厨师食谱是用Ruby编写的,DSL使用#method_missing来定义食谱中的资源。

因此,每个资源是一个方法,它采用一个参数(一个string,名称)和一个块(参数)。

在Ruby中,括号是可选的,关于是否在互联网上使用它们有很多。 在厨师资源中使用它们并不重要,通常它们被省略。

但是,如果您希望在单行上编写资源,则需要使用括号:

 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> 

(这里没有任何行动只是为了举例)