简体   繁体   English

仅在文件已更改时运行p资源类型

[英]run puppet resource types only when file has changed

I feel like this is a very common scenario but yet I haven't found a solution that meets my needs. 我觉得这是一个非常常见的情况,但是我还没有找到满足我需求的解决方案。 I want to create something that works like below. 我想创建类似下面的内容。 (Unfortunately set_variable_only_if_file_was_changed doesn't exist) (很遗憾, set_variable_only_if_file_was_changed不存在)

file { 'file1':
      path => "fo/file1",
      ensure => present,
      source => "puppet:///modules/bar/file1",
      set_variable_only_if_file_was_changed  => $file_change = true
}

if $file_change{
    file{'new file1'
        ...
    }
    exec{'do something1'
        ...
    }
    package{'my package'
        ...
    }
} 

I have seen how exec can be suppressed using the refreshonly => true but with files it seems harder. 我已经看到如何使用refreshonly => true抑制exec,但是使用文件似乎更困难。 I would like to avoid putting code into a .sh file and execute it through a exec. 我想避免将代码放入.sh文件并通过exec执行它。

Puppet does not have the feature you describe. 木偶没有您描述的功能。 Resources that respond to refresh events do so in a manner appropriate to their type; 响应刷新事件的资源以适合其类型的方式进行响应; there is no mechanism to define custom refresh behavior for existing resources. 没有机制来定义现有资源的自定义刷新行为。 There is certainly no way to direct which resources will be included in a node's catalog based on the results of applying other resources -- as your pseudocode seems to suggest -- because the catalog is completely built before anything in it is applied. 当然,无法根据应用其他资源的结果来指示将哪些资源包括在节点的目录中(正如您的伪代码所暗示的那样),因为目录是在应用目录中的任何内容之前完全构建的。

Furthermore, that is not nearly as constraining as you might suppose if you're coming to Puppet with a substantial background in scripting, as many people do. 此外,如果您要像许多人一样具有相当丰富的脚本编写背景来学习Puppet,那么这并没有您想像的那么麻烦。 People who think in terms of scripting need to make a mental adjustment to Puppet, because Puppet's DSL is definitely not a scripting language. 考虑脚本编写的人需要对Puppet进行心理调整,因为Puppet的DSL绝对不是脚本语言。 Rather, it is a language for describing the construction of a machine state description (a catalog). 而是一种用于描述机器状态描述(目录)构造的语言。 It is important to grasp that such catalogs describe details of the machine's target state, not the steps required to take it from its current state to some other state. 重要的是要掌握此类目录描述机器目标状态的详细信息, 而不是将其从当前状态转换为其他状态所需的步骤。 (Even Exec s, a possible exception, are most effective when used in that way.) It is very rare to be unable to determine a machine's intended state from its identity and initial state. (即使以这种方式使用, Exec可能是最有效的。)很少能够根据其标识和初始状态来确定机器的预期状态。 It is uncommon even for it to be difficult to do so. 即使很难做到这一点也不常见。

How you might achieve the specific configuration you want depends on the details. 如何实现所需的特定配置取决于细节。 Often, the answer lies in user-defined facts. 通常,答案在于用户定义的事实。 For example, instead of capturing whether a given file was changed during catalog application, you provide a fact that you can interrogate during catalog building to evaluate whether that file will be changed, and adjust the catalog details appropriately. 例如,您提供的事实是,您可以在目录构建期间进行询问以评估该文件是否被更改,并适当地调整目录详细信息,而不是捕获目录应用程序期间给定文件是否已更改。 Sometimes you don't need such tests in the first place, because Puppet takes action on a given resource only when changes to it are needed. 有时您一开始就不需要这样的测试,因为Puppet仅在需要更改给定资源时才对它进行操作。

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

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