简体   繁体   English

当条件处于 ansible 时

[英]When condition in ansible

I have written the following playbook and it's working fine but when I am doing the same thing with roles, when condition of the fail module is messing up.我已经编写了以下剧本并且它运行良好但是当我对角色做同样的事情时,当失败模块的条件混乱时。 Irrespective of the values defined, when I am giving > in when, in fail module, it's skipping and when giving < , it's failing.不管定义的值如何,当我在失败模块中给出 > 时,它正在跳过,而当给出 < 时,它失败了。 Please don't mind the syntax and '-' s, it's messing up here.请不要介意语法和“-”,它在这里搞砸了。

- hosts: localhost
  vars: 
    vmcpu_list:
      - vmcpu: 2
      - vmcpu: 1
      - vmcpu: 1
    vcpu_value: 0 

  tasks:
    - set_fact: 
          vcpu_value: "{{ vcpu_value }} + vmcpu_list[{{item}}].vmcpu"
      with_sequence: start=0 end="{{ vmcpu_list | length -1 }}" 
    - debug:
           var: "{{ vcpu_value }}"
    - fail: 
          msg: " provided vcpu are more"
      when: vcpu_value|int > 5

NOTE: Sorry earlier I have given vcpu_value|int > 5 above but it should be vcpu_value|int > 3注意:抱歉之前我已经给出了 vcpu_value|int > 5 但它应该是 vcpu_value|int > 3

 - fail: 
          msg: " provided vcpu are more"
      when: vcpu_value|int > 5

you have set vcpu_value: 0 condition assessement vcpu_value < 5 it dosn't match your condition ==> ansible will skip the task您已设置 vcpu_value: 0 条件评估 vcpu_value < 5 它与您的条件不匹配 ==> ansible 将跳过该任务

   - fail: 
              msg: " provided vcpu are more"
          when: vcpu_value|int < 5

you have set vcpu_value: 0 condition assessement vcpu_value < 5 OK ==> ansible will get execute the task您已设置 vcpu_value: 0 条件评估 vcpu_value < 5 OK ==> ansible 将执行任务

There is no problem your code works fine no strange behavior ^^没有问题,您的代码工作正常,没有奇怪的行为 ^^

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

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