本文介绍了Puppet 语法:如何将对象数组包含在排序中 ->链?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有:

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

我想使用 ->~> 结构来控制通知/要求执行顺序,如下所示:

And I want to use the -> and ~> constructs to control the notify/require order of execution, like so:

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

我该怎么做?

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

If I do the above, I get Could not find resource 'File[file1]File[file2]' (for real file paths, of course). I've played with wrapping the $files variable in quotes and {}s, but to no avail.

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

What's the syntax for putting an array variable of resource names into an ordering chain?

推荐答案

您可以使用 来自 RIPienaar 的这个很棒的提示列表:

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

First define a function that handles the chaining, then pass in your array to the function.
The function will get called once for each item in the array.

代码采样时间:

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: }

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

The output of 'puppet apply test.pp' on puppet 2.7.11 on Ubuntu 12.04 gives:

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

这篇关于Puppet 语法:如何将对象数组包含在排序中 ->链?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:54