1
votes

Trying to compare the installed package version and upgrade to latest version in Ansible, Blocked at when package is not installed in server.

- debug:
    msg: "Installed version is null or lower than {{ gcc_new_version.stdout }}"
  when: 'gcc_version.stdout == "" || gcc_version.stdout is version(gcc_new_version.stdout, '<')

Error:

fatal: FAILED! => {"msg": "The conditional check 'gcc_version.stdout == "" || gcc_version.stdout is version(gcc_new_version.stdout, '<')' failed. The error was: template error while templating string: expected token 'name', got '|'. String: {% if gcc_version.stdout == "" || gcc_version.stdout is version(gcc_new_version.stdout, '<') %} True {% else %} False {% endif %}\n\nThe error appears to be in 'ansible/roles/linux/tasks/gcc.yml': line 24, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- debug:\n ^ here\n"}

1

1 Answers

1
votes

I think the issue is the syntax in when condition. Conditions are in Jinja context, so use of single quotes ' around conditions is not required. Also the or keyword can be used instead of ||.

- debug:
    msg: "Installed version is null or lower than {{ gcc_new_version.stdout }}"
  when: (gcc_version.stdout == "") or (gcc_version.stdout is version_compare(gcc_new_version.stdout, '<'))