How to refer to exported resource parameters in a template

39 views
Skip to first unread message

Matthew Pounsett

unread,
Aug 8, 2016, 4:55:38 PM8/8/16
to Puppet Users

I'm using puppet 4.4.1.  I have a case where I need the IP addresses of all the servers that fit a certain set of criteria to appear in a YAML list in a config file on a particular host.

It seems like the first step would be to create an exported resource with the required data in it, but I haven't yet found a way to refer to those data from inside a template (or from inside a module in order to populate a variable I can use in a template).   It seems like this would be a common pattern, but the closest examples I've been able to find are for realizing the exported resource to create a file or other resource on a system, rather than incorporate some of the exported resource's data in other resources.

How do people normally go about doing something like this?  Are there some examples I've just failed to find?





Christopher Wood

unread,
Aug 8, 2016, 5:02:39 PM8/8/16
to puppet...@googlegroups.com
On Mon, Aug 08, 2016 at 11:28:55AM -0700, Matthew Pounsett wrote:
> I'm using puppet 4.4.1.  I have a case where I need the IP addresses of
> all the servers that fit a certain set of criteria to appear in a YAML
> list in a config file on a particular host.

Been there for an activemq.xml config file, ended up using Erik Dalén's puppetdbquery.

https://forge.puppet.com/dalen/puppetdbquery

This in a manifest:

$qf = query_facts('id=root', ['owner', 'datacenter'])

This in a template used in that manifest:

<%
list = {}
@qf.values.each do |h|
if h['owner']
owner = h['owner']
list[owner] = 1
end
if h['datacenter']
datacenter = h['datacenter']
list[datacenter] = 1
end
end
-%>

You get the idea, could output yaml there or whatever you wanted really.

> It seems like the first step would be to create an exported resource with
> the required data in it, but I haven't yet found a way to refer to those
> data from inside a template (or from inside a module in order to populate
> a variable I can use in a template).   It seems like this would be a
> common pattern, but the closest examples I've been able to find are for
> realizing the exported resource to create a file or other resource on a
> system, rather than incorporate some of the exported resource's data in
> other resources.
> How do people normally go about doing something like this?  Are there some
> examples I've just failed to find?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [1]puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> [2]https://groups.google.com/d/msgid/puppet-users/d60d5c64-babe-4b46-8529-ceac74cf576b%40googlegroups.com.
> For more options, visit [3]https://groups.google.com/d/optout.
>
> References
>
> Visible links
> 1. mailto:puppet-users...@googlegroups.com
> 2. https://groups.google.com/d/msgid/puppet-users/d60d5c64-babe-4b46-8529-ceac74cf576b%40googlegroups.com?utm_medium=email&utm_source=footer
> 3. https://groups.google.com/d/optout

Matthew Pounsett

unread,
Aug 8, 2016, 5:36:01 PM8/8/16
to Puppet Users, christop...@pobox.com


On Monday, 8 August 2016 14:02:39 UTC-7, Christopher Wood wrote:
On Mon, Aug 08, 2016 at 11:28:55AM -0700, Matthew Pounsett wrote:
>    I'm using puppet 4.4.1.  I have a case where I need the IP addresses of
>    all the servers that fit a certain set of criteria to appear in a YAML
>    list in a config file on a particular host.

Been there for an activemq.xml config file, ended up using Erik Dalén's puppetdbquery.

https://forge.puppet.com/dalen/puppetdbquery

That looks like it quite neatly solves the problem.
Thanks!

 

jcbollinger

unread,
Aug 9, 2016, 9:12:33 AM8/9/16
to Puppet Users


On Monday, August 8, 2016 at 3:55:38 PM UTC-5, Matthew Pounsett wrote:

I'm using puppet 4.4.1.  I have a case where I need the IP addresses of all the servers that fit a certain set of criteria to appear in a YAML list in a config file on a particular host.

It seems like the first step would be to create an exported resource with the required data in it, but I haven't yet found a way to refer to those data from inside a template (or from inside a module in order to populate a variable I can use in a template).

You've already received a response in that general direction, but I want to observe that the underlying idea is unnatural for Puppet.  Yes, exported resources are a good fit to the task, but resources are not well suited to be vehicles for communication and data storage within the master.  Rather, their role and purpose is to encapsulate details of machine state in a manner that can be applied to the (a) target machine.

Thus, if you use exported resources for your purpose, the natural approach is for the various machines to export resources that serve their purpose by being applied to the target machine.  Since you want all of them to contribute to a single list in a single file, you're looking for a resource type that represents a piece of a file.  No such resource type is included in the Puppet core, but this is the bread & butter of the widely-used Concat module.


  It seems like this would be a common pattern, but the closest examples I've been able to find are for realizing the exported resource to create a file or other resource on a system, rather than incorporate some of the exported resource's data in other resources.


Yes, because applying resources to a node is the natural and intended usage of resources.  The trick is to choose the right resources for the job.

 

How do people normally go about doing something like this?  Are there some examples I've just failed to find?

 

Some people do hack together means to do the job more or less as you initially envisioned, but don't confuse mechanism with result.  I would not be surprised if you had seen examples of exactly what I describe, without recognizing that they, or something like them, would serve your purpose perfectly well.


John

Matthew Pounsett

unread,
Aug 9, 2016, 11:33:32 AM8/9/16
to Puppet Users


On Tuesday, 9 August 2016 06:12:33 UTC-7, jcbollinger wrote:


Some people do hack together means to do the job more or less as you initially envisioned, but don't confuse mechanism with result.  I would not be surprised if you had seen examples of exactly what I describe, without recognizing that they, or something like them, would serve your purpose perfectly well.

Using concat for this purpose represents the worst of ugly hacks to get around a missing feature.   I've been forced to use it before for similar purposes.. and in any kind of large infrastructure it can quickly become an unmanageable collection of thousands of files being concatenated into a single (or small number of) config files.

If accessing the values in exported resources is such anathema, then perhaps the puppet core aught to consider including a function that exposes facts across nodes.
Reply all
Reply to author
Forward
0 new messages