Puppet Syntax: how to include an array of objects into an ordering -> chain?

Deadly 提交于 2019-12-13 04:35:36

问题


Let's say I have:

$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]

How do I do it?

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?


回答1:


You can use the 'What is in an array' hint from this great list of hints from R.I.Pienaar:

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.

Code sample time:

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

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



回答2:


Why not to use require instead?

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

or just do

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


来源:https://stackoverflow.com/questions/11123462/puppet-syntax-how-to-include-an-array-of-objects-into-an-ordering-chain

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!