Ask Your Question

How to merge arrays into one hash

asked 2013-09-23 06:46:37 -0500

Luke K gravatar image

Dear all,

I would like to create a bunch of numerated users (i.e. user001, user002, etc) with set UIDs. I can easily generate arrays for both user names and UIDs using 'range' from puppetlabs/stdlib:

$account_number_start    = '000'
$account_number_end      = '001'
$user_ID_number_start    = 90000
$user_ID_number_end      = 90001
$users     = range("${account_prefix}${account_number_start}","${account_prefix}${account_number_end}")
$uids      = range("${user_ID_number_start}", "${user_ID_number_end}")

My first attempt to create the users failed:

user { $title:
    ensure     => present,
    name       => $users,
    uid        => $uids,

It creates all users with the same UID. I assume that what happens is that it tries to create all users for the first UID, then for the second, etc ... (more)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-09-23 08:11:11 -0500

Luke K gravatar image

In the end I had to write my own Puppet function (maybe there is an easier option?). The function is in {$module}/lib/puppet/parser/functions/createaccounthash.rb

module Puppet::Parser::Functions
  newfunction(:create_account_hash, :type => :rvalue, :doc => <<-'ENDOFDOC'
This function takes an array of users and an array of UIDs and combines them in the form
{username => {uid => $uid}}
  create_account_hash([user1, user2], [1111, 1112])
{user1 => {uid => 1111}, user2 => {uid => 1112}}

  ) do |arguments|

    require 'rubygems'
    require 'etc'

    # Check that both args are arrays.
    unless usernames.is_a?(Array) and uids.is_a ...
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


Asked: 2013-09-23 06:46:37 -0500

Seen: 307 times

Last updated: Sep 23 '13