Skip to content

Commit 719efae

Browse files
committed
Add server facts when looking up values
During normal catalog compilation, server facts are added by the `compiler` terminus prior to calling `Puppet::Parser::Compiler.compile`[1]. However, the lookup application directly calls `Compiler.compile`, bypassing the `compiler` terminus[2]. Therefore, server facts weren't being added when running the lookup command. Ideally, catalog compilation and the lookup command would compile the catalog in the same way, but changing that is risky. For that to work, we would need to pass the already resolved node and facts to the `compiler` terminus and the terminus would need to add server facts to the node. However, the terminus doesn't add server facts if the node is passed in. It only does that if it resolves the node using the indirector[3]. Rather than mess with the terminus and break compilation, just load server facts in the same way that the `compiler` terminus does. [1] https://github.com/puppetlabs/puppet/blob/8.7.0/lib/puppet/indirector/catalog/compiler.rb#L56 [2] https://github.com/puppetlabs/puppet/blob/8.7.0/lib/puppet/application/lookup.rb#L407 [3] https://github.com/puppetlabs/puppet/blob/8.7.0/lib/puppet/indirector/catalog/compiler.rb#L390
1 parent 8068bce commit 719efae

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

lib/puppet/application/lookup.rb

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_relative '../../puppet/application'
44
require_relative '../../puppet/pops'
55
require_relative '../../puppet/node'
6+
require_relative '../../puppet/node/server_facts'
67
require_relative '../../puppet/parser/compiler'
78

89
class Puppet::Application::Lookup < Puppet::Application
@@ -403,6 +404,7 @@ def generate_scope
403404
end
404405
end
405406
node.environment = Puppet[:environment] if Puppet.settings.set_by_cli?(:environment)
407+
node.add_server_facts(Puppet::Node::ServerFacts.load)
406408
Puppet[:code] = 'undef' unless options[:compile]
407409
compiler = Puppet::Parser::Compiler.new(node)
408410
if options[:node]

spec/fixtures/unit/application/environments/production/data/common.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ ab: "%{hiera('a')} and %{hiera('b')}"
2020

2121
g: "This is%{facts.cx} in facts hash"
2222

23+
h: "server version is %{server_facts.serverversion}"
24+
2325
lookup_options:
2426
a: first

spec/unit/application/lookup_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,13 @@ def run_lookup(lookup)
546546
expect(run_lookup(lookup)).to eql("This is G from facts in facts hash")
547547
end
548548

549+
it 'looks up server facts' do
550+
lookup.options[:node] = node
551+
lookup.options[:render_as] = :s
552+
allow(lookup.command_line).to receive(:args).and_return(['h'])
553+
expect(run_lookup(lookup)).to eql("server version is #{Puppet.version}")
554+
end
555+
549556
describe 'when retrieving given facts' do
550557
before do
551558
lookup.options[:node] = node

0 commit comments

Comments
 (0)