| Josh Cooper Gheorghe Popescu I would like to point out some advantages Facter 4 structured custom/external facts have:
- they can override core facts at any level of the fact hierarchy
e.g.
Facter.add('os.name', type: :structured) do |
has_weight(10) |
setcode do |
'my_custom_name' |
end |
end
|
will override the core fact `os.name`.
os => { |
architecture => "x86_64", |
family => "Darwin", |
hardware => "x86_64", |
macosx => { |
build => "20D74", |
product => "macOS", |
version => { |
full => "11.2.1", |
major => "11.2", |
minor => "1" |
} |
}, |
name => "my_custom_name", |
release => { |
full => "20.3.0", |
major => "20", |
minor => "3" |
} |
}
|
- they can augment any existing core/external/custom fact
e.g.
os => { |
architecture => "x86_64", |
family => "Darwin", |
hardware => "x86_64", |
macosx => { |
build => "20D74", |
product => "macOS", |
version => { |
full => "11.2.1", |
major => "11.2", |
minor => "1" |
} |
}, |
name => "Darwin", |
release => { |
full => "20.3.0", |
major => "20", |
minor => "3" |
} |
}
|
can be augmented with
Facter.add('os.popularity', type: :structured) do |
has_weight(10) |
setcode do |
'3' |
end |
end
|
and the result will be:
os => { |
architecture => "x86_64", |
family => "Darwin", |
hardware => "x86_64", |
macosx => { |
build => "20D74", |
product => "macOS", |
version => { |
full => "11.2.1", |
major => "11.2", |
minor => "1" |
} |
}, |
name => "Darwin", |
popularity => "3", |
release => { |
full => "20.3.0", |
major => "20", |
minor => "3" |
} |
}
|
- they can be blocked/cached individually
e.g. if we have
Facter.add('my_org.fact1', type: :structured) do |
has_weight(10) |
setcode do |
'1111111111' |
end |
end |
|
Facter.add('my_org.fact2', type: :structured) do |
has_weight(10) |
setcode do |
'2222222222' |
end |
end |
|
Facter.add('my_org.fact3', type: :structured) do |
has_weight(10) |
setcode do |
'3333333333' |
end |
end
|
we can configure facter to block/cache each fact
facts : { |
blocklist : [ "my_org.fact1" ], |
ttls : [ |
{ "my_org.fact2": 30 days } |
] |
}
|
- they provide a way to break your code in multiple units that are independent. If one fact fails, it will not affect the others
Facter.add('my_org.fact1', type: :structured) do |
has_weight(10) |
setcode do |
'1111111111' |
end |
end |
|
Facter.add('my_org.fact2', type: :structured) do |
has_weight(10) |
setcode do |
nil.size? |
end |
end |
|
Facter.add('my_org.fact3', type: :structured) do |
has_weight(10) |
setcode do |
'3333333333' |
end |
end
|
the result will be
my_org => { |
fact1 => "1111111111", |
fact3 => "3333333333" |
}
|
in contrast if we have
Facter.add('my_org') do |
has_weight(10) |
setcode do |
{ |
"fact1" => "1111111111", |
"fact3" => nil.size, |
"fact3" => "3333333333" |
} |
end |
end
|
we will get no `my_org` fact, although `my_org.fact1` and `my_org.fact3` can be resolved. Of course if we want this behaviour we can still structure facts like this and then they all get resolved or, if one fails, none get resolved. |