在我的Puppetconfiguration中,我想说“如果我声明类X,在Y类之前应用它的资源。 换句话说,我想要声明一个顺序,但对于是否应用类X保持沉默。
如果我正确理解“之前”的元参数,并说:
class X { ... before => Class['Y'], } class Y { ... } node N { include Y } node M { include X include Y }
将在节点M和N上包含X和Y的资源。相反,我想要分别表示“只应用Y”,或者“应用X和Y,并在Y之前应用X.
对于上下文,我想要更具体地做的是确保我的Yum存储库在Puppet应用程序包资源之前进行configuration。 我想省略一些节点的一些存储库。 我希望我的包资源定义对于相应的存储库保持天真。 我不想乱丢我的包资源定义与依赖于特定的存储库。
我尝试使用运行阶段,但超出最简单的configuration,他们似乎导致依赖周期。 例如,这将工作:
stage { 'first': before => Stage['main'] } class X { ... before => Class['Y'], } class Y { ... } node N { include Y } node M { class { "X": stage => first } include Y }
但是这不会:
stage { 'first': before => Stage['main'] } class X { ... class { "Z": stage => first } # to avoid Z "floating off" before => Class['Y'], } class Y { ... include Z } class Z { ... } node N { include Y } node M { class { "X": stage => first } include Y }
在后者中,Puppet认为存在一个依赖循环。 我认为这是由于Z被宣布,其资源pipe理分两个阶段。 为了避免我在实践中看到的依赖循环问题,我可能会简化我在“第一个”阶段所需的类,但是Puppet文档会传播有关运行阶段的FUD 。
以下是configuration的具体位置,使得运行阶段对我来说真的没有吸引力。 在开发虚拟机中,我通过“devrepo”在本地引导Yum服务器。
class yum::devrepo { # Ensures httpd is running as a Yum server before anything else # tries to install packages from it. exec { 'httpd-for-yum': command => '/sbin/service httpd restart', require => Class['yum::server'], } yumrepo { "devrepo": require => [Exec['httpd-for-yum'],], descr => "Local Dev YUM Repo", baseurl => "http://localhost/repos/redhat/5/x86_64/", gpgcheck => "0", enabled => "1", } } class yum::server { include httpd package { ['createrepo']: ensure => present; } exec { 'update-repo-metadata': require => [ Package['createrepo']], cwd => '/var/www/html/yum', command => '/usr/bin/createrepo --update -d repos/redhat/5/x86_64/', creates => '/var/www/html/yum/repos/redhat/5/x86_64/repodata/repomd.xml', } file {'/etc/httpd/conf.d/yum.conf': ensure => file, mode => 0644, source => "puppet:///modules/yum/yum_httpd.conf", require => Package['httpd'], notify => Service['httpd'], } }
我想包括我的生产Yum回购一些节点和dev回购别人。
如果我把yum::server和yum::devrepo放在stage“first”中,那么稍后的其他类中的httpd声明会导致问题,因为其他类会把httpd放在main阶段。
如果木偶能够expression“如果我申报X级,在Y级之前申请它的资源”,如何? 如果没有,我怎么能得到我想要的顺序,而不依赖周期,没有包括我想省略的Yum库资源?
不知道它是否会工作,但也许你可以尝试定义一个节点级作用域的依赖关系。 例如:
class X { ... } class Y { ... } node N { include Y } node M { Class['X'] -> Class['Y'] include X include Y }
看起来golja的答案可能会起作用。 这是我自己登陆的模式。 这似乎是迄今为止工作。
class yum::reposareready { # The sole and critical purpose of this class is to act as an # intermediary between various repositories and the package # resources. It lets us do things like: # class yum::repofoo { ... } # class applicationbar { # package { 'bazfromtherightrepo': ..., require => yum::reposareready, } # } # node n { # class { 'yum::repofoo': before => Class['yum::reposareready'] } # } # With this pattern: # 1. The repository resource doesn't need to know about its ordering. # 2. Nodes can mix in repository resources, including and excluding # repositories as needed. # 3. Classes that declare package resources need only require a # generic "repos are ready" class rather than the knowing the # specific repository from which to get a package. # DO NOT DO THIS: # class yum::repofoo { before => Class['yum::reposareready'] } # class yum::repobar { before => Class['yum::reposareready'] } # # node n { # include yum::repofoo # } # # node m { # include yum::repobar # } # The former scopes the ordering dependency to the node, whereas the # latter does not. The latter would make Puppet apply yum::repofoo # to both nodes n and m, whereas the former only applies # yum::repofoo to node n. }