Puppet语法:如何将一个对象数组包含在一个排序中 - >链?

时间:2012-06-20 15:57:25

标签: puppet

让我说我有:

$files = ["file1", "file2"]
exec { "exec1" :
    command => "mycommand";
}
file { $files :
    ensure => present;
}

我想使用->~>构造来控制执行的notify / require命令,如下所示:

Exec["exec1"] -> File[$files]

我该怎么做?

如果我执行上述操作,我会得到Could not find resource 'File[file1]File[file2]'(当然,对于真正的文件路径)。我已经将$files变量包装在引号和{}中,但无济于事。

将资源名称的数组变量放入排序链的语法是什么?

2 个答案:

答案 0 :(得分:2)

您可以使用this great list of hints from R.I.Pienaar中的“数组中的内容”提示:

首先定义一个处理链接的函数,然后将数组传递给函数 该函数将为数组中的每个项调用一次。

代码示例时间:

exec { "exec1":
     command => "/bin/echo 'i am the very model of a modern major general'";
}

file { 
    "/var/tmp/file1":
        ensure => present;
    "/var/tmp/file2":
        ensure => present;
}

define chaintest() {
    notify{"Calling chaintest with ${name}": }
    Exec["exec1"] -> File["${name}"]
}

$files = ["/var/tmp/file1","/var/tmp/file2"]

chaintest{$files: }

在Ubuntu 12.04上的puppet 2.7.11上输出'puppet apply test.pp'给出:

notice: Calling chaintest with /var/tmp/file1
notice: /Stage[main]//Chaintest[/var/tmp/file1]/Notify[Calling chaintest with /var/tmp/file1]/message: defined 'message' as 'Calling chaintest with /var/tmp/file1'
notice: /Stage[main]//Exec[exec1]/returns: executed successfully
notice: Calling chaintest with /var/tmp/file2
notice: /Stage[main]//Chaintest[/var/tmp/file2]/Notify[Calling chaintest with /var/tmp/file2]/message: defined 'message' as 'Calling chaintest with /var/tmp/file2'
notice: Finished catalog run in 0.11 seconds

答案 1 :(得分:0)

为什么不使用require呢?

$files = ["file1", "file2"]
exec { "exec1" :
    command => "mycommand";
}
file { $files :
    ensure => present;
    require => Exec["exec1"]
}

或只是做

Exec["exec1"] -> [File["file1"], File["file2"]]