Skip to content

Instantly share code, notes, and snippets.

@dairennlombard
Last active August 29, 2015 14:22
Show Gist options
  • Save dairennlombard/9820a82f45c734160983 to your computer and use it in GitHub Desktop.
Save dairennlombard/9820a82f45c734160983 to your computer and use it in GitHub Desktop.
Ruby vars as an array element name?
# data_bag JSON:
{
"id": "example_data_bag_id",
"example_account": {
"private_value": "Example Private Data",
"public_value": "Example Public Data"
}
}
# Recipe:
# node['example_cookbook']['databagname'] = 'example_account'
databagid = node['example_cookbook']['databagname']
# At this point, #{databagid} should = "example_account"
chefkey = Chef::EncryptedDataBagItem.load_secret(node['example_cookbook']['secretpath'])
databagarray = Chef::EncryptedDataBagItem.load(databagid, 'example_data_bag_id', chefkey)
# Should be like passing ('example_account', 'example_data_bag_id', chefkey)
privatevalue = databagarray["#{databagid}"]['private_value']
publicvalue = databagarray["#{databagid}"]['public_value']
# Is it possible to use a Ruby var as an array element name in this way?
@pauzed
Copy link

pauzed commented Jun 4, 2015

data_bag JSON:

Databag name: example_data_bag

{
"id": "example_data_bag_item",
"example_account": {
"private_value": "Example Private Data",
"public_value": "Example Public Data"
}
}

Recipe:

chefkey = Chef::EncryptedDataBagItem.load_secret(node['example_cookbook']['secretpath'])
databagarray = Chef::EncryptedDataBagItem.load('example_data_bag', 'example_data_bag_item', chefkey)
privatevalue = databagarray["example_account"]['private_value']
publicvalue = databagarray["example_account"]['public_value']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment