Question

How can I convert files_copy_to_guest into a Ruby Hash?

.kitchen.yml

  'my_cookbook': 
      'files_copy_to_guest':
       - 
          'home/kevin/bin/script.sh'      : '/vagrant/unix_scripts/script.sh'
          'home/kevin/script2.sh'       : '/vagrant/unix_scripts/script2.sh'

cookbooks/my_cookbook/attributes/default.rb

default['kevin']['files_copy_to_guest']     = [] 

cookbooks/my_cookbook/recipes/default.rb

files = node['kevin']['files_copy_to_guest']   # how to read attribute as Hash?

if files.nil? | files.empty? then
    Chef::Log.warn("node['kevin']['files_copy_to_guest'] is nil or empty!")
end

files.each do |_, value|                
    value.each do |vm_dest, host_src|   
        file vm_dest do 
            owner user
            group user
            mode 0755
            content ::File.open(host_src).read # <<< error on 'value'
        end
    end 
end

I tried:

files = YAML.load(node['kevin']['files_copy_to_guest'],

but that didn't work. I also read that files.to_hash will not work either.

Was it helpful?

Solution

'my_cookbook': 
  'files_copy_to_guest':
   - 
    'home/kevin/bin/script.sh'      : '/vagrant/unix_scripts/script.sh'
    'home/kevin/script2.sh'       : '/vagrant/unix_scripts/script2.sh'

It looks to me like the problem is with your YAML. You have a - line which denotes the beginning of an array, so your object is coming out looking like this:

{
  'my_cookbook' => {
    'files_copy_to_guest' => [
      {
        'home/kevin/bin/script.sh' => '/vagrant/unix_scripts/script.sh',
        'home/kevin/script2.sh' => '/vagrant/unix_scripts/script2.sh'
      }
    ]
  }
}

I think if you ditch the - it should work. You can test the output of your YAML with a tool like this: http://yaml-online-parser.appspot.com/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top