Skip to content

Instantly share code, notes, and snippets.

@moreati
Created February 3, 2021 00:31
Show Gist options
  • Save moreati/e7507c5b606b12ec0ddafcb7c8debbf1 to your computer and use it in GitHub Desktop.
Save moreati/e7507c5b606b12ec0ddafcb7c8debbf1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ansible-playbook
---
- name: Test behaviour of version test
hosts: localhost
gather_facts: false
tasks:
- name: Expected results, comparing a.b.c <-> x.y.z, or a.b <-> x.y
debug:
msg:
- '"1.0" is version("1.0", "==") -> {{ "1.0" is version("1.0", "==") }}'
- '"1.0.0" is version("1.0.0", "==") -> {{ "1.0.0" is version("1.0.0", "==") }}'
- '"2.0" is version("1.0", ">") -> {{ "2.0" is version("1.0", ">") }}'
- '"2.0.0" is version("1.0.0", ">") -> {{ "2.0.0" is version("1.0.0", ">") }}'
- name: Gotchas, comparing a.b.c <-> x.y, or a.b <-> x.y.z
debug:
msg:
- '"1.0.0" is version("1.0", "==") -> {{ "1.0.0" is version("1.0", "==") }}'
- '"1.0" is version("1.0.0", "==") -> {{ "1.0" is version("1.0.0", "==") }}'
- '"1.0.0" is version("1.0", ">") -> {{ "1.0.0" is version("1.0", ">") }}'
- '"3.0.0" is version("3.0.0b1", ">") -> {{ "3.0.0" is version("3.0.0b1", ">") }}'
@moreati
Copy link
Author

moreati commented Feb 3, 2021

PLAY [Test behaviour of version test] *****************************************************************************

TASK [Expected results, comparing a.b.c <-> x.y.z, or a.b <-> x.y] ************************************************
ok: [localhost] => 
  msg:
  - '"1.0" is version("1.0", "==")      -> True'
  - '"1.0.0" is version("1.0.0", "==")  -> True'
  - '"2.0" is version("1.0", ">")       -> True'
  - '"2.0.0" is version("1.0.0", ">")   -> True'

TASK [Gotchas, comparing a.b.c <-> x.y, or a.b <-> x.y.z] *********************************************************
ok: [localhost] => 
  msg:
  - '"1.0.0" is version("1.0", "==")    -> False'
  - '"1.0" is version("1.0.0", "==")    -> False'
  - '"1.0.0" is version("1.0", ">")     -> True'
  - '"3.0.0" is version("3.0.0b1", ">") -> False'

PLAY RECAP ********************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

@moreati
Copy link
Author

moreati commented Nov 18, 2021

See also https://twitter.com/moreati/status/1357074302848430080

Paint me 0.0.1 & call me pre-release. Ansible's version test has a strict mode that does the right thing
Do: '1.2.0' is version('1.2', '==', strict=True) # true
Do: '1.2.0' is version('1.2', '>', strict=True) # false
Do: '1.2.0' is version('1.2.0a1', '>', strict=True) # true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment