title: Ruby and Chef: Resolve and Replace Values in a hash
body: <p>I am using opscode chef to automate the deployment of MySQL cluster. I want to put the desired hosts into a JSON file, then let chef resolve those hostnames to internal IP addresses, then set the IP addresses as variables.</p>
  
  <p>I have a simple hash that looks like this:</p>
  
  <pre><code>    [data_bag_item["dbclstr", "dbclstr", 
  {
  "id"=&gt;"dbclstr", 
  "nodes"=&gt;{"sql1"=&gt;"cdb1.ex.net", 
        "sql2"=&gt;"cdb2.ex.net", 
        "mgmnt"=&gt;"cdb1.ex.net", 
        "db1"=&gt;"cdb1.ex.net", 
        "db2"=&gt;"cdb2.ex.net"
  }}]] 
  </code></pre>
  
  <p>I want to basically grab the nodes key then go through all the key values in the hash grab each key/value then parse the value through my search function that returns the ip address then assign the value to that key.</p>
  
  <pre><code>dbclstr = search(:dbclstr).first # Loads json into hash
  
  privip = dbclstr["nodes"] # grabs node hash from hash (turns into a mash?)
  privip = privip.to_hash # turn mash to hash
  
  privip.map { |key,value| # maps the keys and values of the hash.
  
  item = search(:node,"name:value") #loads machine data from chef into object
  value = "#{item[0][:cloud][:private_ips]}" # extracts ip address from object and sets it as value, done?
  
  }
  </code></pre>
  
  <p>Well this doesn't work.</p>
  
  <p>Individually I can resolve hostnames to IP addresses, but I don't really understand how to grab each key and value, resolve the value, then replace it with the resolved value.</p>