Ask Your Question
0

Pass array values in class

asked 2016-12-07 01:14:14 -0500

karthik gravatar image

updated 2016-12-07 15:22:03 -0500

lavaman gravatar image

Hello All,

I am begineer with puppet ,I was instructed to install the below packages using "class" what i have done initially is

$packages = [ 'python-pil','python-django', 'requests']
class web {
  package { $packages:
    ensure => installed,
  }
}

It hadn't worked then tried with

class python_web_pack {
  package { 'python-pil':
    ensure => installed,
  }
  package {'python-django':
    ensure => installed,
  }  
  package {'requests':
    ensure => installed,
    provider => pip,
  }
}

It was worked.However i have to mention the packages individually to accomplish How to shrink the lines by passing the array values.Please guide me

edit retag flag offensive close merge delete

Comments

https://docs.puppet.com/puppet/latest/function.html#each

Kai Burghardt gravatar imageKai Burghardt ( 2016-12-07 06:01:01 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2016-12-07 08:24:39 -0500

scoffland gravatar image

updated 2016-12-07 15:22:37 -0500

lavaman gravatar image

Try this

class pythonweb { 
  $pythonweb_packages = [ 'python-pil', 'python-django', 'requests' ] 
  package { $pythonweb_packages : 
    ensure => installed
  }
}
edit flag offensive delete link more

Comments

I was going to say this same thing, in the OP's example, the array is out of scope of the class.

DarylW gravatar imageDarylW ( 2016-12-08 13:04:13 -0500 )edit
1

answered 2016-12-07 08:27:33 -0500

Emerson Prado gravatar image

updated 2016-12-09 04:45:07 -0500

EDIT: seeing your code, the problem becomes clear: the $packages array is declared outside the class, so the class doesn't know about it. In tech jargon: the array is not in the class scope.
Just do as @scoffland said and you'll be fine.

Could you pls post your entire code - the web class and the calling class - along with the error?

Noticed the successful attempt passes a parameter - provider - that the failed one doesn't. If this is a requirement, you have to pass it too.

Also, I had problems trying to install several packages at once when there were dependencies among them. If that's the case, you have to explicit which ones get installed first.

edit flag offensive delete link more

Comments

Scope in puppet doesn't always act how people expect coming from other programming languages. We ran into a similar issue with a define declared inside of a class, and expecting the define to have the variable from the class in scope.. Hint, it isn't, and put your defines in their own files!

DarylW gravatar imageDarylW ( 2016-12-09 08:28:59 -0500 )edit

@DarylW VERY IMPORTANT hint. The same for classes. Each class and each define in its own file, respecting the autoload rules, and we're quite better.

Emerson Prado gravatar imageEmerson Prado ( 2016-12-10 08:25:36 -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

Stats

Asked: 2016-12-07 01:14:14 -0500

Seen: 57 times

Last updated: Dec 09 '16