Loops 如何在Ansible中打破循环?
如果要在item的值变为7后中断任务,下面是示例任务Loops 如何在Ansible中打破循环?,loops,ansible,Loops,Ansible,如果要在item的值变为7后中断任务,下面是示例任务 - hosts: localhost tasks: - shell: echo {{ item }} register: result with_sequence: start=4 end=16 when: "{{ item }} < 7" -hosts:localhost 任务: -shell:echo{{item} 寄存器:结果 按顺序:开始=4结
- hosts: localhost
tasks:
- shell: echo {{ item }}
register: result
with_sequence: start=4 end=16
when: "{{ item }} < 7"
-hosts:localhost
任务:
-shell:echo{{item}
寄存器:结果
按顺序:开始=4结束=16
当:“{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
PLAY[localhost]***********************************************************************************************************************************************
任务[收集事实]*****************************************************************************************************************************************
确定:[本地主机]
任务[命令]*************************************************************************************************************************************************
[警告]:when语句不应包含jinja2模板分隔符,如{{}或{%}。找到:{item}}<7
已更改:[localhost]=>(项=4)
已更改:[localhost]=>(项=5)
已更改:[localhost]=>(项=6)
正在跳过:[localhost]=>(项=7)
正在跳过:[localhost]=>(项=8)
正在跳过:[localhost]=>(项=9)
正在跳过:[localhost]=>(项=10)
正在跳过:[localhost]=>(项=11)
正在跳过:[localhost]=>(项=12)
正在跳过:[localhost]=>(项=13)
正在跳过:[localhost]=>(项=14)
正在跳过:[localhost]=>(项=15)
正在跳过:[localhost]=>(项=16)
重演*****************************************************************************************************************************************************
localhost:ok=2 changed=1 unreachable=0 failed=0
Ansible运行循环并:
- 对于项目
,4
和5
执行6
命令-显示echo
更改的状态
- 对于剩余的项目,它不会执行它-显示
跳过的
状态
您唯一可以改进的是通过修复条件来消除警告:
- hosts: localhost
tasks:
- shell: echo {{ item }}
register: result
with_sequence: start=4 end=16
when: "item|int < 7"
-hosts:localhost
任务:
-shell:echo{{item}
寄存器:结果
按顺序:开始=4结束=16
当:“项目| int<7”