Dynamic erb in puppet classes?

Hi guys,

My google skills haven't been finding me much…

I'm wondering if it's possible to loop through and list all the hosts in a
hostgroup to be dumped into an array for a puppet class. I found
this Feature #2260: Support dynamic ERB parameters in global and class params - Foreman and the static values seem
to work, but I'm hoping someone knows of a way to build the array. It would
prove to be super useful!

Cheers,
Andrew.

Use the foreman puppet fucntion to query foreman from your puppet
manifest. If you use the version at [1] then it would be something
like

$hosts = { item => 'hosts',
search => 'hostgroup=foo',
foreman_url => 'https://foreman' }

That will get you a hash of data, which you'll want to process in some
way - templates are usually easiest. For just the the hostnames, a
simple loop would do I guess:

<% hosts.each do |host,data| -%><%= host.name %><% end -%>

Have a play, it's pretty powerful.
Greg

[1] https://github.com/theforeman/puppet-foreman/blob/master/lib/puppet/parser/functions/foreman.rb

··· On 2 March 2014 20:35, Andrew Lau wrote: > Hi guys, > > My google skills haven't been finding me much.. > > I'm wondering if it's possible to loop through and list all the hosts in a > hostgroup to be dumped into an array for a puppet class. I found this > http://projects.theforeman.org/issues/2260 and the static values seem to > work, but I'm hoping someone knows of a way to build the array. It would > prove to be super useful!

Hi Greg,

Thanks for pointing me in the right direction - much appreciated.

I'm a little new with puppet code, so it'd be really great if you could
just verify my logic. I'm assuming I would need to include the custom
parser function into my manifest with the same hierarchy.

Define in the init

$hosts = { item => 'hosts',
search => 'hostgroup=foo',
foreman_url => 'https://foreman' }

$foo = foreman($hosts)

Then in my foreman puppetclasses, I could just drop:
<% foo.each do |foo,data| -%><%= foo.name <http://host.name/> %><% end -%>

Cheers

Andrew.

··· On Mon, Mar 3, 2014 at 9:48 AM, Greg Sutcliffe wrote:

On 2 March 2014 20:35, Andrew Lau andrew@andrewklau.com wrote:

Hi guys,

My google skills haven’t been finding me much…

I’m wondering if it’s possible to loop through and list all the hosts in
a
hostgroup to be dumped into an array for a puppet class. I found this
Feature #2260: Support dynamic ERB parameters in global and class params - Foreman and the static values seem to
work, but I’m hoping someone knows of a way to build the array. It would
prove to be super useful!

Use the foreman puppet fucntion to query foreman from your puppet
manifest. If you use the version at [1] then it would be something
like

$hosts = { item => ‘hosts’,
search => ‘hostgroup=foo’,
foreman_url => ‘https://foreman’ }

That will get you a hash of data, which you’ll want to process in some
way - templates are usually easiest. For just the the hostnames, a
simple loop would do I guess:

<% hosts.each do |host,data| -%><%= host.name %><% end -%>

Have a play, it’s pretty powerful.
Greg

[1]
https://github.com/theforeman/puppet-foreman/blob/master/lib/puppet/parser/functions/foreman.rb


You received this message because you are subscribed to a topic in the
Google Groups “Foreman users” group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/foreman-users/OGPqXM13yGM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at http://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/groups/opt_out.

Other than using parser=future, Puppet's handling of hashes isn't
great - the second code snippet (the ERB one) was intended for a
template, since you get the full power of Ruby in a template (via
ERB). If you need to do something in the manifest itself, you'll need
to manipulate the hash without using ERB, which is usually painful - I
frequently resort to using the inline_template function in my
manifests to get what I want.

Greg

You've lost me a little. So, what I would have to do is:

  1. Import foreman parser function into my puppet module

  2. Write a template using your snippet eg.
    $hosts = { item => 'hosts',
    search => 'hostgroup=foo',
    foreman_url => 'https://foreman' }
    <% hosts.each do |host,data| -%><%= host.name %><% end -%>

  3. Use an inline template to bring this into puppet

You aren't talking about foreman templates or using ERB in the
puppetclass->smart_variable functions?

Thanks.

··· On Mon, Mar 3, 2014 at 8:28 PM, Greg Sutcliffe wrote:

Other than using parser=future, Puppet’s handling of hashes isn’t
great - the second code snippet (the ERB one) was intended for a
template, since you get the full power of Ruby in a template (via
ERB). If you need to do something in the manifest itself, you’ll need
to manipulate the hash without using ERB, which is usually painful - I
frequently resort to using the inline_template function in my
manifests to get what I want.

Greg


You received this message because you are subscribed to a topic in the
Google Groups “Foreman users” group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/foreman-users/OGPqXM13yGM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
foreman-users+unsubscribe@googlegroups.com.
To post to this group, send email to foreman-users@googlegroups.com.
Visit this group at http://groups.google.com/group/foreman-users.
For more options, visit https://groups.google.com/groups/opt_out.

> You've lost me a little. So, what I would have to do is:

> 1. Import foreman parser function into my puppet module

Whether or not the class is in Foreman won't affect the functioning of
your class, but obviously you need to do this if you want to apply it
via the ENC :slight_smile:

> 2. Write a template using your snippet eg.
> $hosts = { item => 'hosts',
> search => 'hostgroup=foo',
> foreman_url => 'https://foreman' }

This bit is Puppet code and should be in a .pp file

> <% hosts.each do |host,data| -%><%= host.name %><% end -%>

This bit is ERB and needs to be used in a template (either a real one
or via inline_template()

> 3. Use an inline template to bring this into puppet

See above, this is only if you need the processed data in the .pp
file, otherwise do it all in your template.

> You aren't talking about foreman templates or using ERB in the
> puppetclass->smart_variable functions?

Correct.

Consider this code snippet:

foo/manifests/init.pp
class foo {
$hosts = { item => 'hosts',
search => 'hostgroup=foo',
foreman_url => 'https://foreman'
}
file { '/tmp/foo':
content => template('foo/content.erb')
}
}

foo/templates/content.erb
<% hosts.each do |host, data| -%>
<%= host %>
<%= data.inspect %>

··· On 3 March 2014 09:59, Andrew Lau wrote: ---- <% end -%>

Hopefully you can see how /tmp/foo will contain a pretty-printed
version of the data that came from Foreman, which you can then decide
which bits you need.

Greg

Thanks a lot for the run down. Time to take this for a spin

Cheers

··· On Mon, Mar 3, 2014 at 9:10 PM, Greg Sutcliffe wrote: > On 3 March 2014 09:59, Andrew Lau wrote: >> You've lost me a little. So, what I would have to do is: > >> 1. Import foreman parser function into my puppet module > > Whether or not the class is in Foreman won't affect the functioning of > your class, but obviously you need to do this if you want to apply it > via the ENC :) > >> 2. Write a template using your snippet eg. >> $hosts = { item => 'hosts', >> search => 'hostgroup=foo', >> foreman_url => 'https://foreman' } > > This bit is Puppet code and should be in a .pp file > >> <% hosts.each do |host,data| -%><%= host.name %><% end -%> > > This bit is ERB and needs to be used in a template (either a real one > or via inline_template() > >> 3. Use an inline template to bring this into puppet > > See above, this is only if you need the processed data in the .pp > file, otherwise do it all in your template. > >> You aren't talking about foreman templates or using ERB in the >> puppetclass->smart_variable functions? > > Correct. > > Consider this code snippet: > > foo/manifests/init.pp > class foo { > $hosts = { item => 'hosts', > search => 'hostgroup=foo', > foreman_url => 'https://foreman' > } > file { '/tmp/foo': > content => template('foo/content.erb') > } > } > > foo/templates/content.erb > <% hosts.each do |host, data| -%> > <%= host %> > <%= data.inspect %> > ---- > <% end -%> > > Hopefully you can see how /tmp/foo will contain a pretty-printed > version of the data that came from Foreman, which you can then decide > which bits you need. > > Greg > > -- > You received this message because you are subscribed to a topic in the Google Groups "Foreman users" group. > To unsubscribe from this topic, visit https://groups.google.com/d/topic/foreman-users/OGPqXM13yGM/unsubscribe. > To unsubscribe from this group and all its topics, send an email to foreman-users+unsubscribe@googlegroups.com. > To post to this group, send email to foreman-users@googlegroups.com. > Visit this group at http://groups.google.com/group/foreman-users. > For more options, visit https://groups.google.com/groups/opt_out.