<p>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:</p>

<pre><code>control 'some-control-slug' do
  title 'My control'
  impact 1.0
  desc 'Example code for Stack Exchange'

  if os.windows? &amp;&amp; ::Gem::Version.new(os.release) &lt; ::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? &amp;&amp; ::Gem::Version.new(os.release) &gt;= ::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
</code></pre>

<p>Additionally, you can limit the entire inspec control with Chef-like hooks, such as <code>only_if</code>. 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 <code>desc</code> or <code>title</code>:</p>

<pre><code>only_if { os.windows? }
</code></pre>

CC BY-SA 4.0
2018-11-20T22:49:51.573Z
2018-11-20T22:49:51.573Z
TheLonelyGhost
<p>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 <a href="http://www.hurryupandwait.io/blog/accessing-chef-node-attributes-from-kitchen-tests" rel="nofollow noreferrer">the technique</a> of dumping the Chef <code>node</code> object to a JSON file in <code>/tmp</code> but that isn't doing it for me as <em>that</em> runs within the Test Kitchen VM whereas <a href="https://www.inspec.io/docs/reference/ruby_usage/" rel="nofollow noreferrer">Inspec executes on the real host</a> and therefore can't see it.</p>

CC BY-SA 4.0
2018-09-24T08:27:36.553Z
May I ask why you run the inspec tests from test kitchen instead of during the node run with for exemple the chef-audit cookbook ? From what you describe, inspec run in a temporary VM targetting a real host, why not running inspec on the real host itself ?
Tensibai
CC BY-SA 4.0
2018-09-24T08:37:14.193Z
Inspec on a real host targeting a temporary VM in which runs the entire run list
Gaius
CC BY-SA 4.0
2018-09-24T09:04:16.777Z
So turn the other way, why not running inspec within the VM ?
Tensibai
CC BY-SA 4.0
2018-09-24T11:29:44.353Z
Because in my use cases it needs to see things that the VMs do not/cannot see
Gaius
CC BY-SA 4.0
2018-09-24T11:56:57.513Z
At this point you'd better [edit] your question to give more context. I highly suspect this will turn into a XY question and should be asked on #inspec channel on [chef's slack](http://community-slack.chef.io/) instead. the StackExchange format won't help much I think.
Tensibai
<chef><virtualbox><inspec>
Inspec tests conditional on OS version
Gaius
# Inspec tests conditional on OS version

(Asked by: username_0 on 2018-09-23)

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](http://www.hurryupandwait.io/blog/accessing-chef-node-attributes-from-kitchen-tests) 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](https://www.inspec.io/docs/reference/ruby_usage/) and therefore can't see it.

**Tags:** chef, virtualbox, inspec

> **Comments:**
> **username_1**: May I ask why you run the inspec tests from test kitchen instead of during the node run with for exemple the chef-audit cookbook ? From what you describe, inspec run in a temporary VM targetting a real host, why not running inspec on the real host itself ?  
> **username_0**: Inspec on a real host targeting a temporary VM in which runs the entire run list  
> **username_1**: So turn the other way, why not running inspec within the VM ?  
> **username_0**: Because in my use cases it needs to see things that the VMs do not/cannot see  
> **username_1**: At this point you'd better [edit] your question to give more context. I highly suspect this will turn into a XY question and should be asked on #inspec channel on [chef's slack](http://community-slack.chef.io/) instead. the StackExchange format won't help much I think.  

---

## 1 answers

### Answer by username_2 on 2018-11-20. Score: 3 | Accepted answer

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? }


---

