【发布时间】: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