Unable to insert bash command in chef recipe
I am trying to embed a shell command in my chef recipe, however, when Chef executes the command, everything seems to be 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 directory /home/robin/test
contains a folder named 19.26-3, so I am expecting a symbolic link with the name curr-version-libs
pointing to 19.26-3
.
Instead, I get a circular symbolic link:
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 $ (ls -1 | grep '^ [0-9.-] + $') is removed and I end up with a command ln -s curr-version-libs
.
Does anyone know what's going on here? I tried to use the resource execute
but get the same results.
source to share
If your 19.26-3 directory exists prior to the chef launch launch, then it's easy. If you are creating a symbolic link, I would recommend using the link resource for this .
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's not there, I would recommend using ruby_block and dynamically defining the link resource.
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 setting a regex and and calling the stripe before assigning the version Robin suggested.
source to share
You seem to be calling the wrapper to create a sim link. In this case, a much better way to do it is to use the chef's link resource . I would never use a script or execute resource to do what you are doing.
Using a link resource you should do the following:
link "/home/robin/test/curr-version-libs" do
to '/home/robin/test/19.26-3'
user 'root'
group 'root'
link_type :symbolic
action :create
end
Quick side comment: I am a mentor and trained a few people to come up with a chef. Those who come to understand what resources, providers, and lightweight resources (like LWRP) offer are much happier and more efficient than those who are just trying to dump their old shell scripts into their cookbooks.
I highly recommend reading Resources and Vendors and Lightweight Resources Documentation
source to share