Ask Your Question
3

Iterate nested hash from hiera in manifest

asked 2013-03-20 22:32:44 -0500

STOIE gravatar image

updated 2013-03-24 22:52:14 -0500

Hi all,

I have the following YAML coming from hiera:

---
servers:
    s01:
        sip: 1.1.1.1
        shostname: server01
    s02:
        sip: 1.1.1.2
        shostname: server02

Now in my puppet manifest I want to iterate the hash and lets say create notify blocks for each shost...

So, lets say I pull in the var:

$servers = hiera("servers")

Then, I want to create these blocks:

notify { "currently looping server hostname ${BLAH}.": }

Which I want to output:

currently looping server hostname server01
currently looping server hostname server02

If you understand what I want to do and care to take the time ... (more)

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted
8

answered 2013-03-22 09:29:36 -0500

jonn gravatar image

To elaborate on my comment - here's an example of using a defined type with create_resources (assuming that hiera returns a hash in the {title => {parameters} }: form):

  define notify_servers($sip, $shostname) {
    notify { "currently looping server hostname $shostname": }
  }

  $servers = hiera("servers")
  create_resources( notify_servers, $servers )

This would output:

notice: currently looping server hostname server02
notice: currently looping server hostname server01

Is that the type of thing that you'd be looking for?

edit flag offensive delete link more

Comments

This is exactly what I am after, however, I am having one issue when using it... I am receiving a "can't convert nil into Hash" for the create_resources command ...(more)

STOIE gravatar imageSTOIE ( 2013-03-24 19:46:54 -0500 )edit

Got it working... seemingly, having the hyphens in the YAML make for an array of hashes of an array of hashes... not just a hash or hashes... so, by removing ...(more)

STOIE gravatar imageSTOIE ( 2013-03-24 22:53:35 -0500 )edit

Hi Jonn, what if the hash is like: servers: s01: sip: 1.1.1.1 shostname: server01 key1: value1 s02: sip: 1.1.1.2 shostname: server02 key2: value2 how do you use create_resources on this hash which does not have exactly same keys?

BellaXie gravatar imageBellaXie ( 2017-04-11 23:35:10 -0500 )edit
1

answered 2013-03-21 14:46:39 -0500

lavaman gravatar image

I think something like create_resources is what you're looking for:

http://docs.puppetlabs.com/references/latest/function.html#createresources

edit flag offensive delete link more

Comments

1

Hi there, thanks for the input! - I was wondering though, could this method be used with custom resource types, simply because none of the fields are relevant to any built-in ...(more)

STOIE gravatar imageSTOIE ( 2013-03-21 16:32:05 -0500 )edit
1

It can, yep - you can pass a defined type to create_resources.

jonn gravatar imagejonn ( 2013-03-22 09:08:16 -0500 )edit
0

answered 2015-09-02 08:53:57 -0500

winningpanthers gravatar image

Super.. This is exactly what I was looking for. I wish I had found this earlier. Million Thanks

edit flag offensive delete link more
0

answered 2016-09-23 09:09:32 -0500

Excellently answered.

edit flag offensive delete link more

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

Stats

Asked: 2013-03-20 22:32:44 -0500

Seen: 16,782 times

Last updated: Sep 02 '15