简体   繁体   English

Ansible 无法将版本与 is 版本进行比较

[英]Ansible fails to compare version with is version

I use Ansible 2.8.5.我使用 Ansible 2.8.5。 I could use ansible_facts.package but simple, let's use the following playbook.我可以使用ansible_facts.package但很简单,让我们使用以下剧本。

  - debug:
     msg: "{{ { 'version':'5.52.2' } is version('6.0.0','>=') }}"

returns返回

   ..."msg" : True

How can I get a correct dot separated version correctly compared?如何正确比较正确的点分隔版本?

I read the official documentation and did not manage to understand how the function version is working.我阅读了官方文档,但没能理解函数version是如何工作的。 Some people even said that this should rise a TypeError exception.甚至有人说这应该引发TypeError异常。 Nobody seems to understand how version is designed to work...似乎没有人理解version是如何设计的......

Try this.尝试这个。 refer Version Comparison .请参阅版本比较 Provide the version in the string.在字符串中提供版本。

- hosts: localhost
  tasks:
    - set_fact:
            version1: "5.52.2"
            version2: "6.0.0"
    - debug:
            msg: "{{ version1  is version( version2  , '>=') }}"

Why don't you register the version first in a variable and then add a check.为什么不先在变量中register版本,然后添加检查。 Then you can compare the content of the variable.然后你可以比较变量的内容。 Here is an example -这是一个例子——

failed_when: "'<version>{{ version }}</version>' not in this.content"
    - debug: msg=" version is {{ this.content.split('\n')[0].split('version')[1].split('>')[1].split('</')[0]}}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM