changed_when: NumberA > NumberB

I’m attempting to compare version numbers of a app by extracting the current version from the output of a shell command and comparing it to the version to be deployed. However the result of the changed_when: evaluation is always true, regardless of the numbers I put in. I’ve confirmed the numbers that it’s evaluating through debug: msg tasks. Is there a way to make this work?

Here’s a simplified version:


- hosts: all
  vars:
    NEW_VERSION: 2
  tasks:
  - name: weblogic patch downgrade check 
    shell: "echo '2'"
    register: current_version 
    changed_when: current_version.stdout > {{ NEW_VERSION }}

The actual task syntax i'm using

- name: weblogic patch downgrade check 
  shell: "cat /app/psoft/logs/wlversion |sed -n -e 's/^Patch \([0-9]\{8\}\).*/\1/p'"
  register: old_patch_version 
  changed_when: old_patch_version.stdout > {{ WEBLOGIC_PATCH }}

In this example, both old_patch_version.stdout and {{ WEBLOGIC_PATCH }} are returning 20844228 but the task is registering as changed: true.
Thanks!

I came across this link which pretty much explains what my problem is. These are strings i’m trying to do mathematical compares against. I’d need a way to convert them to int’ss to do a comparison like this. If anyone has any ideas, please let me know.

https://github.com/ansible/ansible/issues/8498

I found the answer. By putting | int }} next to the variable, it converts to an integer and compares properly.

first thing you don't need the extra moustaches:

changed_when: current_version.stdout > NEW_VERSION

yes, int will 'fix' the comparison

changed_when: current_version.stdout|int > NEW_VERSION|int

but this will only work for major versions 2.1 vs 3.2 and not minor
2.1 vs 2.2, what you reall want is:

changed_when: current_version.stdout | version_compare(NEW_VERSION,
operator='gt')