Hi Raghu,
It sounds like you want to use the map function:
https://puppet.com/docs/puppet/6.17/function.html#map
Here's an example of how to use it with your data structure:
$array_inputs = $vm_data.map |$region| {
$hash_region = {
location => $region['location'],
vm_list => $region['vmlist'],
}
}
I have pulled out the following lines though, as the variable name
interpolation within a variable name is probably not going to work how
you're expecting it to:
customer_id => "customer-${$region['location']}",
api_key => "api-${$region['location']}",
secret_key => "secret-${$region['location']}",
For those lookups you are probably best off creating a separate lookup hash
perhaps like this:
$region_data = {
'apac' => {
'customer_id' => 'xxx',
'api_key' => 'yyy',
'secret_key' => 'zzz',
}
}
customer_id => "customer-${region_data['apac']['customer_id']}"
Cheers
Jesse
On Thu, 27 Aug 2020 at 14:01, Raghu Ram Baisani <[email protected]>
wrote:
>
> Thanks for the reply.
> I didn't understand what you are trying to say.
> The code is something like this:
> $array_inputs = []
> $vm_data.each |$region| {
> $hash_region = {
> location => $region['location'],
> vm_list => $region['vmlist'],
> customer_id => "customer-${$region['location']}",
> api_key => "api-${$region['location']}",
> secret_key => "secret-${$region['location']}",
> }
> $array_inputs << $hash_region
>
> }
> return $array_inputs
> }
> I'm not able to add a new hash into the array after every iteration.
> Can you please provide some resolution for this.
> Thanks in advance
> Raghuram Baisani
>
> On Thursday, August 27, 2020 at 3:12:11 AM UTC+5:30 [email protected]
> wrote:
>
>> Hi Raghu,
>>
>> Yes this is possible. Your example looks workable, but don't forget to
>> add commas after the values of the hash.
>>
>> Check out Puppet's 'reduce' function, it iterates over data and allows
>> you to return a new structure after all the iteration which could be your
>> new hash. Alternatively it might be worth writing something in Ruby if
>> Puppet's immutable variables get in the way which may happen if you're
>> heavily modifying your hash.
>>
>> On Thu, 27 Aug 2020, 3:45 am Raghu Ram Baisani, <[email protected]>
>> wrote:
>>
>>> Hi Guys
>>>
>>> Some one help me solving this issue.
>>> *Example:*
>>> [
>>> {
>>> name => raghuram,
>>> age => 22
>>> },
>>> {
>>> name => vinay
>>> age => 25
>>> }
>>> ]
>>> the idea is to iterate through an existing hash and create new hash with
>>> some modifications and add it to array.
>>> Thanks in advance
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Puppet Users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/puppet-users/32ee1353-0cd3-47fa-af66-c3ef976dcdean%40googlegroups.com
>>> <https://groups.google.com/d/msgid/puppet-users/32ee1353-0cd3-47fa-af66-c3ef976dcdean%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/79f3b6f2-bb70-492b-9818-6f35322c4573n%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/79f3b6f2-bb70-492b-9818-6f35322c4573n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
--
*Jesse Reynolds*
Principal Professional Services Engineer
Melbourne, Australia
mobile: +61 414 669 790
[email protected]
*Puppet <https://puppet.com/>*
--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/puppet-users/CAMwMGPMSAXS136suygK4zTiO-GHB_8aEAnF8cDcjyMBA9aUieQ%40mail.gmail.com.