1

I just started playing with chef today, and one thing I can't wrap my head around is how I can use data bags to store multiple key-values in one item (1 bag > 1 item > many key-values) and be able to invoke them one by one within a recipe.

Here is a simple test to delete directory if the directory from databag exists.

directory 'bag_item[installvar.testbag]['testdir'])' do
  action :delete
  only_if { Dir.exist?(bag_item[installvar.testbag]['testdir']) }
end

Obviously I'm not using the index right but I'm not strong in ruby at all, I didn't know what ruby was until couple of weeks ago... all the googling points me at examples of mass usage on multiple items by one key-value, which is not what I am trying to achieve. databag is installvar itemID is testbag and the key I'm looking to retrieve is testdir.

phs
  • 10,687
  • 4
  • 58
  • 84
BubbleWrap
  • 11
  • 3

2 Answers2

0

Check out https://docs.chef.io/ruby.html for some very basic Ruby introductions. Specifically look at the Hash section, as the item returned by the data_bag_item() method call acts like a Hash in most ways. Each bag item is a JSON-compatible hash at heart.

coderanger
  • 52,400
  • 4
  • 52
  • 75
0

so the solution is simple:

var = data_bag_item('installvar', 'testbag')
  dir = var['testdir']

directory "#{dir}" do
  action :delete
  only_if  Dir.exist?("#{['dir']}")
end

this works.

BubbleWrap
  • 11
  • 3