我刚开始使用 Chef,对 ruby 不太了解。
我无法理解食谱中使用的语言语法。
比如说,我在食谱中的recipes/default.rb 中创建一个目录,如下所示:
directory "/home/test/mydir" do
owner "test"
mode "0755"
action :create
recursive true
end
我认为这是有效的 ruby 脚本的一部分。线条像什么owner "test"
意思是?这是函数调用、变量赋值还是其他什么?
Chef 是用 Ruby 编写的,并广泛使用 Ruby 的功能来设计自定义 DSL。几乎每个 Chef 配置文件都是使用基于 Ruby 的 DSL 编写的。
这意味着为了有效地使用 Chef,您应该熟悉 Ruby 语法的基础知识,包括
- Grammar
- 数据类型(与其他语言相比主要区别是符号)
- Blocks
您不需要了解很多关于 Ruby 中的元编程的知识。
您发布的代码案例是基于 Ruby 的 DSL 的一个很好的示例。让我解释一下。
# Call the method directory passing the path and a block
# containing some code to be evaluated
directory "/home/test/mydir" do
# chown the directory to the test user
owner "test"
# set the permissions to 0555
mode "0755"
# create the directory if it does not exists
action :create
# equivalent of -p flag in the mkdir
recursive true
end
块是指定要在单个上下文(在本例中为该路径的上下文)中评估的一组操作(在本例中为创建、设置权限等)的便捷方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)