conditional within define in puppet

2020-07-23 09:01发布

问题:

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.



标签: puppet