Keith
Keith

Reputation: 2019

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.

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

Upvotes: 0

Views: 511

Answers (1)

Szymon
Szymon

Reputation: 1525

UUID is part of filesystem2 Ohai data:

filesystem2:
  by_device:
    /dev/md1:
      ...
      uuid:                f49a3dc8-a0b6-4e1c-8cd3-926fa7d8ee29

There is no need to run blkid for this.

However, if you really need to do compute something in a block and use it later, you could declare uuid variable before the block and use ruby_block instead. You can also use node variable inside a ruby block. Anyway, you will be affected by Chef's two pass model and it would require further workarounds (like lazy attributes).

There is also a option to use helper method, but since UUID is part of Ohai data, I do not see any reason to even try (in this case).

Upvotes: 0

Related Questions