Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops ansible循环包括任务和直到成功_Loops_Ansible_Until Loop - Fatal编程技术网

Loops ansible循环包括任务和直到成功

Loops ansible循环包括任务和直到成功,loops,ansible,until-loop,Loops,Ansible,Until Loop,我想知道如何循环完成多个任务,直到满足条件 #main.yml #output.yml - name: get status raw: cat /tmp/output register: rawoutput - name: copy to localhost copy: content: "{{rawoutput.stdout}}" dest: /tmp/output1 delegate_to: localho

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

#main.yml

#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/输出2

Synced
Syncing
Synced
Failed
我正在使用,但当子任务失败时,playbook将退出

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

如果有更好的方法,我将不胜感激。谢谢 谢谢

我想我找到了答案

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