I am somewhat new to chef and i am trying to run a splunk cookbook and i am getting the below error any ideas thanks

And i am getting the below error: Anyone any idea Thanks in advance.

ecipe Compile Error in /var/chef/cache/cookbooks/chef-splunk/recipes/default.rb

NoMethodError

undefined method `[]’ for nil:NilClass

Cookbook Trace:

/var/chef/cache/cookbooks/chef-splunk/recipes/default.rb:20:in `from_file’

Relevant File Content:

/var/chef/cache/cookbooks/chef-splunk/recipes/default.rb:

13: # Unless required by applicable law or agreed to in writing, software
14: # distributed under the License is distributed on an “AS IS” BASIS,
15: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: # See the License for the specific language governing permissions and
17: # limitations under the License.
18: #
19:
20>> if node[‘splunk’][‘disabled’]
21: include_recipe ‘chef-splunk::disabled’
22: Chef::Log.debug(‘Splunk is disabled on this node.’)
23: return
24: end
25:
26: if node[‘splunk’][‘is_server’]
27: include_recipe ‘chef-splunk::server’
28: else
29: include_recipe ‘chef-splunk::client’

System Info:

chef_version=12.22.1
platform=ubuntu
platform_version=14.04
ruby=ruby 2.3.6p384 (2017-12-14 revision 61254) [x86_64-linux]
program_name=chef-client worker: ppid=2172;start=04:20:59;
executable=/opt/chef/bin/chef-client

Hi @Celtic67

I have checked the chef-splunk cookbook and it seems to be missing an initial value for the node[‘splunk’][‘disabled’] node object.
You can fix this by defining it in a environment json or by modifying the cookbook attribute file.

Thanks Mark, i’ve defined: the below statement in the default.rb in the recipe folder and still did not work, is that you mean on first response appreciate any help.

include_recipe ‘chef-splunk::server’
include_recipe ‘chef-splunk::client’

Are you running the code in chef-server <-> chef-client style or executing it in locally with chef-solo?
Either way you could create a small json file ./my_environment.json with the contents of

{
  "splunk": {
     "disabled": false
   }
}

and pass in a --json-attributes ./my_environment.json to the executable. This way you would have node[‘splunk’][‘disabled’] defined without touching and modifying the original code.

Thanks Mark appreciate that, this what i did: i’ve created a jason file: and placed it in the environment folder with the content below:
{
“splunk”: {
“disabled”: false
}
}
and modified the default.rb with the code below: Everything got uploaded to the chef-server however when i try to upload the sample.json i will i get the following error: ERROR: . failed to create_child: Failure creating ‘sample.json’: 400 “Bad Request” cause: {“error”:[“Invalid key splunk in request body”]} any ideas?

Thank you

require ‘json’

file = File.read(‘sample.json’)
data_hash=JSON.parse(file)
#data_hash.keys
data_hash[‘splunk’]

if node[‘splunk’][‘disabled’]
include_recipe ‘chef-splunk::disabled’
Chef::Log.debug(‘Splunk is disabled on this node.’)
return
end

if node[‘splunk’][‘is_server’]
include_recipe ‘chef-splunk::server’
else
include_recipe ‘chef-splunk::client’
endrequire ‘json’

file = File.read(‘sample.json’)
data_hash=JSON.parse(file)
#data_hash.keys
data_hash[‘splunk’]

if node[‘splunk’][‘disabled’]
include_recipe ‘chef-splunk::disabled’
Chef::Log.debug(‘Splunk is disabled on this node.’)
return
end

if node[‘splunk’][‘is_server’]
include_recipe ‘chef-splunk::server’
else
include_recipe ‘chef-splunk::client’
end

You need to define environment related keys also

{
  "name": "name_of_environment",
  "description": "description of environment",
  "json_class": "Chef::Environment",
  "chef_type": "environment",
  "default_attributes": {
    "splunk": {
      "disabled": false
    }
  }
}

Then you can run
knife environment from file path_to_your.json