![Puppet,管理两个依赖的文件资源?](https://linux22.com/image/37058/Puppet%EF%BC%8C%E7%AE%A1%E7%90%86%E4%B8%A4%E4%B8%AA%E4%BE%9D%E8%B5%96%E7%9A%84%E6%96%87%E4%BB%B6%E8%B5%84%E6%BA%90%EF%BC%9F.png)
我无法弄清楚,当我有两个由木偶管理的资源,相互依赖(一个是另一个的父级)并且两者都设置为 时,如何处理这种情况absent
。下面的测试用例。目录尚不存在,但 puppet 抱怨在那里创建文件。如何修复这个清单?
$ find . -type f
./modules/test/manifests/init.pp
./modules/test/files/b/x
./modules/test/files/b/z
./modules/test/files/b/y
./manifests/site.pp
./test.pp
$ cat manifests/site.pp
## site.pp
node default {
include test
}
$ cat modules/test/manifests/init.pp
## init.pp
class test {
file { 'dir1':
ensure => absent,
path => '/tmp/a',
before => File['dir2'],
}
file { 'dir2':
ensure => absent,
path => '/tmp/a/b',
source => 'puppet:///modules/test/b',
force => true,
purge => true,
recurse => true,
}
}
$ puppet apply --modulepath ./modules manifests/site.pp
Notice: Compiled catalog for localhost in environment production in 0.11 seconds
Error: Could not set 'file' on ensure: cannot generate tempfile `/tmp/a/b/z20140523-14751-1qpqnds-9'
Error: Could not set 'file' on ensure: cannot generate tempfile `/tmp/a/b/z20140523-14751-1qpqnds-9'
Wrapped exception:
cannot generate tempfile `/tmp/a/b/z20140523-14751-1qpqnds-9'
Error: /Stage[main]/Test/File[/tmp/a/b/z]/ensure: change from absent to file failed: Could not set 'file' on ensure: cannot generate tempfile `/tmp/a/b/z20140523-14751-1qpqnds-9'
Error: Could not set 'file' on ensure: cannot generate tempfile `/tmp/a/b/y20140523-14751-1bxpkko-9'
Error: Could not set 'file' on ensure: cannot generate tempfile `/tmp/a/b/y20140523-14751-1bxpkko-9'
Wrapped exception:
cannot generate tempfile `/tmp/a/b/y20140523-14751-1bxpkko-9'
Error: /Stage[main]/Test/File[/tmp/a/b/y]/ensure: change from absent to file failed: Could not set 'file' on ensure: cannot generate tempfile `/tmp/a/b/y20140523-14751-1bxpkko-9'
Error: Could not set 'file' on ensure: cannot generate tempfile `/tmp/a/b/x20140523-14751-1u3y3dl-9'
Error: Could not set 'file' on ensure: cannot generate tempfile `/tmp/a/b/x20140523-14751-1u3y3dl-9'
Wrapped exception:
cannot generate tempfile `/tmp/a/b/x20140523-14751-1u3y3dl-9'
Error: /Stage[main]/Test/File[/tmp/a/b/x]/ensure: change from absent to file failed: Could not set 'file' on ensure: cannot generate tempfile `/tmp/a/b/x20140523-14751-1u3y3dl-9'
Notice: Finished catalog run in 0.14 seconds
编辑:
完整上下文:
## init.pp
class test {
$is_master = false
if $is_master {
$file_ensure = present
$directory_ensure = directory
} else {
$file_ensure = absent
$directory_ensure = absent
}
file { 'dir1':
ensure => $directory_ensure,
path => '/tmp/a',
before => File['dir2'],
}
file { 'dir2':
ensure => $directory_ensure,
path => '/tmp/a/b',
source => 'puppet:///modules/test/b',
force => true,
purge => true,
recurse => true,
}
}
您无法删除 dir1,因为它在主设备上会失败,我需要使其在主设备和从设备上都能工作。
答案1
彻底删除File['dir2']
资源,修改File['dir1']
为:
file { 'dir1':
ensure => absent,
path => '/tmp/a',
recurse => true,
force => true,
}
使用您的代码,您尝试/tmp/a
在删除 之前删除/tmp/a/b
, 。如果你/tmp/a
先删除,那么就/tmp/a/b
根本不存在。你想要相反的顺序。
然而,这甚至没有必要。如果您要删除/tmp/a
,则没有理由创建资源定义来删除其中的任何内容。