1

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

AWS_Lernar
  • 627
  • 2
  • 9
  • 26

1 Answers1

1

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') }
seshadri_c
  • 6,906
  • 2
  • 10
  • 24
  • I tried this but it updates the symlink next time it runs (if symlink was modified by user).My requirement is not to update symlink if it exists when next time chef runs.It should run this inly when symlink doesnot exists – AWS_Lernar Sep 02 '21 at 10:56
  • In my test, it is skipping the task. Even `test -L /home/user1/veera` can be used but the guard should be `not_if`. Also, in your example, you have `usr/local/...`, which is not exactly `/usr/local/...` – seshadri_c Sep 02 '21 at 10:58
  • if i use ```not_if test -L /home/user1/veera ``` it doesnt create symlink if it doesn't exists – AWS_Lernar Sep 02 '21 at 11:06
  • Check by correcting your source path to `/usr/...` and using `File.symlink?` – seshadri_c Sep 02 '21 at 11:07
  • I did typo in my question i am giving correct ```/usr/local/veera-10.0``` – AWS_Lernar Sep 02 '21 at 11:27
  • Can you update the question with what is changing in the source path? Or I suspect that `/home/user1/veera` is changing from link to directory or file. – seshadri_c Sep 02 '21 at 11:50
  • I updated question. Also i verified using ```ls -l``` its not changed to file or directory its l in beginning which means its symlink – AWS_Lernar Sep 02 '21 at 12:01
  • 1
    Do you want `not_if { File.exist?('/home/user1/veera') }`? – lamont Sep 03 '21 at 19:37
  • That's a good point, though I have a feeling that OP is trying to link directories. – seshadri_c Sep 04 '21 at 03:59