Ansible 条件下的可变环路中断
我需要第一件有一定条件的东西。例如,在本例中,我想要'banana',第一个状态为true的项。示例剧本:Ansible 条件下的可变环路中断,ansible,ansible-facts,Ansible,Ansible Facts,我需要第一件有一定条件的东西。例如,在本例中,我想要'banana',第一个状态为true的项。示例剧本: - hosts: localhost vars: my_fruit: fruits: [ {state: false, fruit: apple}, {state: true, fruit: banana}, {state: true, fruit: orange}, {state: false, fruit: pear}
- 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
结果为空,如果我跳过-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
我怎样才能得到“香蕉”
谢谢 问题来自这样一个事实,即您将变量“my_fruit”初始化为null,而不是空字符串(您的条件“
my_fruit='
”正在测试变量是否为空字符串),这就是playbook跳过整个列表的原因。我建议您将变量初始化为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"
}
问题在于,您将变量“my_fruit”初始化为null,而不是空字符串(您的条件“
my_fruit=='
”正在测试变量是否为空字符串),这就是playbook跳过整个列表的原因。我建议您将变量初始化为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"
}
试试这个
-设置事实:
my|fruit:{(fruits | selectattr('state')| first.fruit}
详情:
- 请参阅“如果未指定测试,则属性的值将作为布尔值进行计算。”
- 可以在列表中使用索引,而不是过滤器
first
my|fruit:{{(fruits | selectattr('state')| list).0.fruit}”
试试这个
-设置事实:
my|fruit:{(fruits | selectattr('state')| first.fruit}
详情:
- 请参阅“如果未指定测试,则属性的值将作为布尔值进行计算。”
- 可以在列表中使用索引,而不是过滤器
first
my|fruit:{{(fruits | selectattr('state')| list).0.fruit}”