Looking for easy way to filter out windows versions below Server 2016

Is there a easy way to filter out older windows versions below 2016

For windows 7, windows 2008 etc I do other command in my playbook then for server 2016 and 2019 and windows 10.

I want to make one when statement instead of different ones.

You can do this if fact gathering is enabled with the ‘ansible_distribution_version’ factor but if you don’t have that you could just gather that info like;

`

  • name: skip using the built in facts
    debug:
    msg: Hello
    when: ansible_distribution_version is version_compare(‘10.0’, ‘>=’)

  • name: get OS version manually
    win_shell: (Get-Item -LiteralPath C:\Windows\System32\kernel32.dll).VersionInfo.ProductVersion
    changed_when: False
    register: os_version

  • name: skip using the manually gathered OS version var
    debug:
    msg: World
    when: os_version | trim is version_compare(‘10.0’, ‘>=’)

`

Both examples hinge on using the version_compare (or version) test [1]. This allows you to compare version numbers and check if it is less, equal, greater, etc, than the one you specify. For Windows 10 or Server 2016 you want to test if it’s greater than 10.0. Here is a brief list of version numbers for Windows;

  • Server 2008 - 6.0
  • Server 2008 R2/Windows 7 - 6.1
  • Server 2012 - 6.2
  • Server 2012 R2/Windows 8.1 - 6.3
  • Server 2016/Windows 10 - 10.0

[1] https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#version-comparison

Thanks

Jordan

`

`

  • name: skip using the manually gathered OS version var
    debug:
    msg: World
    when: (os_version.stdout | trim) is version_compare(‘10.0’, ‘>=’)

`

`

Minor edit to the last tasks’ when condition.

Thx Jordan Borean this was the information I was looking for.
many thx