Skip to content

Commit 32fa75d

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 (cherry picked from commit 719efae)
1 parent 48e13cc commit 32fa75d

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
@@ -1,6 +1,7 @@
11
require_relative '../../puppet/application'
22
require_relative '../../puppet/pops'
33
require_relative '../../puppet/node'
4+
require_relative '../../puppet/node/server_facts'
45
require_relative '../../puppet/parser/compiler'
56

67
class Puppet::Application::Lookup < Puppet::Application
@@ -406,6 +407,7 @@ def generate_scope
406407
node.add_extra_facts(given_facts) if given_facts
407408
end
408409
node.environment = Puppet[:environment] if Puppet.settings.set_by_cli?(:environment)
410+
node.add_server_facts(Puppet::Node::ServerFacts.load)
409411
Puppet[:code] = 'undef' unless options[:compile]
410412
compiler = Puppet::Parser::Compiler.new(node)
411413
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
@@ -543,6 +543,13 @@ def run_lookup(lookup)
543543
expect(run_lookup(lookup)).to eql("This is G from facts in facts hash")
544544
end
545545

546+
it 'looks up server facts' do
547+
lookup.options[:node] = node
548+
lookup.options[:render_as] = :s
549+
allow(lookup.command_line).to receive(:args).and_return(['h'])
550+
expect(run_lookup(lookup)).to eql("server version is #{Puppet.version}")
551+
end
552+
546553
describe 'when retrieving given facts' do
547554
before do
548555
lookup.options[:node] = node

0 commit comments

Comments
 (0)