Ask Your Question

How to get an array index in manifest

asked 2014-01-14 04:51:03 -0500

jaksky gravatar image

Hi, I would like to simplify my manifest by reducing duplicity in input parameters. I am provided an array of hostnames in the cluster and nodeId which says node number within cluster. That information can be derived.

$cluster=["host1",'host2", "host3.localdomain","host4"]
# on host4
$nodeId = 4

That's what I would like to get simplified. The issue can be hostname vs. fqdn. Puppet afaik doesn't offer any loop costrcts and I am not aware of if the lambdas are available.

Can someone give me a hint? Thx

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-01-14 07:47:32 -0500

doc75 gravatar image

updated 2014-01-15 15:53:48 -0500


Here is a functionn (tested with rspec):

# Inputs:
#   1st argument: Array in which we search for the element
#   2nd argument: element to be seearched for in the Array
# Returned values:
#   -1: element not found
#   otherwise: position of the element in the array (starting at index 0)
# Raised errors:
#   Puppet::ParseError: if 1st element is not an Array
require 'fileutils'
module Puppet::Parser::Functions
  newfunction(:array_index, :type => :rvalue) do |args|
    unless args[0].class == Array then
      raise Puppet::ParseError, 'array_index(): 1st argument must be an array'

    in_array = args[0]
    searched_elt = args[1]
    res = in_array.index searched_elt
    if res.nil ...
edit flag offensive delete link more


I am not sure then how to get en index in the original array. Usage just for filtering is clear

jaksky gravatar imagejaksky ( 2014-01-14 09:13:57 -0500 )edit

Can't you use the syntax: " $a.each {|$index, $value| ... }"

doc75 gravatar imagedoc75 ( 2014-01-14 14:34:50 -0500 )edit

That doesn't work as puppet 2.7 any other possibility?

jaksky gravatar imagejaksky ( 2014-01-15 14:12:32 -0500 )edit

Sorry, I am on Puppet 3. I think you need to create your own Puppet function. Do you need some adivce to do so ?

doc75 gravatar imagedoc75 ( 2014-01-15 14:30:29 -0500 )edit

I was hoping in some standard solution e.g. stdlib or so. Seems there is no choice. If you can guide me would be great as I have very little ...(more)

jaksky gravatar imagejaksky ( 2014-01-15 14:47:42 -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: 2014-01-14 04:51:03 -0500

Seen: 1,111 times

Last updated: Jan 15 '14