How to check if a directory exists in puppet

隐身守侯 提交于 2019-12-11 11:38:38

问题


Trying to create an autostart directory on a rpi using puppet. It is supposed to mkdir only if the location doesn't exist.

Here is the current code:

exec { "mkdir_autostart":
command => "mkdir /home/pi/.config/autostart",
unless => "[ -d /home/pi/.config/autostart ]",
path    => "/usr/local/bin/:/bin/",
}

Here is what I get:

err: Failed to apply catalog: Parameter unless failed on Exec[mkdir_autostart]: 
'[ -d  /home/pi/.config/autostart ]' is not qualified and no path was specified.
Please qualify the command or specify a path.

Also tried with onlyif statement, but that generated the same error. What am I doing wrong?

EDIT:

Added path (path => "/usr/local/bin/:/bin/",) and now get:

err: /Stage[main]/auto::Sign/Exec[mkdir_autostart]: Could not evaluate: Could not find command '['

回答1:


You should use the "file" type:

file { "/home/pi/.config/autostart":
   ensure => directory
}

But if for any reason, you want to keep your "exec" type, use test -d /home/pi/.config/autostart




回答2:


Instead of using unless I'd suggest just adding a -p flag to mkdir:

exec { "mkdir_autostart":
  command => "mkdir -p /home/pi/.config/autostart",
  path    => "/usr/local/bin/:/bin/"
}

Or better yet just use Puppet's file resource type as mentioned above by @sebastien.prudhomme.



来源:https://stackoverflow.com/questions/20108694/how-to-check-if-a-directory-exists-in-puppet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!