简体   繁体   English

木偶只需要从exec资源调用文件资源

[英]puppet need to call file resource from exec resource only

Is there anyway to call puppet file resource from exec resource only when unless condition met? 反正是有叫傀儡file从资源exec资源,只有当unless条件满足? Means by default file resource shouldn't executed and it can be triggered thru exec resource only. 意味着默认情况下不应执行file资源,并且只能通过exec资源来触发它。

Updated details: 更新的详细信息:

Here is my manifest file 这是我的清单文件

file { '/tmp/test_script.sh':
        path => '/tmp/test_script.sh',
        mode => 755,
        owner => 'root',
        group => 'root',
        ensure => file,
        source => "puppet:///modules/custom_files//tmp/test_script.sh",
}

exec {'run_script':
        unless => '/bin/rpm -qa | grep package-name',
        require => File['test_script.sh'],
        command => '/tmp/test_script.sh',
}

Here I want file { '/tmp/test_script.sh': resource has to executed only when condition unless => '/bin/rpm -qa | grep package-name', 在这里,我要file { '/tmp/test_script.sh':资源仅在有条件时才执行, unless => '/bin/rpm -qa | grep package-name', unless => '/bin/rpm -qa | grep package-name', on exec resource doesn't meet. unless => '/bin/rpm -qa | grep package-name',但不符合exec资源。 Otherwise this file resource shouldn't be executed. 否则,不应执行此文件资源。

What you present has no chance of working anything like how you intend. 您呈现的内容无法像您的预期那样工作。 Because the Exec requires the File to (conditionally) be applied first, its own unless parameter would not be evaluated in time to affect that, even if there were a way it could do. 由于Exec要求首先(有条件地)应用File ,因此,即使有某种方法可以执行,它的unless参数不能及时评估也不会影响到它。

Generally speaking, details of machines' current state on which Puppet is to base decisions about the contents of that machine's catalog must be communicated to the catalog compiler via facts . 一般而言,必须通过事实将Puppet用来确定有关该计算机目录内容的机器当前状态的详细信息传达给目录编译器。 Puppet / Facter has no built-in fact that conveys the information you need, but it is pretty easy to add an external fact or a custom fact that the agent will evaluate for you. Puppet / Facter没有内置的事实可以传达您所需的信息,但是添加外部事实或代理可以为您评估的自定义事实非常容易。

Details vary slightly according to the software versions involved, but if you are using even remotely recent Puppet and Facter then my recommendation would be an external fact. 具体细节根据所涉及的软件版本而略有不同,但是如果您甚至使用的是远程最新的Puppet和Facter,那么我的建议将是一个外部事实。 For example, you might drop this script in the appropriate module's facts.d/ directory: 例如,您可以将此脚本放置在相应模块的facts.d/目录中:

#!/bin/bash

echo mypackage=$(/bin/rpm -q mypackage || echo none)

In your manifest, you could then do this: 在清单中,您可以这样做:

if $mypackage == 'none' {
  file { '/tmp/test_script.sh':
    path   => '/tmp/test_script.sh',
    mode   => 755,
    owner  => 'root',
    group  => 'root',
    ensure => file,
    source => "puppet:///modules/custom_files//tmp/test_script.sh",
  }

  exec {'run_script':
    command => '/tmp/test_script.sh',
    require => File['test_script.sh'],
  }
}

Note that facts are evaluated before any resources are applied, so if you have other manifests that might manage the RPM in question, then you'll want to coordinate with that. 请注意,事实是在应用任何资源之前进行评估的,因此,如果您有其他清单可能会管理所讨论的RPM,则需要与之进行协调。

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

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