What is the recommended way of getting a list of all the nodes from within a Puppet template?
Let's say I have a load balancer running Nginx. Inside of the template, I want to generate a list of backend servers. For the sake of simplicity, let's say these app-servers have a hostname that matches 'appserver*'.
Currently, I define an array inside site.pp with each app-server, and use that as follows:
upstream www.foobar.com {
<% appservers.each do |val| -%>
server <%= val %>:80;
<% end -%>
}
I'd like to do away with that manually defined array, and instead query Puppet.
I don't think there is one recommended way. It depends on your setup I guess.
There are several ways to achieve this. You can use exported resources and query puppetDB.
The method I prefer, because I'm already using Foreman, is to use foremans parser function to get the nodes list from foreman.
So in your manifest, you can do something like:
$appservers = foreman("hosts", 'hostgroup = appservers')
Assuming your app-servers are in Foremans hostgroup calles 'appservers'. Here is a blog post that explains it.
Another way, would be to store your 'appserver' list in a text file, or database, then write your own function to retrieve the list. As you can see, writing a parser function is easy.
Another way is to use Hiera.