patternMinor
Inspec tests conditional on OS version
Viewed 0 times
inspecversiontestsconditional
Problem
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.Solution
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:
Additionally, you can limit the entire inspec control with Chef-like hooks, such as
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('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
endAdditionally, 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? }Code Snippets
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
endonly_if { os.windows? }Context
StackExchange DevOps Q#5037, answer score: 3
Revisions (0)
No revisions yet.