我想知道如何循环多个任务直到满足条件。
#main.yml
- set_fact:
num: 1
req_num: 10
- name: Start to unregister entities
include_tasks: output.yml
loop: "{{ range(num, req_num + 1)|list }}"
#输出.yml
- name: get status
raw: cat /tmp/output
register: rawoutput
- name: copy to localhost
copy:
content: "{{rawoutput.stdout}}"
dest: /tmp/output1
delegate_to: localhost
- name: reg output2
shell: awk something /tmp/output1 |awk '/something/,0' |head -n something |tail -n something > /tmp/output2 ; cat /tmp/output2
register: output2
delegate_to: localhost
- name: compare output2
debug:
msg: "{{item}}"
with_items: "{{ output2.stdout_lines }}"
until: item == "Synced"
retries: 2
delay: 2
#cat /tmp/输出2
Synced
Syncing
Synced
Failed
我在用this https://stackoverflow.com/questions/64164654/execute-ansible-include-tasks-until-a-certain-condition-is-met-kind-of-while-un,但是当子任务失败时,剧本会退出。
我的目标是确保 output2 中的所有内容都是“同步的”,循环output.yml
直到结果为“已同步”,或在 x 次尝试后失败。
感谢是否有更好的方法来做到这一点。谢谢
谢谢
我想我找到了答案here https://github.com/ansible/ansible/issues/46203#issuecomment-556013701.
来自 ovski4
- name: 'Wait until success'
block:
- name: Set the retry count
set_fact:
retry_count: "{{ 0 if retry_count is undefined else retry_count|int + 1 }}"
- name: Get server updated ip
uri:
url: https://localhost/ip
return_content: yes
status_code: 200
register: ip
- name: ssh to the server
wait_for:
host: "{{ ip }}"
port: 22
timeout: 30
state: started
rescue:
- fail:
msg: Ended after 5 retries
when: retry_count|int == 5
- debug:
msg: "Failed to connect - Retrying..."
- include_tasks: wait_until_success.yml
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)