Puppet 仅当文件存在于特定目录中时才有条件

问题描述 投票:0回答:3

我的木偶清单中有以下文件资源:

file{
  'example.dat'
  path   => "/path/to/example.dat",
  owner  => devops,
  mode   => "0644",
  source => "/path/to/example.txt" 
}

我想仅当

.txt
文件存在于特定目录中时才运行上面的代码片段。否则,我不想运行该代码片段。

我该如何在木偶中做到这一点?

automation puppet devops
3个回答
1
投票

一般来说,您将创建一个“自定义事实”,当相关目录满足您的条件时,该事实返回 true。例如: Facter.add('contains_txt') do setcode do ! Dir.glob("/path/to/dir/*.txt").empty? end end

然后你会写:

if $facts['contains_txt'] { file { 'example.dat': path => "/path/to/example.dat", owner => devops, mode => "0644", source => "/path/to/example.txt", } }



0
投票
find_file

:

if find_file('path-to-your-file') {
  # do something here if your file exists
}

文档:
https://www.puppet.com/docs/puppet/5.5/function.html


0
投票
find_file()

在 Puppet 服务器上运行,而不是在代理上运行。

    

© www.soinside.com 2019 - 2024. All rights reserved.