It's important to understand that |ruby| code used in custom resources and controls DSL is executed on the system that runs |inspec|. This allows |inspec| to work without |ruby| and rubygems being required on the target(node or container).
For example, parsing a |csv| file like this to check the content:
..code-block:: ruby
require 'CSV'
control 'check-interns-group' do
impact 0.8
title 'Ensure interns are assigned to the correct group'
The |ruby| code can be slightly changed in order to parse the |csv| content from the remote target. This is accomplished using the |inspec| file resource that retrieves the content of the file from the target.
..code-block:: ruby
require 'CSV'
control 'check-interns-group' do
impact 0.8
title 'Ensure interns are assigned to the correct group'
You can also use |ruby| variables and logic to instantiate an |inspec| resource once. For example, run a command and use the content in multiple tests:
..code-block:: ruby
control 'check-perl' do
impact 0.3
title 'Check perl compiled options and permissions'
perl_out = command('perl -V')
#require 'pry'; binding.pry;
describe perl_out do
its('exit_status') { should eq 0 }
its('stdout') { should match (/USE_64_BIT_ALL/) }
its('stdout') { should match (/useposix=true/) }
its('stdout') { should match (/-fstack-protector/) }
An **advanced** but very useful |ruby| tip. In the previous example, I commented out the ``require 'pry'; binding.pry;`` line. If you remove the ``#`` prefix and run the control, the execution will stop at that line and give you a ``pry`` shell. From this ``pry`` shell, you can print variables, see methods available, etc. For the above example: