gpt4 book ai didi

loops - ansible 循环 include_tasks 直到成功

转载 作者:行者123 更新时间:2023-12-03 08:28:30 25 4
gpt4 key购买 nike

我想知道如何循环多个任务直到满足条件。

#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 }}"

#output.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/output2

Synced
Syncing
Synced
Failed

我正在使用this ,但是当子任务失败时,剧本会退出。

我的目标是确保output2中的所有内容都是“已同步”,循环output.yml直到结果是“已同步”,或者在x次尝试后失败。

感谢是否有更好的方法来做到这一点。谢谢谢谢

最佳答案

我想我找到了答案 here .

来自 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

关于loops - ansible 循环 include_tasks 直到成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65914952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com