【问题标题】:Puppet Syntax: how to include an array of objects into an ordering -> chain?Puppet 语法:如何将对象数组包含到排序 -> 链中?
【发布时间】:2012-06-20 15:57:24
【问题描述】:

假设我有:

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

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

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

我该怎么做?

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

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

【问题讨论】:

    标签: puppet


    【解决方案1】:

    您可以使用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
    

    【讨论】:

      【解决方案2】:

      为什么不使用 require 呢?

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

      或者干脆做

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

      【讨论】:

      • 因为实际的应用程序要大得多,文件阵列要大得多,资源链也多得多,而且有更多的人在工作。如果我愿意在每个对象中使用 requires ,我可以通过多种方式使其工作,但人们最终会犯错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 2013-06-17
      • 2017-09-13
      • 2021-06-17
      • 2015-02-17
      • 1970-01-01
      相关资源
      最近更新 更多