【问题标题】:Ansible variable inside when里面的 Ansible 变量 when
【发布时间】:2021-01-13 10:14:28
【问题描述】:

我正在编写一个剧本,我在其中获得名为 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

【问题讨论】:

    标签: ansible jinja2 ansible-awx ansible-tower


    【解决方案1】:

    要仅显示带有空支架和隔板的设备,请尝试此操作

    - 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,因为我不明白

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-05
      • 2018-12-05
      • 2015-12-18
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多