Use Foreman API in puppet manifest

At work we use foreman for system installation and environment management. Sometimes it is necessary to get a list of hosts with a specific puppet class installed. In puppet itself it is not really easy to get the result, because you need to configure and setup your own exported resources to do that. Foreman itself has a much easier way. Its own puppet foreman function from Foreman Puppet modules.

Now it is quite easy to get hosts according their puppet classes:


    $hosts = foreman({item => 'hosts', search => "class=exampleClass and domain=${domain}", foreman_url => "http://${foreman}"})

The variable hosts then looks like:


    {"hostA":{"name": "XXXX", ... }, "hostB": {"name": "YYYY", ....} }

Now you have a dict of hosts and can iterate over it in your template like in the following example:


    <%=
    @hosts.each do |key|
      key["host"]["name"]
    end
    %>
comments powered by Disqus