在puppet中定义条件

时间:2015-12-25 01:09:05

标签: puppet

运行Puppet 3.8

我有两个定义:

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

    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,  
      }  
    }

想将这些转换为带有可选布尔参数的一个define语句,如:

    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,  
      }  
    }  

然后使用init.pp中的语句实例化它:

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

但这样做会出错:

    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

其中第40行具有上述语法。我是一个有木偶的新手,所以我很抱歉这是一个简单的语法问题。我试图从PuppetLabs文档和其他木偶用户那里找到一种方法来完成这项工作,到目前为止还没有运气。

1 个答案:

答案 0 :(得分:2)

您正尝试在资源声明中嵌入if。唉,这是不可能的。该块必须是全局的或在常规块中(例如,类主体,定义主体,lambda主体)。

在这种情况下,您希望"修改"可以这么说package资源。我喜欢为此目的使用以下构造:

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",  
    } 
}

请注意,此范围内仅允许此覆盖语法,因为requireresponsefile属性最初没有分配任何值。