Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using File::read in a provider's default.rb in Chef

I am trying to create an LWRP that will call the resource that is defined within itself. My cookbook's structure is as follows:

enter image description here

In the machine cookbook's provider, I have a code snippet as follows:

require 'chef/provisioning'  # driver for creating machines
require '::File'

    def get_environment_json
      @@environment_template = JSON.parse(File::read(new_resource.template_path + "environment.json"))
      return @@environment_template
    end

The code is only trying to read a json file and I am using File::read for it.

I keep getting an error as follows:

LoadError

cannot load such file -- ::File

Does anyone know how I can use File::read inside my LWRP's provider?

like image 310
streetsoldier Avatar asked Apr 30 '15 17:04

streetsoldier


1 Answers

OK, so the prior two answers are both half right. You have two problems. First, you can't require ::File as it's already part of Ruby. This is the cause of your error.

Second, if you call File.read you will grab Chef's File not ruby's. You need to do a ::File.read to use Ruby's File class.

like image 55
Tejay Cardon Avatar answered Nov 15 '22 02:11

Tejay Cardon