1

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?

vastlysuperiorman
  • 1,694
  • 19
  • 27
  • "I want to replace the values in the Update Needed column with {checkbox}done{checkbox}, but only when update needed is true." It appears that what you are really trying to do is replace the values with `-`, but only when the fact is false. Is your description your intent or is your code your intent? – Matthew Schuchard Feb 03 '17 at 12:33
  • @MattSchuchard The line `updates = (condition) ? "{checkbox}done{checkbox}" : "-"` uses the ternary operator. It would set `updates` equal to either the checkbox macro or a hyphen depending on the stated condition. – vastlysuperiorman Feb 03 '17 at 16:18
  • Ok so your intent is what the code is doing. With that information, I can tell you that your `do` is not iterating over anything, but merely specifying `inventory` as a code block so you can elaborate on its functionality. What would be really helpful here is if you showed what your desired output is so we don't have to guess. – Matthew Schuchard Feb 03 '17 at 16:42
  • @MattSchuchard Ah, sorry. Edited with desired output. Hope that clarifies. – vastlysuperiorman Feb 03 '17 at 16:54
  • Cool, looks like you figured it out though. You probably had a variable scope issue and you seemed to have successfully debugged it. MCo inventory scripts are a rare and interesting question, so upvote for it. – Matthew Schuchard Feb 03 '17 at 19:44

1 Answers1

0

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.

vastlysuperiorman
  • 1,694
  • 19
  • 27