From 719efae576a1fa4730ed579db8fbcd79b48a74d5 Mon Sep 17 00:00:00 2001 From: Josh Cooper Date: Tue, 11 Jun 2024 22:30:23 -0700 Subject: [PATCH] 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 --- lib/puppet/application/lookup.rb | 2 ++ .../application/environments/production/data/common.yaml | 2 ++ spec/unit/application/lookup_spec.rb | 7 +++++++ 3 files changed, 11 insertions(+) diff --git a/lib/puppet/application/lookup.rb b/lib/puppet/application/lookup.rb index 384c99f2c5b..e250725ed18 100644 --- a/lib/puppet/application/lookup.rb +++ b/lib/puppet/application/lookup.rb @@ -3,6 +3,7 @@ require_relative '../../puppet/application' require_relative '../../puppet/pops' require_relative '../../puppet/node' +require_relative '../../puppet/node/server_facts' require_relative '../../puppet/parser/compiler' class Puppet::Application::Lookup < Puppet::Application @@ -403,6 +404,7 @@ def generate_scope end end node.environment = Puppet[:environment] if Puppet.settings.set_by_cli?(:environment) + node.add_server_facts(Puppet::Node::ServerFacts.load) Puppet[:code] = 'undef' unless options[:compile] compiler = Puppet::Parser::Compiler.new(node) if options[:node] diff --git a/spec/fixtures/unit/application/environments/production/data/common.yaml b/spec/fixtures/unit/application/environments/production/data/common.yaml index 88b20ad3a45..453775d84ae 100644 --- a/spec/fixtures/unit/application/environments/production/data/common.yaml +++ b/spec/fixtures/unit/application/environments/production/data/common.yaml @@ -20,5 +20,7 @@ ab: "%{hiera('a')} and %{hiera('b')}" g: "This is%{facts.cx} in facts hash" +h: "server version is %{server_facts.serverversion}" + lookup_options: a: first diff --git a/spec/unit/application/lookup_spec.rb b/spec/unit/application/lookup_spec.rb index 00842837176..1a1cd54169e 100644 --- a/spec/unit/application/lookup_spec.rb +++ b/spec/unit/application/lookup_spec.rb @@ -546,6 +546,13 @@ def run_lookup(lookup) expect(run_lookup(lookup)).to eql("This is G from facts in facts hash") end + it 'looks up server facts' do + lookup.options[:node] = node + lookup.options[:render_as] = :s + allow(lookup.command_line).to receive(:args).and_return(['h']) + expect(run_lookup(lookup)).to eql("server version is #{Puppet.version}") + end + describe 'when retrieving given facts' do before do lookup.options[:node] = node