Ask Your Question

How to use an array as argument to check for installed packages together with if defined function

asked 2017-09-12 10:55:56 -0500

mknl gravatar image


is it possible to check if an array of packages is already installed before installing them or do I have to check them one by one like:

if ( ! defined ( Package['packagename'] ) ) {
    package {
            ensure => present;

I would like to do something like:

$array_of_packages = ['packagename1','packagename2','packagename3']    
if ( ! defined ( Package [ $array_of_packages ] ) ) {
                    package { $array_of_packages:
                            ensure => present

If this is not supported is there a recommended way to check it (except one by one checking)?

Thanks in advance

edit retag flag offensive close merge delete



Why do you want that? The resource itself already checks what the current state of a package is and acts accordingly then.

Kai Burghardt gravatar imageKai Burghardt ( 2017-09-12 13:27:21 -0500 )edit

Because otherwise puppet throughs an error. If it just would skip it would be ok for me but it doesn't. Error which is shown is: "Duplicate declaration: Package[packagename] is already declared in file ..." which points to the file in another module someone else already wanted to install it.

mknl gravatar imagemknl ( 2017-09-13 02:06:49 -0500 )edit

Your problem is not 'is it already installed?', your problem is 'is this particular package resource already defined in this catalog?' Puppet only allows you to define each resource once

DarylW gravatar imageDarylW ( 2017-09-13 05:59:22 -0500 )edit

Correct, so my wording in this case was wrong. Anyway, there are two modules where I want to install all necessary prequired packages of a software. Two modules which can be used independetly, so I would like to define some packages twice. But how, if one by one define+install should be prevented?

mknl gravatar imagemknl ( 2017-09-13 06:05:54 -0500 )edit

DarylW gravatar imageDarylW ( 2017-09-13 06:12:18 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2017-09-13 06:20:13 -0500

DarylW gravatar image

The best solution that I can find is in the stdlib: the ensure_packages resource

Under the hood it is doing the same thing as your code, and is subject to the same ordering issues (if a module directly creates the package resource after calling 'ensure_resource' then you will see the same error.

edit flag offensive delete link more



Thanks a lot. I will be able to finally test it and confirm that it works with a complete new installed system which use these modules, but at least it does not show any error at the current test-system during a "puppet agent --test" run.

mknl gravatar imagemknl ( 2017-09-13 07:10:01 -0500 )edit

right now I use: ensure_packages($array_of_packages, {'ensure' => 'installed'})

mknl gravatar imagemknl ( 2017-09-13 07:12:33 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-09-12 10:55:56 -0500

Seen: 20 times

Last updated: Sep 13