Ask Your Question
0

How to specify function dependency?

asked 2013-11-18 08:27:16 -0600

deric gravatar image

updated 2016-04-15 13:20:18 -0600

I want to make sure that statement will be executed before other class declaration.

ensure_packages(['python'])

(ensure_packages is a function from puppet/stdlib)

However ensure_packages accepts only one argument (an Array), unlike ensure_resource where you can write:

  ensure_resource('package', 'python', {
    ensure  => present,
    before  => Anchor['some::anchor'],
  })

The latter would cause a Duplicate declaration: Package[python] is already declared error. So, I'd like to stay with first function and add some require statement. Is it possible?

UPDATE: Current version of stdlib supports such statement:

  ensure_packages(['python'], {
    'ensure'  => 'latest',
    'require' => Anchor['some::anchor'],
  })

However if somewhere else in your codebase (e.g. 3rd party module) you have declaration like this:

 package { 'python':
     ensure => present,
 }

You'll get the duplicate declaration error.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2013-11-18 09:27:52 -0600

shtuffit gravatar image

To avoid "Duplicate declaration: Package[python] is already declared" error you can wrap the ensure_resource:

if ! defined(Package['python']) {
  ensure_resource('package', 'python', {
    ensure  => present,
    before  => Anchor['some::anchor'],
  })
}
edit flag offensive delete link more

Comments

1

`ensure_*` functions were created to avoid such constructions

deric gravatar imagederic ( 2013-11-18 16:15:31 -0600 )edit
0

answered 2016-04-09 12:26:17 -0600

DarylW gravatar image

updated 2016-04-15 08:08:01 -0600

Does the function create an actual puppet resource under the hood? Is it possible to use the collection (Spaceship) operator to enforce the ordering? Or even simply use the capital P form of package?

Package['python'] -> Anchor['some::anchor']

-- EDIT --

I tested it out, and it does seem to work that way..

ensure_packages(['python'])

exec{'hi':
  command => '/usr/bin/python --version',
  logoutput => true,
}

Package['python'] -> Exec['hi']

Here's the output from the run

$ sudo puppet apply ~/test_ensure_package.pp
Notice: Compiled catalog for ubuntu in environment production in 0.18 seconds
Notice: /Stage[main]/Main/Exec[hi]/returns: Python 2.7.10
Notice: /Stage[main]/Main/Exec[hi]/returns: executed successfully
Notice: Applied catalog in 0.08 seconds

-- UPDATE -- Even with today's current version of ensure_packages, you can still run into duplicate declaration problems if you pass a hash in.

I was using the consul module(1.0.2) along with a class that had ensure_packages('unzip'), and I had the duplicate declaration error.

It contained the following code, which was causing the problem (issue outlined here)

ensure_packages('unzip', { before => 'Staging::File['consul.zip'] })

I replaced it with

ensure_packages('unzip')
Package['unzip'] -> 'Staging::File['consul.zip']

And that solved my problem!

edit flag offensive delete link more

Comments

This question was from a little over 2 years ago; the current version of ensure_packages does what the asker requires.

gmcallister gravatar imagegmcallister ( 2016-04-11 09:57:41 -0600 )edit

Ah, I didn't even see the date, I saw it suggested on the side, and figured I could help!

DarylW gravatar imageDarylW ( 2016-04-11 21:16:24 -0600 )edit

It would be good if someone deleted a lot of the old unanswered questions I think.

Alex Harvey gravatar imageAlex Harvey ( 2016-04-12 03:13:40 -0600 )edit

They do contain good information sometimes.. perhaps if answers are provided but marked, we could just select the correct response to them.. or if there was some kind of warning that popped up telling you the question was old

DarylW gravatar imageDarylW ( 2016-04-13 11:30:40 -0600 )edit

Just saw this IRL. Adding the hash to ensure_packages caused 'Duplicate declaration' error. One had a hash with a before param for ordering. My solution above solves the ordering problem without a duplicate declaration.

DarylW gravatar imageDarylW ( 2016-04-15 07:45:16 -0600 )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

2 followers

Stats

Asked: 2013-11-18 08:27:16 -0600

Seen: 1,784 times

Last updated: Apr 15 '16