Error while loading Chef data bag item, created via Chef code

688 Views Asked by At

I've two questions associated with the issue. (Resolved issue, solution provided below)

I'm having a data bag named, java. It contains the data bag item specific to each node's hostname, as shown below

id:          node_hostname
java_config:
  jdk_version: 7

I'm able to create the data bag using Chef code

  dbag_hash = Hash[ "id" => node['hostname'], "java_config" => [ "jdk_version" => "7" ] ]

  databag_item = Chef::DataBagItem.new
  databag_item.data_bag("java")
  databag_item.raw_data = dbag_hash
  databag_item.save

  userDataBag = data_bag_item('java', node['hostname'])
  jdk_version = userDataBag['jdk_version']['java_config']

But while executing the recipe, i'm getting below error in the code, where it is assigning value to jdk_version

>>       jdk_version = userDataBag['java_config']['jdk_version']
TypeError
---------
can't convert String into Integer

Question 1: How to fetch the value, from the above data bag item format?

If I create the data bag as below, I'm able to fetch jdk_version

id:          node_hostname
jdk_version: 7

Code to create data bag in above format

  dbag_hash = Hash[ "id" => node['hostname'], "jdk_version" => "7" ]

  databag_item = Chef::DataBagItem.new
  databag_item.data_bag("java")
  databag_item.raw_data = dbag_hash
  databag_item.save

  userDataBag = data_bag_item('java', node['hostname'])
  jdk_version = userDataBag['jdk_version']

Question 2: what am I doing wrong in the previous data bag item implementation?

Issue Resolved:: I'm now able to create data bag item in the format mentioned below, able to fetch data successfully

id:          node_hostname
java_config:
  jdk_version: 7

Added "{}" braces instead of "[]" for jdk_version and its value, while creating the data bag item hash.

Working code is provided below,

  dbag_hash = Hash[ "id" => node['hostname'], "java_config" => { "jdk_version" => "7" } ]

  databag_item = Chef::DataBagItem.new
  databag_item.data_bag("java")
  databag_item.raw_data = dbag_hash
  databag_item.save

  userDataBag = data_bag_item('java', node['hostname'])
  jdk_version = userDataBag['java_config']['jdk_version']
1

There are 1 best solutions below

0
On BEST ANSWER

I've added the resolution in the question itself. But for the benefit of readers, I'm adding it in the answer section.

I was able to create data bag item in the format mentioned below and able to fetch data successfully

id: node_hostname
java_config:
  jdk_version: 7

Working code is provided below,

dbag_hash = Hash[ "id" => node['hostname'], "java_config" => { "jdk_version" => "7" } ]

databag_item = Chef::DataBagItem.new
databag_item.data_bag("java")
databag_item.raw_data = dbag_hash
databag_item.save

userDataBag = data_bag_item('java', node['hostname'])
jdk_version = userDataBag['java_config']['jdk_version'] 

Added curly braces "{}" instead of square braces "[]" for jdk_version and its value, while creating the data bag item hash.