Sha256: 203cb8089d1eca0e2d85a1560570e4c161e252bc86e5c276298a55dae5c60f8b

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

Puppet::Parser::Functions.newfunction(:query_nodes, :type => :rvalue, :arity => -2, :doc => <<-EOT

  accepts two arguments, a query used to discover nodes, and a optional
  fact that should be returned.

  The query specified should conform to the following format:
    (Type[title] and fact_name<operator>fact_value) or ...
    Package["mysql-server"] and cluster_id=my_first_cluster

  The second argument should be single fact (this argument is optional)

EOT
) do |args|
  query, fact = args

  require 'puppet/util/puppetdb'
  # This is needed if the puppetdb library isn't pluginsynced to the master
  $LOAD_PATH.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..'))
  begin
    require 'puppetdb/connection'
  ensure
    $LOAD_PATH.shift
  end

  puppetdb = PuppetDB::Connection.new(Puppet::Util::Puppetdb.server, Puppet::Util::Puppetdb.port)
  if fact then
    query = puppetdb.parse_query query, :facts if query.is_a? String
    puppetdb.facts([fact], query).each_value.collect { |facts| facts[fact] }
  else
    query = puppetdb.parse_query query, :nodes if query.is_a? String
    puppetdb.query(:nodes, query).collect { |n| n['name'] }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby-puppetdb-1.6.1 lib/puppet/parser/functions/query_nodes.rb
ruby-puppetdb-1.6.0 lib/puppet/parser/functions/query_nodes.rb
ruby-puppetdb-1.5.3 lib/puppet/parser/functions/query_nodes.rb