Hi all,
I am new to Chef and I can't find a solution to the issue at hand; please read the following code:
bash 'blk-nofs' do
user 'root'
code <<-EOH
for block in $(lsblk -o NAME -dn)
do
validation=$(blkid /dev/$"{block}")
if [[ -z "${validation}" ]]
then
db_disk="${block}"
else
:
fi
done
EOH
end
lvm_physical_volume '/dev/"${db_disk}"'
But the value of db_disk does not get exported to be used by another resource.
Is there a way to do so?
Thank you for your time.
Giuseppe
By default, Chef has no way of knowing that you've set a Bash variable - which will go away once its subprocess has finished.
What you'll want to do instead is to capture the script's output to a ruby variable, using Chef::Mixin::ShellOut. There's some examples here:
Once you have your value in a ruby variable, you can call it as part of a resource block.
So it might look something like this:
db_disk = ''
ruby_block "get_db_disk" do
block do
Chef::Resource::RubyBlock.send(:include,Chef::Mixin::ShellOut)
db_disk = shell_out("your code here")
end
end
lvm_physical_volume '/dev/'+db_disk do
# pv allocation here
end
Hi Pahanda,
Thank you for your reply.
This is now:
db_disk = ''
ruby_block "get_db_disk" do
block do
Chef::Resource::RubyBlock.send(:include,Chef::Mixin::ShellOut)
db_disk = shell_out(
"for block in $(lsblk -o NAME -dn)
do
validation=$(sudo blkid /dev/$block)
if [[ -z $validation ]]
then
db_disk=$block
else
:
fi
done"
)
end
end
lvm_physical_volume '/dev/'+db_disk
But I get this error on run because the passed variable's value is empty:
[2019-07-05T19:23:08+00:00] FATAL: LVM::External::ExternalFailure: lvm_physical_volume[/dev/] (postgres::psql-11-dbfiles line 43) had an error: LVM::External::ExternalFailure: Fatal error,/sbin/lvm pvcreate /dev/ returned 5 with 'Device /dev/ not found (or ignored by filtering).'
That tells me that the output from the bash script didn't actually get stored to the variable, so it was empty - leaving the argument to lvm_physical_volume invalid.
I'd recommend putting some debugging in; I don't know how well the shell_out command handles line breaks like that (you might need semicolons in your bash if/else block).
db_disk = ''
ruby_block "get_db_disk" do
block do
Chef::Resource::RubyBlock.send(:include,Chef::Mixin::ShellOut)
db_disk = shell_out("for block in $(lsblk -o NAME -dn); do validation=$(sudo blkid /dev/$block); if [[ -z $validation ]]; then echo $block; fi; done")
end
end
lvm_physical_volume '/dev/'+db_disk
and I have tested the command included inside the shell_out block but it still fails to pass the value of the variable outside the block.
db_disk = ''
ruby_block "get_db_disk" do
block do
Chef::Resource::RubyBlock.send(:include,Chef::Mixin::ShellOut)
db_disk = shell_out!("for block in $(lsblk -o NAME -dn); do if [ -z $(sudo blkid /dev/$block) ]; then echo $block; fi; done").stdout
puts " disk is #{db_disk}"
puts $?.success?
end
end
I get the disk fine:
* ruby_block[get_db_disk] action run disk is sdb
true
- execute the ruby block get_db_disk
But still:
[2019-07-08T12:08:09+00:00] FATAL: LVM::External::ExternalFailure: lvm_physical_volume[/dev/] (postgres::psql-11-dbfiles line 18) had an error: LVM::External::ExternalFailure: Fatal error,/sbin/lvm pvcreate /dev/ returned 5 with 'Device /dev/ not found (or ignored by filtering).'