【问题标题】:Waiting on async tasks in Ansible throws error在 Ansible 中等待异步任务会引发错误
【发布时间】:2022-07-25 18:57:00
【问题描述】:

我在 Ansible 中生成了几个异步任务,并尝试等待它们全部完成,然后再继续执行下一组任务(总共 9 个)。以下是其中的几个示例:

- name: EC2 spin up
  async: 6000
  poll: 0
  shell: # spin up an instance with a playbook
  register: ec2_item
  when: deployment_type == 'x' or deployment_type == 'y'

- name: EC2 spin up another
  async: 6000
  poll: 0
  shell: # # spin up another instance with a playbook
  register: ec2_item_again
  when: deployment_type == 'x' or deployment_type == 'y'

在这之后,我正在运行这个块来等待他们:

- name: Wait on EC2 Async Tasks
  async_status:
  jid: "{{ item.ansible_job_id }}"
  with_items:
    - "{{ ec2_item }}"
    - "{{ ec2_item_again }}"
  register: job_result
  until: job_result.finished
  retries: 60
  delay: 15

最终,事情完成了,但没有他跟随就出错了:

fatal: [127.0.0.1]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'ansible_job_id'\n\nThe error appears to be in '/opt/app-root/src/playbooks/sb_build_flow.yml': line 201, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n      delay: 6\n    - async_status:\n      ^ here\n"
}

它试图查看一个不再存在的任务,但我不确定如何实际完成这项工作。同样,我只是想让 Ansible 也等待异步任务完成后再继续。

【问题讨论】:

    标签: asynchronous async-await ansible


    【解决方案1】:

    根据文档async_status module – Obtain status of asynchronous task -Examplesjid 至少有一个错误的标识。

    以下最小示例似乎运行成功

    ---
    - hosts: test
      become: false
      gather_facts: false
    
      tasks:
    
      - name: Wait ONE
        shell:
          cmd: "sleep 15"
        async: 6000
        poll: 0
        register: ONE
    
      - name: Wait TWO
        shell:
          cmd: "sleep 15"
        async: 6000
        poll: 0
        register: TWO
    
      - name: Wait on tasks
        async_status:
          jid: "{{ item.ansible_job_id }}"
        loop: "[{{ ONE }}, {{ TWO }}]"
        loop_control:
          label: "{{ item.ansible_job_id }}"
        register: job_result
        until: job_result.finished
        retries: 60
        delay: 15
    

    导致输出

    TASK [Wait ONE] *****************************************
    changed: [test.example.com]
    
    TASK [Wait TWO] *****************************************
    changed: [test.example.com]
    FAILED - RETRYING: Wait on tasks (60 retries left).
    
    TASK [Wait on tasks] ************************************
    changed: [test.example.com] => (item=229736317985.176035)
    changed: [test.example.com] => (item=829194009194.176257)
    

    更多文档

    【讨论】:

      猜你喜欢
      • 2014-09-06
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 2016-07-24
      • 2013-02-10
      • 2017-07-22
      • 1970-01-01
      相关资源
      最近更新 更多