Pergunta

I am playing with Roles with Chef Solo (11.4.4 and 11.6.0). A bit Confused.

For Chef Solo runs, should roles be written in Ruby or JSON?

As per the official docs: About Roles, Roles can be stored as domain-specific Ruby (DSL) files or JSON data.

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.

My question is, if the requirement is to run Chef in solo mode without a server and roles are needed, should the roles be written in Ruby or JSON (we don't have a server to convert Ruby to JSON)?

My guess is the latter. Does anyone know the correct answer?

BTW: I've seen people mixing Ruby and JSON in role files...

What is the Ruby DSL equivalent for rbenv.rb below?

Example, run rbenv + ruby-build cookbooks to install rbenv on Ubuntu.

rbenv.json

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

roles/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 works as expected. This is to achieve cloning via HTTPS because it easier behind the firewall.

However, using a Ruby DSL version of role rbenv.rb like below

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

It didn't seem to work because it still used the default attributes (clone via git URL instead of HTTPS).

I am new to Ruby so most likely I made some mistakes in the DSL code, please help;-)

 * 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
Foi útil?

Solução

I prefer to use JSON format wherever possible for one simple reason - it's easy to parse and validate with a script. Here are three things that you can do if all your Chef data is in JSON format:

  1. Easily perform a syntax check in a git pre-commit hook, something that's much harder to do when the file is in the Ruby DSL format.
  2. Validate the keys and values in a data bag entry. This can be useful to check that you are not going to deploy invalid or nonsensical data bag entries to production.
  3. Compare (with a little extra work - key ordering in a dictionary needs to be taken into account) the value of an object on a server with what's in git. The --format json argument is useful here.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top