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

16 views
Skip to first unread message

Seth Daemen

unread,
Mar 23, 2019, 9:01:58 AM3/23/19
to Ansible Project
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.

Jordan Borean

unread,
Mar 25, 2019, 3:43:57 PM3/25/19
to Ansible Project
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


Thanks

Jordan

Jordan Borean

unread,
Mar 25, 2019, 3:45:06 PM3/25/19
to Ansible Project
- 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.

Seth Daemen

unread,
Mar 27, 2019, 11:25:42 AM3/27/19
to Ansible Project
Thx Jordan Borean this was the information I was looking for.
many thx

Op maandag 25 maart 2019 20:45:06 UTC+1 schreef Jordan Borean:
Reply all
Reply to author
Forward
0 new messages