Ask Your Question
0

how to build an array of hash keys?

asked 2015-01-10 14:04:08 -0500

Phondos gravatar image

I have defined some virtual resources. I would like to create a class that will realize some of those resources and build an array of the $titles of those resources. Is this even possible in Puppet, or do I need to abstract my data out to Heira to do something like this?

define users ($groups) {
  user { $title:
    groups => $groups
  }
}

@users { ‘user1’:
  groups => [ ‘foo’, ‘bar’, ‘baz’ ]  
}

@users { ‘user2’:
  groups => [ ‘bar’, ‘baz’ ]
}

@users { ‘user3’:
  groups => [ ‘foo’, ‘baz’ ]
}

class members {

# Collect the resources where ‘foo’ is in groups

  Users <| groups == ‘foo’ |>

# Would like to build an array here of Users[title] for the Users resources we just 
# collected. To end up with $group_members = [ ‘user1’, ‘user3’ ]

  $group_members = ???

}
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2015-01-11 15:32:52 -0500

inhumantsar gravatar image

Your best bet would be to write a custom function to that effect. Puppet Docs say collectors can be used as values (eg: in functions) but warn that using them will realize the resources involved. So in theory, you should be able to pass the collection of Users, foreach through the users, adding each title into an array before returning that.

$user_titles = get_titles(Users <| groups == 'foo' |>)

Of course, that's assuming that using (abusing?) collectors in this way will pass an array of Puppet::Resource objects back to your custom function.

Sorry this isn't a complete answer.

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: 2015-01-10 13:57:31 -0500

Seen: 503 times

Last updated: Jan 11 '15