I'm trying to update an existing mcollective inventory script. The script currently gathers information about available updates. I want to replace certain "true" values with markup that will produce a checkbox when copied into my wiki. Here is a simplified version (fewer fields) of my current script.
# patching_inventory.mc
inventory do
puts "||Server||Update Needed||Package Count||Kernel Release||"
format "|%s|%s|%s|%s|"
fields { [
identity,
facts["apt_has_updates"],
facts["apt_updates"],
facts["kernelrelease"]
] }
end
I want to replace the values in the Update Needed
column with {checkbox}done{checkbox}
, but only when update needed is true. Otherwise a placeholder (such as '-') will work. Output looks like this:
||Server||Update Needed||Package Count||Kernel Release||
|host1|true|26|3.20.96|
|host2|false|0|4.18.120|
|host3|true|109|3.21.17|
...
|host197|true|26|3.20.96|
And I want it to look like this:
||Server||Update Needed||Package Count||Kernel Release||
|host1|{checbox}done{checkbox}|26|3.20.96|
|host2|-|0|4.18.120|
|host3|{checbox}done{checkbox}|109|3.21.17|
...
|host197|{checbox}done{checkbox}|26|3.20.96|
My initial attempt was to do something like this:
inventory do
updates = (facts["apt_has_updates"] == 'true') ? "{checkbox}done{checkbox}" : '-'
puts "||Server||Update Needed||Package Count||Kernel Release||"
format "|%s|%s|%s|%s|"
fields { [
identity,
updates,
facts["apt_updates"],
facts["kernelrelease"]
] }
end
But it occurs to me that the inventory do
is maybe not iterating like my non-ruby mind assumed it would be. Yet somewhere, there must be iteration happening because the format string is used many times with different facts. Is there a way to tell the formatter to substitute values for each fact as I have attempted above?
Can any of you point me in the right direction?
After more exploring, it turns out I was just putting the ternary value conversion in the wrong place. It works fine if the value is placed in the list of fields. Here's my working code:
# patching_inventory.mc
inventory do
puts "||Server||Update Needed||Package Count||Kernel Release||"
format "|%s|%s|%s|%s|"
fields { [
identity,
facts["apt_has_updates"],
facts["apt_updates"] == "true" ? "{checkbox}done{checkbox}" : "-",
facts["kernelrelease"]
] }
end
I'll be honest, I'm not sure why that works... there must be some iterator that's evaluating the fields for each host in the inventory. If anyone has additional insight, feel free to post another answer.