Class: PuppetDB::Model::Node

Inherits:
Base
  • Object
show all
Defined in:
lib/puppetdb/model/node.rb

Overview

Parses Nodes from Puppetdb

All attributes returned by for the node from the puppetdb will be added as getter to the object

Examples:

print uptime for hardware

servers =  PuppetDB::Model::Node.all_hardware_servers.each do |server|
  puts server.certname
  puts server.uptime
end

get nodes by fact

PuppetDB::Model::Node.by_fact({'custom_fact' => 'FooBar'}).each do |server|
  puts server.certname
end

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

client, client=, query, request

Constructor Details

#initialize(attributes) ⇒ Node

parses the data returned by a puppetdb query of ‘node’ objects and parses it

Parameters:

  • attributes

    The data returned by a PuppetDB query



21
22
23
24
25
# File 'lib/puppetdb/model/node.rb', line 21

def initialize(attributes)
  @facts = {}
  attributes[:environment] = attributes[:catalog_environment]
  super(attributes)
end

Class Method Details

.allObject

get all nodes



90
91
92
# File 'lib/puppetdb/model/node.rb', line 90

def self.all
  query('nodes {}')
end

.all_hardware_serversObject

get all hardware servers



85
86
87
# File 'lib/puppetdb/model/node.rb', line 85

def self.all_hardware_servers
  query("nodes { inventory { facts.virtual = 'physical'  and facts.dmi.chassis.type != 'Desktop' }}")
end

.by_class(classname) ⇒ Object

get nodes with a specific class

Examples:

Nodes with ‘Environment’ class loaded

PuppetDB::Model::Node.by_class('Environment').each do |server|
  puts server.uptime
end

Parameters:

  • classname (String)

    The name of the Class to search the Nodes



60
61
62
# File 'lib/puppetdb/model/node.rb', line 60

def self.by_class(classname)
  query("nodes { resources { type = 'Class' and title = #{classname.dump} }}")
end

.by_fact(facts, regexp: false) ⇒ Object

get nodes by matching facts

Parameters:

  • facts (Hash)

    name value pair for matching facts. Subfacts can be separated by period(.)

  • regexp (Boolean) (defaults to: false)

    Defines if the matching is done with regexp. Default : False



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/puppetdb/model/node.rb', line 68

def self.by_fact(facts, regexp: false)
  # use strict comparator by default, because this is less performance intensive
  comparator = regexp ? '~' : '='

  filters = facts.map do |key, value|
    # Boolean values doesn't support regexp operator(~) and must not be escaped
    if [TrueClass, FalseClass].include?(value.class)
      "facts.#{key} = #{value}"
    else
      "facts.#{key} #{comparator} #{value.to_s.dump}"
    end
  end

  query("nodes { inventory { #{filters.join(' and ')} }}")
end

.get(**arguments) ⇒ Object

Get object from Puppet DB

Pass a list of key: val arguments as filters for the query.

Examples:

Get nodes in environment

PuppetDB::Model::Nodes.get(report_environment: production)

Regexp search in title

PuppetDB::Model::Node.get(title: '^foo.*bar', regexp: true)

Parameters:

  • regexp (Boolean)

    Set this to true for regular expression matching



38
39
40
# File 'lib/puppetdb/model/node.rb', line 38

def self.get(**arguments)
  super object: 'nodes', **arguments
end

Instance Method Details

#bios_versionObject

get the bios version



103
104
105
# File 'lib/puppetdb/model/node.rb', line 103

def bios_version
  fact('dmi.bios.version').value
end

#by_name(fqdn, regexp: false) ⇒ Object

get nodes by name

Parameters:

  • fqdn (String)

    FQDN to search

  • regexp (Boolean) (defaults to: false)

    Defines if the matching is done with regexp. Default : False



46
47
48
49
50
51
# File 'lib/puppetdb/model/node.rb', line 46

def by_name(fqdn, regexp: false)
  # use strict comparator by default, because this is less performance intensive
  comparator = regexp ? '~' : '='

  query("node { certname #{comparator} #{fqdn.dump}}")
end

#fact(name) ⇒ Object

get a fact associated with this node

Examples:

get dmi info from host

Bios = PuppetDB::Model::Node.by_name.fact('dmi.bios.).value


98
99
100
# File 'lib/puppetdb/model/node.rb', line 98

def fact(name)
  @facts[name] ||= Fact.new(certname: certname, name: name)
end

#uptimeObject

get the uptime



108
109
110
# File 'lib/puppetdb/model/node.rb', line 108

def uptime
  fact('system_uptime.uptime').value
end