How to pass variable between Chef resource blocks of same recipe

We have a Chef recipe with a couple resource blocks. The first resource block is in bash and gets the value of the UUID of a logical volume and stores into variable $uuid.

# Get UUID value
bash 'get uuid' do
  cwd "/"
  code <<-EOH
    uuid=$(blkid -o value -s UUID /dev/vg_volgroup/lv_logicalvolume)
  EOH
end

We need to pass the variable $uuid to our second resource block:

  # Mount directory, format, update fstab
  mount node['mount_dir'] do
    dump 1
    pass 2
    device #{uuid}
    device_type :uuid
    fstype node['fstype']
    options node['options']
    action [ :mount, :enable]
  end

Unfortunately, this is not working. The value of $uuid is not getting passed into the second resource block for device.

Is there a more proper way to reference $uuid from within the second resource block? Is what I’m asking even possible?

You could get the uuid from ohai. Try running “ohai filesystem2/by_device” from command line to see if you can get what you need. Then you could use something like node[‘filesystem2’][‘by_device’][‘YOUR_DEVICE’][‘uuid’] ohai attribute in your recipe.

Another option would be to set the uuid in ruby outside of your chef resource block, something like:

# Mount directory, format, update fstab
uuid = %x(/usr/sbin/blkid -o value -s UUID /dev/vg_volgroup/lv_logicalvolume)
mount node['mount_dir'] do
    dump 1
    pass 2
    device #{uuid}
    device_type :uuid
    fstype node['fstype']
    options node['options']
    action [ :mount, :enable]
end

Setting uuid in ruby like you do above is not working. Not able to get the device, or device does not exist. Maybe the syntax needs fixing?

The “ohai filesystem2/by_device” command does work, but trying to use the ohai attribute in my recipe is not working yet. I must not be constructing the attribute properly.

Actually, my ohai attribute path for the device was slightly off. I fixed it and now it works. Thanks much!

OK cool. For the other option, I forgot that ruby includes the newline, so I think this will work:

uuid = %x(/usr/sbin/blkid -o value -s UUID /dev/vg_volgroup/lv_logicalvolume).chomp

But I feel the ohai option is more the "chef way" in any case.

Glad to help.