Skip to content

Commit b84ab7d

Browse files
authored
Allow digging into fields of facts when they are a hash on old puppets (#656)
If a facts_to_tags tag looks like a hash path (eg: 'networking.domain'), dig that path into the hash and use the inner value for the tag. For backwards compatibility, if the name looks like a hash path but there's a fact that's an exact string match (eg: there's a fact named 'networking.domain') then that will be used for the tag. Note that on Puppet 6.0 and later `getvar` already behaves like this, so this only makes it work on old Puppets.
1 parent 9985be0 commit b84ab7d

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

functions/tag6.pp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ function datadog_agent::tag6(
1818
}
1919

2020
if $lookup {
21-
$value = getvar($tag_names)
21+
$match_as_string = $facts[$tag_names]
22+
if $match_as_string == undef {
23+
$value = $facts.dig(*split($tag_names, '[.]'))
24+
} else {
25+
$value = $match_as_string
26+
}
2227
if $value =~ Array {
2328
$tags = prefix($value, "${tag_names}:")
2429
} else {

spec/classes/datadog_agent_spec.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,18 +2177,28 @@
21772177
{
21782178
agent_major_version: 6,
21792179
puppet_run_reports: true,
2180-
facts_to_tags: ['osfamily', 'facts_array'],
2180+
facts_to_tags: ['osfamily', 'facts_array', 'facts_hash.actor.first_name', 'looks.like.a.path'],
21812181
}
21822182
end
21832183
let(:facts) do
21842184
{
2185-
operatingsystem: 'CentOS',
2186-
osfamily: 'redhat',
2187-
facts_array: ['one', 'two'],
2185+
'operatingsystem' => 'CentOS',
2186+
'osfamily' => 'redhat',
2187+
'facts_array' => ['one', 'two'],
2188+
'facts_hash' => {
2189+
'actor' => {
2190+
'first_name' => 'Macaulay',
2191+
'last_name' => 'Culkin',
2192+
},
2193+
},
2194+
'looks.like.a.path' => 'but_its_not',
21882195
}
21892196
end
21902197

2191-
it { is_expected.to contain_file('/etc/datadog-agent/datadog.yaml').with_content(%r{tags:\n- osfamily:redhat\n- facts_array:one\n- facts_array:two}) }
2198+
it do
2199+
is_expected.to contain_file('/etc/datadog-agent/datadog.yaml')
2200+
.with_content(%r{tags:\n- osfamily:redhat\n- facts_array:one\n- facts_array:two\n- facts_hash.actor.first_name:Macaulay\n- looks.like.a.path:but_its_not})
2201+
end
21922202
end
21932203

21942204
describe 'a5 ensure facts_array outputs a list of tags' do

0 commit comments

Comments
 (0)