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

标签 ruby chef-infra chef-recipe chef-solo

我正在与 Chef Solo 一起玩角色(11.4.4 和 11.6.0)。有点困惑。

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

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

NOTE: chef-client uses Ruby for Roles, when these files are uploaded to Chef Server, they are converted to JSON. Whenever chef-repo is refreshed, the contents of all domain-specific Ruby files are re-compiled to JSON and re-uploaded to the server.

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

我猜是后者。有谁知道正确答案吗?

顺便说一句:我见过有人在角色文件中混合使用 Ruby 和 JSON...

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

例子,运行rbenv + ruby-build在 Ubuntu 上安装 rbenv 的 Recipe 。

rbenv.json

{
  "run_list": ["role[rbenv]"]
}

角色/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"
  }
)

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"}

它似乎没有用,因为它仍然使用默认属性(通过 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

最佳答案

出于一个简单的原因,我更喜欢尽可能使用 JSON 格式 - 使用脚本很容易解析和验证。如果您的所有 Chef 数据都是 JSON 格式,您可以执行以下三项操作:

  1. 在 git 预提交 Hook 中轻松执行语法检查,这在文件为 Ruby DSL 格式时更难执行。
  2. 验证数据包条目中的键和值。这对于检查您是否打算将无效或无意义的数据包条目部署到生产中很有用。
  3. 比较(需要一些额外的工作——需要考虑字典中的键顺序)服务器上对象的值与 git 中的值。 --format json 参数在这里很有用。

关于ruby - Chef Solo - Ruby DSL 或 JSON 中的角色数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18583910/

相关文章:

chef-infra - 如何在 Chef Recipe 中自动执行用户交互命令

ruby - 使用 Knife 更新 Chef 中的运行列表

ruby - 如何使用 Chef Recipe 删除文件中的一行?

bash - wget 手动工作但在 Chef bash 中失败

ruby-on-rails - 诺科切替换

ruby - 如何在 ruby​​ 中对字母数字数组进行排序

Ruby 的 "foo = true if !defined? foo"不会按预期工作

ruby - 在 ruby​​ 中创建哈希数组

ubuntu - 通过 Chef solo 和 Vagrant 安装 apt 包

chef-infra - 在包含的 LWRP 配方中定义的通知服务