Puppet - test if a package already defined?

前端 未结 4 1109
面向向阳花
面向向阳花 2021-02-12 15:09

I\'m writing some puppet modules and have a package defined in two modules hence get the following error:

err: Could not retrieve catalog from remote server: Err         


        
相关标签:
4条回答
  • 2021-02-12 15:30

    You are missing Package[] inside defined(). The correct way to do it:

    if ! defined(Package['gnome-session-fallback']) {
        package { 'gnome-session-fallback':
            ensure => installed,
        }
    }
    
    0 讨论(0)
  • 2021-02-12 15:32

    The cleanest way to do this is to use the ensure_resource function from puppetlabs-stdlib:

    ensure_resource('package', 'gnome-session-fallback', {'ensure' => 'present'})

    0 讨论(0)
  • 2021-02-12 15:36

    To answer my own question about what the "proper" approach is : This issue is discussed at https://groups.google.com/forum/?fromgroups=#!topic/puppet-users/julAujaVsVk and jcbollenger offers what looks like a "best-practice" solution - resources which are defined multiple times should be moved into their own module and included into the classes on which they depend. I applied this and solved my problem.

    This doesn't actually answer why "if !defined" fails however...

    0 讨论(0)
  • 2021-02-12 15:36

    One cleaner way (among multiple ways) is to create a virtual package resource and then realize it. You can realize the same virtual package multiple times without error.

    @package { 'gnome-session-fallback':
        ensure => installed,
    }
    

    And then where you need it:

    realize( Package[ 'gnome-session-fallback' ] )
    
    0 讨论(0)
提交回复
热议问题