Loop with two variables in puppet

294 views
Skip to first unread message

Jiří Červenka

unread,
Jul 27, 2012, 4:12:48 AM7/27/12
to puppet...@googlegroups.com
Hi,
I am stuck on this task. I need to generate a file with this content:
...
/bin/mount --bind /home/some/path/ /home/someuser/www
/bin/mount --bind /home/comple/tely/different/path/ /home/differentuser/www
/bin/mount --bind /home/another/path/ /home/anotheruser/www
.
.
...

For each row I need to insert two variables.

I am able to construct simple loop in erb file, but this does not solve my problem:

<% userss.each do |user| -%>
        /bin/mount --bind /some/path/<%= user %> /home/somepath/<%= user %>
<% end -%>

I need loop, whose content looks like this:

/bin/mount --bind /some/path/<%= path %> /home/somepath/<%= user %>

Can someone point me in right direction?

Thanks.

David Schmitt

unread,
Jul 27, 2012, 5:21:04 AM7/27/12
to puppet...@googlegroups.com

Unless you want to establish a proper data structure (array of pairs),
I'd suggest Array#zip:

http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-zip


Best Regards, D.
> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/puppet-users/-/AyvMd9vq4EAJ.
> To post to this group, send email to puppet...@googlegroups.com.
> To unsubscribe from this group, send email to
> puppet-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/puppet-users?hl=en.

Jochen Schalanda

unread,
Jul 27, 2012, 5:05:33 AM7/27/12
to puppet...@googlegroups.com
Hi Jiří,

you could use a simple hash to get the result you want. Using
Hash.each_pair [1] you can iterate over the keys and values in your hash
representing the mount points.

Example:

your_manifest.pp:

$mountpoints = { 'user1' => '/home/some/path', 'user2' =>
/home/another/path' }


your_template.erb:

<% mountpoints.each do |user, path| -%>
/bin/mount --bind <%= path %> /home/<%= user %>/www
<% end -%>


[1]: http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-each_pair


Best regards,
Jochen


On 27.07.2012 10:12, Jiří Červenka wrote:
Reply all
Reply to author
Forward
0 new messages