简体   繁体   English

尽管执行,木偶文件资源仍会运行,除非

[英]Puppet File resource runs despite Exec unless

I'm applying a local manifest to test an exec and file resource: 我正在应用本地清单来测试execfile资源:

class test_file {   
  exec { "touch /tmp/testfile":
    path   => ["/bin", "/usr/bin", "/sbin", "/usr/sbin"],
    unless => "test -d /tmp 2>/dev/null",
  }

  file { "/tmp/success":
    mode    => "0777",
    owner   => "root",
    group   => "staff",
    source  => "file:///Users/Shared/success",
    require => Exec["touch /tmp/testfile"],
  }
}

What I expect to happen: The exec runs and exits because the unless statement returns 0, so the file resource never runs and neither file is created in /tmp . 我期望发生的事情: exec运行并退出,因为unless语句返回0,所以文件资源从不运行,也没有在/tmp创建文件。

What Actually happens: The file resource always runs, and copies the "success" file to /tmp 实际发生的情况: file资源始终运行,并将“成功”文件复制到/tmp

I've run this on macOS 10.11 and 10.12 with both puppet agent 4.10 and 5.3.2 ... with the same results. 我已经在puppet agent 4.10和5.3.2的macOS 10.11和10.12上运行了此程序,结果相同。 I'm simply trying to run a conditional that says don't copy a file if some command exits successful. 我只是试图运行一个条件,说如果某些命令成功退出,请不要复制文件。 Any insights or directions would be appreciated. 任何见解或方向将不胜感激。

You are misundersanding how Puppet works. 您误会了Puppet的工作方式。

The require metaparameter declares the order in which resources should be applied.* Given your manifest, Puppet only promises that the Exec resource - if it is applied at all - will always be applied before the File resource. require元参数声明了应用资源的顺序。*给您的清单,Puppet仅承诺Exec资源( 如果已应用)将始终在File资源之前应用。

There is another metaparameter, subscribe . 还有另一个元参数, subscribe If a resource subscribes to another resource, then Puppet tries to "refresh" that resource if the resource it is subscribed to changes state. 如果一个资源订阅了另一个资源,那么如果订阅了该资源的状态更改,Puppet会尝试“刷新”该资源。

Unfortunately, you can't use that here, because File resources ignore "refresh events". 不幸的是,您不能在这里使用它,因为文件资源会忽略“刷新事件”。

What you can do, although it's a bit ugly, is: 尽管有些丑陋,但是您可以做的是:

class test_file () {
  Exec {
    path => ["/bin", "/usr/bin", "/sbin", "/usr/sbin"],
  }

  exec { "touch /tmp/testfile":
    unless  => "test -d /tmp 2>/dev/null",
  }

  exec { "cp /Users/Shared/success /tmp/success":
    subscribe   => Exec["touch /tmp/testfile"],
    refreshonly => true,
  }
}

* The require metaparameter also prevents a second resource from being applied at all if the first resource fails to apply. *如果第一个资源无法应用,那么require元参数也将完全阻止第二个资源被应用。 See the failed dependencies section in the docs. 请参阅文档中的失败依赖项部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM