在puppet中创build文件之前检查目录?

我试图做一个函数哪个目录/文件将被创build只有当第一个目录存在,而不是它必须被跳过,因为失败的依赖。

我试过这个“唯一”的解决方法 ,但不幸的是,它不适用于我的function。

$check_directory = file("/path/to/directory") if($check_directory != '') { file{"/path/to/config": ensure => directory, mode => 0755, } file{"/path/to/config/a.conf": ensure => file, mode => 0755, content => template("config_template.conf"), } } 

我有一个错误:

 Error: Is a directory - /path/to/directory 

有没有办法做任何其他的陈述? 或者任何参数? 谢谢。

您应该能够简单地在您的a.conf文件资源中使用require语句:

 file{"/path/to/directory": ensure => directory, mode => 0755, } file{"/path/to/config": ensure => directory, mode => 0755, } file{"/path/to/config/a.conf": ensure => file, mode => 0755, content => template("config_template.conf"), require => File["/path/to/directory"], } 

这将确保该目录将在文件之前创build。

@ Sven的答案的精神是绝对正确的:你在Puppet中使用beforerequirenotify等设置资源依赖关系。唯一会改变的是我将使用defined()函数来testing:

 file { '/tmp/foo': ensure => 'directory', mode => '0755', } if defined(File['/tmp/foo']) { notice("/tmp/foo is defined! making /tmp/bar/baz") file { '/tmp/bar': ensure => 'directory', mode => '0755', } file { '/tmp/bar/baz': ensure => 'present', mode => '0755', require => File['/tmp/bar'], } } 

其他两个有用的花絮:

  • 如果你在Puppet 3.2+中启用“Future Parser” ,你可以像这样访问资源属性: $a = File['/tmp/foo']['ensure'] 。 这会将/tmp/foo文件资源的ensure属性的值存储在variables$a 。 如果你决定研究这个,我会重写上面的例子, if defined(File['/tmp/foo']) and File['/tmp/foo']['ensure'] == 'directory' (未经testing)。

  • 如果你将这些项目作为parameter passing给一个类,那么通过in-puppet-how-can-i-access-a-variable-attribute-inside-a-defined来访问类参数的值是一种偷偷摸摸的方式。 types 。