Chef Inspec control status capture

I want to perform some action if my control fails, example, I may want to send an email or execute some ruby code, how can I capture the control execution result, I tried ruby begin rescue block but it did not work, how to do this

I would suggest a custom reporter plugin. You can perform the inspec run, then examine all the results and perform any Ruby code you wish based on the results. See inspec/ at master · inspec/inspec · GitHub for a development guide for plugins and inspec/lib/plugins/inspec-reporter-html2 at master · inspec/inspec · GitHub and GitHub - tecracer-chef/inspec-reporter-flex: Flexible, templating reporter for InSpec for examples of custom reporters. While both of those produce templated output, there is no reason why you couldn't take some other action, such as sending an email or connecting to a monitoring system.

Happy coding!

Thank you a lot for this, it helps, I wonder if I can access any object that stores the result, I mean I just want to do something write after describe or control block, writhing the control script itself. e.g.

describe comand(ls -l ) do
its stdout shoud eq abc
if Inspec.Object.status == failes
do something
or send custom message to HTML report

something like this will give us more freedom.

I'm afraid that really isn't possible in the way you suggest. InSpec executes the control code in multiple passes and so the results of the test are not available immediately to subsequent code. This is because InSpec is RSpec based - it preparses the tests, then runs the RSpec test runner, then processes the test results through the reporters. The test results are simply not known in the phase of script evaluation you are looking at.

Handling the results of failed controls is really best handled in the reporter phase of processing.

1 Like

thank you a lot for the reply, this clarifies the things.