大网数据广告活动
服务器运维问答

Exists dir on puppet

· 52CE 我爱测

I want to check "if dir exists" condition with puppet how it done here How to do a file/dir exists conditional in Puppet? but then if i write exec { 'check_presence': command => "true", path => ["/usr/bin","/usr/sbin", "/bin"], o…

问题原文

原帖链接 · user423253 · CC BY-SA 3.0

I want to check "if dir exists" condition with puppet how it done here How to do a file/dir exists conditional in Puppet? but then if i write

  exec { 'check_presence':
    command => "true",
    path    =>  ["/usr/bin","/usr/sbin", "/bin"],
    onlyif  => "test -d /test"
  }
  file { '/test/123':
    ensure  => directory,
    mode    => '0644',
    require => Exec['check_presence']
  }

from exec i get alwyas true, and puppet always try to create /test/123

Debug: Exec[check_presence](provider=posix): Executing check 'test -e /test'
Debug: Executing 'test -e /test'

Error: Cannot create /test/123; parent directory /test does not exist
Error: /Stage[main]/tst::test/File[/test]/ensure: change from absent to directory failed: Cannot create /test/123; parent directory /test does not exist

Why he do it?! And how i must check parent directory exists?!

My puppet is 3.8

UPD I don't want to create /test ! I want to create /test/123 ONLY if /test already exists!

已采纳解答

原帖链接 · c4f4t0r · CC BY-SA 3.0

puppet doesn't have one native function for this, the only workaround:

mkdir -p your_module_path/lib/puppet/parser/functions/ now you need to create this file directory_exists.rb with this code:

require 'puppet'

module Puppet::Parser::Functions
  newfunction(:directory_exists, :type => :rvalue) do |args|
    if File.directory?(args[0])
      return true
    else
      return false
    end
  end
end

Now in your puppet code, you can use the function:

if directory_exists('/test') {
  file { '/test/123':
    ensure  => directory,
    mode    => '0644',
    owner   => 'root',
    group   => 'root',
  }
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-07-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。