想要在项目的值变为7之后中断任务,这是示例任务
- hosts: localhost
tasks:
- shell: echo {{ item }}
register: result
with_sequence: start=4 end=16
when: "{{ item }} < 7"
在上面的代码中,它将任务从4迭代到16,如下所示
PLAY [localhost] ***********************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************
ok: [localhost]
TASK [command] *************************************************************************************************************************************************
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ item }} < 7
changed: [localhost] => (item=4)
changed: [localhost] => (item=5)
changed: [localhost] => (item=6)
skipping: [localhost] => (item=7)
skipping: [localhost] => (item=8)
skipping: [localhost] => (item=9)
skipping: [localhost] => (item=10)
skipping: [localhost] => (item=11)
skipping: [localhost] => (item=12)
skipping: [localhost] => (item=13)
skipping: [localhost] => (item=14)
skipping: [localhost] => (item=15)
skipping: [localhost] => (item=16)
PLAY RECAP *****************************************************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
答案 0 :(得分:3)
Ansible运行循环并且:
4
,5
和6
的执行echo
命令 - 显示changed
状态,
其余项目未执行 - 显示skipped
状态,
你已经达到了你想要的目标。
唯一可以改进的是通过修复条件来消除警告:
- hosts: localhost
tasks:
- shell: echo {{ item }}
register: result
with_sequence: start=4 end=16
when: "item|int < 7"