symbolic link not working with chef recipe

791 views Asked by At

I want to create symbolic link only if its not present

I have written below recipe

link '/home/user1/veera' do
    to '/usr/local/veera-10.0'
    only_if {test -L /home/user1/veera}
end

I tried below condition in recipe

only_if {test -L /home/user1/veera}

But it updates the symlink next time when chef runs next time(if it its changed).But i don't want that.

I want to skip this if symlink exist and create only when symlink it doesn't exists.

Also tried this

link '/home/user1/veera' do
    to '/usr/local/veera-10.0'
    not_if { File.symlink?('/home/user1/veera') }
end

But its still updating the symlink when changed on next chef run

1

There are 1 answers

9
seshadri_c On BEST ANSWER

The link resource is idempotent in nature, and shouldn't change if the link is already present.

However, for some reason if you want to skip the step, we can use symlink? method from Ruby's File class. This method returns true/false based on whether the target path is a link or not.

Like:

File.symlink?('/home/user1/veera')

Another potential issue that I noticed, and corrected in the below example is your source path - usr/local/veera-10.0 will most likely be relative to your current working directory. To be sure, use /usr/local/veera-10.0.

So in recipe:

link '/home/user1/veera' do
  to '/usr/local/veera-10.0'
  not_if { File.symlink?('/home/user1/veera') }
end

Update

As @lamont suggested in his comment, try using the File.exist? test as you want to skip the step if this path exists (whether its a file, dir, or symlink).

not_if { File.exist?('/home/user1/veera') }