Chef Solo - Ruby DSL或JSON中的角色数据?

Ter*_*ang 1 ruby chef-infra chef-recipe chef-solo

我和Roles一起玩Solo(11.4.4和11.6.0).有点困惑.

对于Chef Solo运行,角色应该用Ruby还是JSON编写?

根据官方文档:关于角色,角色可以存储为特定于域的Ruby(DSL)文件或JSON数据.

注意:chef-client使用Ruby for Roles,当这些文件上载到Chef Server时,它们将转换为JSON.每当刷新chef-repo时,所有特定于域的Ruby文件的内容都会重新编译为JSON并重新上载到服务器.

我的问题是,如果要求是在没有服务器且需要角色的情况下以独奏模式运行Chef,那么角色应该用Ruby还是JSON编写(我们没有服务器将Ruby转换为JSON)?

我的猜测是后者.有谁知道正确的答案?

BTW:我见过人们在角色文件中混合使用Ruby和JSON ......

下面的rbenv.rb的Ruby DSL等价物是什么?

例如,运行rbenv+ ruby-buildcookbook在Ubuntu上安装rbenv.

rbenv.json

{
  "run_list": ["role[rbenv]"]
}
Run Code Online (Sandbox Code Playgroud)

角色/ rbenv.rb

name "rbenv"
description "rbenv + ruby-build"
run_list(
  "recipe[rbenv]",
  "recipe[ruby_build]"
)
override_attributes(
  :rbenv => {
    :git_repository => "https://github.com/sstephenson/rbenv.git"
  },
  :ruby_build => {
    :git_repository => "https://github.com/sstephenson/ruby-build.git"
  }
)
Run Code Online (Sandbox Code Playgroud)

Chef Solo run chef-solo -c solo.rb -j rbenv.json -l debug按预期工作.这是通过HTTPS实现克隆,因为它更容易在防火墙后面.

但是,使用Ruby DSL版本的角色rbenv.rb如下所示

name "rbenv"
description "rbenv + ruby-build"
run_list "recipe[rbenv]", "recipe[ruby_build]"
# default_attributes ":rbenv" => {":install_prefix" => "/opt"}
override_attributes ":rbenv" => {":git_repository" => "https://github.com/sstephenson/rbenv.git"}, ":ruby_build" => {":git_repository" => "https://github.com/sstephenson/ruby-build.git"}
Run Code Online (Sandbox Code Playgroud)

它似乎没有用,因为它仍然使用默认属性(通过git URL而不是HTTPS克隆).

我是Ruby的新手,所以我很可能在DSL代码中犯了一些错误,请帮忙;-)

 * git[/opt/rbenv] action sync[2013-09-03T03:44:53+00:00] INFO: Processing git[/opt/rbenv] action sync (rbenv::default line 91)
[2013-09-03T03:44:53+00:00] DEBUG: git[/opt/rbenv] finding current git revision
[2013-09-03T03:44:53+00:00] DEBUG: git[/opt/rbenv] resolving remote reference

================================================================================
Error executing action `sync` on resource 'git[/opt/rbenv]'
================================================================================


Mixlib::ShellOut::ShellCommandFailed
------------------------------------
Expected process to exit with [0], but received '128'
---- Begin output of git ls-remote "git://github.com/sstephenson/rbenv.git" master* ----
STDOUT: 
STDERR: fatal: unable to connect to github.com:
github.com[0: 192.30.252.128]: errno=Connection timed out
---- End output of git ls-remote "git://github.com/sstephenson/rbenv.git" master* ----
Ran git ls-remote "git://github.com/sstephenson/rbenv.git" master* returned 128
Run Code Online (Sandbox Code Playgroud)

Tim*_*ter 6

我希望尽可能使用JSON格式,原因很简单 - 使用脚本很容易解析和验证.如果所有Chef数据都是JSON格式,您可以执行以下三项操作:

  1. 在git pre-commit钩子中轻松执行语法检查,这在文件是Ruby DSL格式时更难做到.
  2. 验证数据包条目中的键和值.这有助于检查您是否不会将无效或无意义的数据包条目部署到生产环境中.
  3. 比较(通过一些额外的工作 - 字典中的键排序需要考虑)服务器上的对象的值与git中的内容.这个--format json论点在这里很有用.