How to compare version strings in salt sls files

2019-05-12 23:43发布

问题:

Does saltstack have an equivalent to puppets versioncmp() function? Or alternatively, is there a way to get the distutils.version or packaging.version.parse methods (as mentioned on Compare version strings in Python) available in a jinja+yaml rendered sls file?

回答1:

you can use the module pkg.version_cmp:

# salt-call pkg.version_cmp '1.0.2' '1.1.1'
local:
    -1
# salt-call pkg.version_cmp '0.2.4.1-0ubuntu1' '0.2.4-0ubuntu1'
local:
    1

Inside jinja you can use it in a way similar to:

{% if salt['pkg.version_cmp']('1.1.0','1.0.5') > 0 %}
  ....
{% endif %}