conditional within define in puppet

不羁的心 提交于 2019-12-09 18:51:24

问题


Running Puppet 3.8

I have two defines:

    define desktop::vinstall () {  
      package { $title:  
        ensure        => installed,  
        allow_virtual => true,  
        configfiles   => keep,  
      }  
    }  

and

    define desktop::vinstallwseeds () {  
      package { $title:
        ensure        => installed,  
        allow_virtual => true,  
        configfiles   => keep,
        require       => File["/var/cache/debconf/pkg-${title}.seeds"],  
        responsefile  => "/var/cache/debconf/pkg-${title}.seeds",  
      }  
      file { "/var/cache/debconf/pkg-${title}.seeds":  
        source => "puppet:///modules/desktop/pkg-${title}.seeds",  
        ensure => present,  
      }  
    }

Would like to turn these into one define statement with an optional boolean argument, something like:

    define desktop::vinstallopt ( $queryresponse = 'false', ) {  
      package { $title:  
        ensure        => installed,  
        allow_virtual => true,  
        configfiles   => keep,  
        if $queryresponse == 'true' {  
          require      => File["/var/cache/debconf/pkg-${title}.seeds"],  
          responsefile => "/var/cache/debconf/pkg-${title}.seeds",  
        }  
      }  
      file { "/var/cache/debconf/pkg-${title}.seeds":  
        source => "puppet:///modules/desktop/pkg-${title}.seeds",  
        ensure => present,  
      }  
    }  

and then instantiate it with statements like this one in init.pp:

    @desktop::vinstallopt { 'gdebi': queryresponse => 'false', }

But doing so gives an error:

    Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Puppet::Parser::AST::Resource failed with argument error ArgumentError: Invalid resource type desktop::vinstallopt at /etc/puppet/modules/desktop/manifests/init.pp:40 on node machine.prvt.net

where line 40 has the syntax above. I'm a newbie with puppet, so my apologies if this turns out the be a simple syntax question. I've tried to find a way to make this work from the PuppetLabs documentation and from other puppet users, so far without luck.


回答1:


You are trying to embed an if block inside a resource declaration. Alas, this is not possible. The block must be global or in a regular block (e.g., class body, define body, lambda body).

In this case, you want to "amend" the package resource, so to speak. I like to use the following construct for this purpose:

package { $title:  
    ensure        => installed,  
    allow_virtual => true,  
    configfiles   => keep,  
}  
if $queryresponse {  
    Package[$title] {
        require      => File["/var/cache/debconf/pkg-${title}.seeds"],  
        responsefile => "/var/cache/debconf/pkg-${title}.seeds",  
    } 
}

Please note that this override syntax is only allowed in this scope because the require and responsefile attributes don't have any value assigned originally.



来源:https://stackoverflow.com/questions/34459241/conditional-within-define-in-puppet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!