【问题标题】:Ansible loop break at conditionAnsible 循环中断条件
【发布时间】:2020-06-03 10:19:47
【问题描述】:

我需要有特定条件的第一个项目。例如,在这种情况下,我想要“香蕉”,即第一个状态为 true 的项目。示例剧本:

- hosts: localhost
  vars:
    my_fruit:
    fruits: [
      {state: false, fruit: apple},
      {state: true, fruit: banana},
      {state: true, fruit: orange},
      {state: false, fruit: pear}
    ]
  tasks:
    - name: get first fruit with state = true.
      set_fact:
        my_fruit: "{{ item.fruit }}"
      loop: "{{ fruits }}"
      when: 
        - item.state == true
        - my_fruit == ''

    - name: Check true fruit.
      debug:
        var: my_fruit

输出是:

PLAY [localhost] ********************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [localhost]

TASK [get first fruit with state = true.] *******************************************************************************************************************************************************************
skipping: [localhost] => (item={'state': False, 'fruit': 'apple'}) 
skipping: [localhost] => (item={'state': True, 'fruit': 'banana'}) 
skipping: [localhost] => (item={'state': True, 'fruit': 'orange'}) 
skipping: [localhost] => (item={'state': False, 'fruit': 'pear'}) 

TASK [Check true fruit.] ************************************************************************************************************************************************************************************
ok: [localhost] => {
    "my_fruit": null
}

PLAY RECAP **************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

my_fruit结果为null,如果我跳过- my_fruit == ''条件,结果为'橙色':

PLAY [localhost] ********************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [localhost]

TASK [get first fruit with state = true.] *******************************************************************************************************************************************************************
skipping: [localhost] => (item={'state': False, 'fruit': 'apple'}) 
ok: [localhost] => (item={'state': True, 'fruit': 'banana'})
ok: [localhost] => (item={'state': True, 'fruit': 'orange'})
skipping: [localhost] => (item={'state': False, 'fruit': 'pear'}) 

TASK [Check true fruit.] ************************************************************************************************************************************************************************************
ok: [localhost] => {
    "my_fruit": "orange"
}

PLAY RECAP **************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

我怎样才能得到'香蕉'?

谢谢!

【问题讨论】:

    标签: ansible ansible-facts


    【解决方案1】:

    问题在于您将变量“my_fruit”初始化为 null 而不是空字符串(您的条件“my_fruit == ''”正在测试变量是否为空字符串),这就是剧本的原因正在跳过你的整个列表。我建议您将变量初始化为my_fruit: '',它应该可以工作

    TASK [get first fruit with state = true.] ****************************************************************************************************************************
    skipping: [localhost] => (item={'state': False, 'fruit': 'apple'}) 
    ok: [localhost] => (item={'state': True, 'fruit': 'banana'})
    skipping: [localhost] => (item={'state': True, 'fruit': 'orange'}) 
    skipping: [localhost] => (item={'state': False, 'fruit': 'pear'}) 
    
    TASK [Check true fruit.] *********************************************************************************************************************************************
    ok: [localhost] => {
        "my_fruit": "banana"
    }
    

    【讨论】:

      【解决方案2】:

      试试这个

          - set_fact:
              my_fruit: "{{ (fruits|selectattr('state')|first).fruit }}"
      

      详情:

      • selectattr “如果没有指定测试,属性的值将被评估为布尔值。”

      • 可以在列表中使用索引,而不是过滤器first

              my_fruit: "{{ (fruits|selectattr('state')|list).0.fruit }}"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-12
        • 2022-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-07
        • 2016-03-29
        • 1970-01-01
        相关资源
        最近更新 更多