when 里面的 Ansible 变量

Ansible variable inside when

我正在制作剧本,我在其中获得了名为 sd* 的磁盘,没有支架,也没有分区。

我有这个:

  - set_fact:
      test: "{{ ansible_facts.devices }}"

  - debug:
      msg: "{{ test }}"

  - debug:
      msg: "{{ test }}.{{item}}"
    when: "ansible_facts.devices.{{item}}.holders == [] and ansible_facts.devices.{{item}}.partitions == {} and ansible_facts.devices.{{item}} == {{item}}"
    loop:
    - sdb
    - sdc

但是我收到这个错误:

  conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: ansible_facts.devices.{{item}}.holders == [] and ansible_facts.devices.{{item}}.partitions == {} and ansible_facts.devices.{{item}} == {{item}}

如何在 jinja2 模板中使用变量?我也试过这个:

ansible_facts.devices.item.holders == [] and ansible_facts.devices.item.partitions == {} and ansible_facts.devices.item == item

但是我得到了错误,那个项目是一个未定义的变量。

此致 VallingSki

要仅显示带有空支架和隔板的设备,试试这个

- debug:
    msg: "{{ test[item] }}"
  loop: [sdb,sdc]
  when:
    - ansible_facts.devices[item].holders|length == 0
    - ansible_facts.devices[item].partitions|length == 0

(未测试)


备注

  • 属性分区也是一个列表,不是字典
  • 我省略了条件ansible_facts.devices[item] == item因为我不明白