Ansible:理解复合条件when语句

Ansible:理解复合条件when语句,ansible,ansible-playbook,Ansible,Ansible Playbook,考虑一下下面这个微不足道的ansible剧本和相关输出。为什么? 任务5是否得到执行?这些任务是针对debian运行的。任务1 按预期失败。那么,为什么要和它一起 “ansible_lsb.major_发行版| int

考虑一下下面这个微不足道的ansible剧本和相关输出。为什么? 任务5是否得到执行?这些任务是针对debian运行的。任务1 按预期失败。那么,为什么要和它一起 “ansible_lsb.major_发行版| int<14”实现了吗?这个有吗 与运算符优先级有关吗

-jk

2) 改变

when:is_ubuntu and ansible_lsb.major_release|int<14

when:is|u ubuntu | bool and ansible|u lsb.major|u release | int<14
成功了


-jk

TLDR:您的变量将作为字符串输出,而不是计算。使用jinja2修复评估,然后将var过滤为
| bool

调试 调试此问题只缺少一件事。以下是我在本地OSX盒上运行的内容:

- name: stackoverflow 26188055
  hosts: local
  vars:
    - bcbio_dir: /mnt/bcbio
    - is_ubuntu: "'{{ansible_distribution}}' == 'Ubuntu'"
    - is_debian: "'{{ansible_distribution}}' == 'Debian'"
  tasks:
    - debug: var=is_ubuntu
    - debug: var=is_debian
    - debug: msg="this shows the conditional passes even though it shouldnt"
      when: is_ubuntu and true
以及输出:

TASK: [debug var=is_ubuntu] *************************************************** 
ok: [127.0.0.1] => {
    "is_ubuntu": "'MacOSX' == 'Ubuntu'"
}

TASK: [debug var=is_debian] *************************************************** 
ok: [127.0.0.1] => {
    "is_debian": "'MacOSX' == 'Debian'"
}

TASK: [debug msg="this shows the conditional passes even though it shouldnt"] *** 
ok: [127.0.0.1] => {
    "msg": "this shows the conditional passes even though it shouldnt"
}
评价 据我所知,你不可能真的计算到布尔值。通常,这是通过展开变量(将其放置在每个“when”中)来完成的。然而,它可以实现,因为您可以将布尔值作为字符串,然后将其转换为布尔值(搜索“布尔值”)

这是输出

TASK: [debug var=is_ubuntu] *************************************************** 
ok: [127.0.0.1] => {
    "is_ubuntu": "False"
}

TASK: [debug var=is_debian] *************************************************** 
ok: [127.0.0.1] => {
    "is_debian": "False"
}

TASK: [debug msg="this shows the conditional passes even though it shouldnt"] *** 
skipping: [127.0.0.1]
版本比较过滤器 注意:您可能希望利用。用法留给读者作为练习

when: is_ubuntu and ansible_lsb.major_release|int < 14 
when: is_ubuntu|bool and ansible_lsb.major_release|int < 14 
- name: stackoverflow 26188055
  hosts: local
  vars:
    - bcbio_dir: /mnt/bcbio
    - is_ubuntu: "'{{ansible_distribution}}' == 'Ubuntu'"
    - is_debian: "'{{ansible_distribution}}' == 'Debian'"
  tasks:
    - debug: var=is_ubuntu
    - debug: var=is_debian
    - debug: msg="this shows the conditional passes even though it shouldnt"
      when: is_ubuntu and true
TASK: [debug var=is_ubuntu] *************************************************** 
ok: [127.0.0.1] => {
    "is_ubuntu": "'MacOSX' == 'Ubuntu'"
}

TASK: [debug var=is_debian] *************************************************** 
ok: [127.0.0.1] => {
    "is_debian": "'MacOSX' == 'Debian'"
}

TASK: [debug msg="this shows the conditional passes even though it shouldnt"] *** 
ok: [127.0.0.1] => {
    "msg": "this shows the conditional passes even though it shouldnt"
}
- name: stackoverflow 26188055
  hosts: local
  vars:
    - bcbio_dir: /mnt/bcbio
    - is_ubuntu: "{{ansible_distribution == 'Ubuntu'}}"
    - is_debian: "{{ansible_distribution == 'Debian'}}"
  tasks:
    - debug: var=is_ubuntu
    - debug: var=is_debian
    - debug: msg="this shows the conditional passes even though it shouldnt"
      when: is_ubuntu|bool and true
TASK: [debug var=is_ubuntu] *************************************************** 
ok: [127.0.0.1] => {
    "is_ubuntu": "False"
}

TASK: [debug var=is_debian] *************************************************** 
ok: [127.0.0.1] => {
    "is_debian": "False"
}

TASK: [debug msg="this shows the conditional passes even though it shouldnt"] *** 
skipping: [127.0.0.1]