Search code examples
puppeterb

Puppet iteration from external file


I'm new to configuration management, just FYI.

I'm trying to puppetize elasticsearch, and want to have a master list of elasticsearch nodes in a file (which can be used for multiple things, not just this purpose).

I would like to add elasticsearch.yml via an ERB template and expand the list of FDQN's into the discovery.zen.ping.unicast.hosts: [] param.

For example I have an external file called es_hosts in module/files that contains:

host1.domain.com 
host2.domain.com 
host3.domain.com 
host4.domain.com

Then when puppet builds the ERB template have this in the param:

discovery.zen.ping.unicast.hosts: ["host1.domain.com", "host2.domain.com", "host3.domain.com", "host4.domain.com"]

I've tried a few things, but I can't get my head wrapped around it.

I would be using this list for other things like building firewall rules, etc, so I'd like to have one master list for reference that can be updated by my team.

Thanks for any help!


Solution

  • Rather than have a list in a file, it would be better to have it in Hiera, since defining lists and other external data is specifically what Hiera is for.

    (If you have not used Hiera yet, you definitely should read up on it.)

    So in Hiera you would have:

    ---
    es_hosts:
    - host1.domain.com 
    - host2.domain.com 
    - host3.domain.com 
    - host4.domain.com
    

    In your manifest, you would read that in from Hiera using the hiera function:

    $es_hosts = hiera('es_hosts')
    

    (Note that instead of the hiera function, we often use Puppet's Automatic Parameter Lookup feature instead to read data into our manifests from Hiera, but your requirement here - a list of ES hosts to be used in multiple contexts - suggests you will want this list not to be bound to a specific class input. If this does not make sense to you right now, you will need to learn about Parameterised Classes and Automatic Parameter Lookup, but it's otherwise not relevant to this answer.)

    Finally, in your ERB template you would have:

    discovery.zen.ping.unicast.hosts: ["<%= @es_hosts.join('", "') %>"]
    

    Pay attention to the fact that the $es_hosts variable from your manifest is accessed via a Ruby instance variable @es_hosts in your ERB template.

    Finally, note that there is an Elasticsearch Puppet module available on the Puppet Forget here. You may find that using that module is better than writing your own.