Chef Nested Databag

685 Views Asked by At

I'm new to Chef and I'm having some problems to get the values from data_bags with nested attributes.

{   
  "id": "bareos-fd",   
  "description": "Client resource of the Director itself.",   
  "address": "localhost",   
  "job": {
    "backup-bareos-fd": {
      "jobdefs": "DefaultJob"
    },
    "BackupCatalog": {
      "description": "Backup the catalog database (after the nightly save)",
        "jobdefs": "DefaultJob",
        "level": "Full",
        "fileset": "Catalog",
        "schedule": "WeeklyCycleAfterBackup",
        "run_before": "/usr/lib/bareos/scripts/make_catalog_backup.pl MyCatalog",
        "run_after": "/usr/lib/bareos/scripts/delete_catalog_backup",
        "bootstrap": "|/usr/bin/bsmtp -h localhost -f \\\"\\(Bareos\\) \\\" -s \\\"Bootstrap for Job %j\\\" root@localhost",
        "priority": "11"
     },
     "RestoreFiles": {
       "type": "Restore",
       "fileset": "LinuxAll",
       "storage": "File",
       "pool": "Incremental",
       "messages": "Standard",
       "where": "/tmp/bareos-restores"
     }   
   } 
 }

How can I write a foreach to get the nested values (like BackupCatalog and its values?)

1

There are 1 best solutions below

2
coderanger On

The object returned from data_bag_item works like a hash:

bag = data_bag_item('something', 'bareos-fd')
bag['job']['BackupCatalog'].each do |key, value|
  # ...
end