Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't embed bash command in to Chef Recipe

I'm attempting to embed a shell command in to my Chef Recipe, however when Chef executes the command it seems to get things wrong. Here's the resource in question:

script "create libs symlink" do
  interpreter "bash"
  user "root"
  cwd "/home/robin/test"
  code <<-EOH
ln -s $(ls -1 | grep '^[0-9.-]\+$') curr-version-libs
  EOH
end

The /home/robin/test directory contains a folder called 19.26-3, so I'm expecting a symlink called curr-version-libs pointing at 19.26-3.

Instead, I'm ending up with a circular symlink:

drwxr-xr-x 4 root root  4096 Jan 17 22:35 19.26-3
drwxr-xr-x 2 root root  4096 Jan 17 22:35 config
lrwxrwxrwx 1 root root    17 Jan 28 17:31 curr-version-libs -> curr-version-libs

It seems that the $(ls -1 | grep '^[0-9.-]+$') is being removed and I'm ending up with the command ln -s curr-version-libs.

Does anyone know what's going on here? I've tried using an execute resource, but I get the same results.

like image 322
Robin Avatar asked Mar 17 '26 17:03

Robin


1 Answers

If your 19.26-3 directory exists before chef run starts, then it is easy. If you are creating a symbolic link, I would recommend using link resource for that.

version = `ls /home/robin/test/ -1 | grep '^[0-9.-]+$'`.strip

link "/home/robin/test/curr-version-libs" do
  to ::File.join( "/home/robin/test", version )
end

But if it is not there, I would recommend using ruby_block and defining your link resource dynamically.

ruby_block "create libs symlink" do
  block do
    version = `ls /home/robin/test/ -1 | grep '^[0-9.-]+$'`.strip
    res = Chef::Resource::Link.new( "/home/robin/test/curr-version-libs", run_context )
    res.to ::File.join( "/home/robin/test", version )
    res.run_action :create
  end
end

Edit: I corrected the answer by fixing regex and and calling strip before assigning to version as Robin proposed.

like image 130
Draco Ater Avatar answered Mar 20 '26 06:03

Draco Ater



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!