How to check if the directory is symlink in chef
Asked Answered
G

2

8

I just want to do delete directory if it is not symlnik.

directory "/var/www/html/" do
  action :delete
  only_if ???
end
Gumbotil answered 6/4, 2014 at 9:40 Comment(0)
T
14

The selected answer will not work on Windows or systems where Bash is the default interpreter. You should use a Ruby solution to be cross-platform (and faster, since there's no process spawning):

directory '/var/www/html' do
  action :delete
  not_if { File.symlink?('/var/www/html') }
end
Tetrabrach answered 6/4, 2014 at 18:18 Comment(1)
Thank you! It sounds good. I changed the recipe and it works nice! Cookbook needs to be cross-platform. I changed the answer selection to yours.Gumbotil
C
1

How about:

directory "/var/www/html/" do
    action :delete
    not_if "test -L /var/www/html/"
end

test -L $file returns 0 (true) if $file is a symlink.

Carl answered 6/4, 2014 at 11:29 Comment(3)
Thanks! I solved the problem with your solution. But I eliminate last slash of paths.Gumbotil
I would go for a ruby-based solution in the not_if block. See here.Hiatus
Thank you. ruby-based solution is better.Gumbotil

© 2022 - 2024 — McMap. All rights reserved.