在具有不同variables值的不同点处重新应用模板

我似乎不明白如何在Puppet清单的应用程序中改变文件资源的内容。 我用Vagrant来设置开发环境。

我想安装oh-my-zshell ,为.zshrc 文件提供一个模板 。 哦,myzshell有插件的概念,所以我select我想要的插件,将它们添加到模板,并使其成为傀儡清单的模板:

class oh_my_zshell { include git include zsh $oh_my_zshell_dir = "${vagrant_home_dir}/.oh-my-zsh" $plugins = "command-not-found common-aliases sudo" $theme = "ys" $zshrc = "${vagrant_home_dir}/.zshrc" $backup = "${zshrc}.orig" @file { "zshrc": path => $zshrc, ensure => file, content => template("oh_my_zshell/zshrc.erb"), } realize( File["zshrc"],Package["zsh"] ) 

喀嚓!

并在模板中:

 # Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*) # Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/ # Example format: plugins=(rails git textmate ruby lighthouse) plugins=(<%= @plugins %>) 

这工作正常。

后来,我决定我想安装chruby 。 chruby有一个oh-my-zshell插件,所以我想用这个插件的新值重新应用模板:

 class chruby { include apt_update include oh_my_zshell include stdlib $plugins = "${oh_my_zshell::plugins} chruby" $theme = $oh_my_zshell::theme 

喀嚓!

  realize(File["zshrc"]) file_line {"chruby set default ruby": path => $oh_my_zshell::zshrc, line => 'chruby ruby-2.1.2', require => File["zshrc"], } 

但是,这根本不会改变文件 – 插件语句中没有chruby ,并且没有行设置默认的ruby。

对我来说是有意义的,因为安装不同的东西,他们改变一个文件来满足它的要求,并且在安装过程的每一个时刻,文件都处于一个可行的状态。 oh_my_zshell类不需要知道chruby类或其他可能稍后安装的东西。

有什么我应该做的这个工作,还是我的工作反对木偶应该使用的方式? 我可以看到的另一种方法是将文件写出类,并在主清单中完成,直到所有variables都被填充。

我是木偶新手,所以任何帮助或见解将不胜感激。

你不应该使用虚拟资源( @filerealize )。 模板的variables将来自它在@file定义的原始范围,而不是它实现的地方。

你想要的是一个定义

 define thingy( $path, $plugins) { file { '$path': content => template("oh_my_zshell/zshrc.erb"), } } 

如果你想添加行,你应该添加一个可选的参数给定义,并在模板中有条件地使用。 使用内容或来源定义文件,然后尝试在另一个资源中编辑该文件的内容将无法达到您想要的效果。

或者,也许有意义的是有oh_my_zshell是一个需要参数并传递“additional_plugins”variables的类…

 # declare class with param class oh_my_zshell($additional_plugins = '') { # put it into the other variable or use in the template or whatever } # instead of the include/require class { 'oh_my_zshell': additional_plugins = 'chruby', }