What is the idiomatic way to tell Inspec to run test X on OS version A and test Y on OS version B? I'm aware of the technique of dumping the Chef node object to a JSON file in /tmp but that isn't doing it for me as that runs within the Test Kitchen VM whereas Inspec executes on the real host and therefore can't see it.
Asked
Active
Viewed 2,838 times
3
Gaius
- 1,096
- 10
- 18
1 Answers
3
What I would suggest, like comments under your post, is to take this to the #inspec channel on the Chef Community Slack to ask there. That said, here's how I might attack this:
control 'some-control-slug' do
title 'My control'
impact 1.0
desc 'Example code for Stack Exchange'
if os.windows? && ::Gem::Version.new(os.release) < ::Gem::Version.new('6.1')
# Only executed on Windows machines older than 2008r2
describe directory('C:/Users') do
it { should exist }
end
elsif os.windows? && ::Gem::Version.new(os.release) >= ::Gem::Version.new('10')
# Only executed on Windows Server 2016 or newer
describe directory('D:/MyFolder') do
it { should_not exist }
end
end
# Executed on all host types
describe sys_info do
its('hostname') { should match(/SomeAwesomeHost/i) }
end
end
Additionally, you can limit the entire inspec control with Chef-like hooks, such as only_if. The above example code, as written, may execute on a Linux target. We could limit all of them by placing the following up near the desc or title:
only_if { os.windows? }
TheLonelyGhost
- 146
- 3