Ask Your Question

How can I collect exported resource parameters in an array?

asked 2015-01-19 09:23:12 -0600

Krist van Besien gravatar image

updated 2015-01-20 08:40:35 -0600

I export host resources like this:

@@host{ "${::fqdn}":
          ip  => $::ipaddress,
          tag => ['cluster']

Host <<| tag == 'cluster' |>>

This exports the host resource, on each node, with a tag "cluster". I then collect them, and build the /etc/hosts file on each node with this.

I however also want to collect the hostnames in an array, to pass on to some other resource. So if for example the nodes server1, server2 and server3 all export a host resource, with tag "cluster" I want to collect them, and then create an array and end up with something like this:

cluster_members = [ 'server1'.'server2'.'server3']

How would I do this?

I though I could do something like:

Create a custom resource type:

define mod::lib::set_member ( $node_name )
  $mod::set_members += [$node_name]

Then on each node:

  node_name => $::fqdn

And on the node that needs to collec the list:

$mod::set_members = []

 mod::Lib::set_member <<|  |>> ->
 notify {"Replication Set containst ${mod::set_members}":}

I don't get any complaints, but it doesn't work either. The "notify" informs me that the array set_members is empty...

So again, how can I do this? I need to basically build an array varialble, that contains all the hostnames of hosts that have exported a particular resource. How do I do this?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2015-01-20 11:14:04 -0600

Krist van Besien gravatar image

I've managed to almost solve this myself in a way, but I am still having an issue.

The approach I'm following is using concat to create a custom fact in /etc/facter/facts.d. I used the following code:

@@concat::fragment {"member_${replica}_${::fqdn}":
    target => $factfile,
    content => "${::fqdn},",
    tag => $replica,
    order => 3

  concat { $factfile:
    ensure => present

  concat::fragment {"memberset":
    target => $factfile,
    content => "memberset=",
    order => 1 

  Concat::Fragment <<| tag == $replica |>>

($factfile is the location of the custom fact file...)

edit flag offensive delete link more

answered 2015-03-02 11:02:33 -0600

Nick gravatar image

updated 2015-03-04 07:07:56 -0600

We had this same problem and we solved it using the puppetdbquery module which can query PuppetDB for your exported resources. We then call something like this to get the FQDN of all nodes with a matching tag.

$fqdns = sort(query_nodes("Host { tag = \"${tagname}\" }", 'fqdn'))

Having said that, I just remembered that I had to modify this module to get it to query on tags properly. You can see the pull request here and use that patched version if you want, or otherwise find something other than tag that you can query on. (UPDATE: the pull request has now been merged, so as long as you use the latest version of the module you should be good.)

edit flag offensive delete link more

answered 2015-01-19 11:47:46 -0600

anthonyclark gravatar image

I haven't tried this myself, but it may be possible to push each host name to an array when the collector is realized.

This may help with that task:

Otherwise, Puppet 4 will apparently be able to manage arrays better.

edit flag offensive delete link more


So I can can create a resource type and do a $::clustermembers += $name within each exported resource?

Krist van Besien gravatar imageKrist van Besien ( 2015-01-20 03:14:56 -0600 )edit

I now tried this, and it doesn work. What should I do?

Krist van Besien gravatar imageKrist van Besien ( 2015-01-20 08:33:08 -0600 )edit

I honestly don't know :( I wish I could help further and test this out myself, but I've got too many work deadlines right now :(

anthonyclark gravatar imageanthonyclark ( 2015-01-20 09:44:49 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-01-19 09:23:12 -0600

Seen: 4,572 times

Last updated: Mar 04 '15