Ansible 将多个异步任务注册到同一个变量 [英] Ansible Register Multiple Async Tasks to the Same Variable

查看:26
本文介绍了Ansible 将多个异步任务注册到同一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将多个异步任务注册到同一个变量?例如,如果我有两个任务,每个任务都调用一个异步 shell 命令:

Is there a way to register multiple async tasks to the same variable? For example if I have two tasks that each invoke an async shell command:

  - name: Run async task 1
    shell: echo "task 1"
    async: 30
    poll: 0
    register: db_wait

  - name: Run async task 2
    shell: echo "task 2"
    async: 30
    poll: 0
    register: db_wait

  - debug: msg="task vars {{db_wait}}"

当我打印 db_wait 变量时,它只包含一项任务的引用.

When I print the db_wait variable it only contains the reference of one task.

 "msg": "task vars {u'started': 1, u'results_file': u'/home/vagrant/.ansible_async/202582702042.7326', u'ansible_job_id': u'202582702042.7326', u'changed': False}"

有没有办法为异步任务或某种列表注册相同的变量,我可以添加并在以后迭代?

Is there a way to register the same variable for the async tasks or some sort of list I can add to and can iterate at a later point?

推荐答案

我最后想出了以下解决方案,希望它可以帮助其他人.问题的核心是我需要在不同的游戏中在不同的主机上运行一些长时间运行的任务,但只想在剧本的最后收集结果.

I came up with the following solution in the end, hopefully it may help other people. The core of the problem is I needed to run some long running tasks on different hosts in different plays, but only want to gather the results at the very end of the playbook.

为了让剧本运行我拥有的工作:

For the plays to run the jobs I have:

---
- name: Long Running tasks 1
  hosts: remote_host_1
  tasks:
    - name: Tasks 1
      shell: "{{item}}"
      with_items:
        - sleep 10
        - echo "Another Task"
      async: 3600
      poll: 0
      register: wait_task1


- name: Long Running tasks 2
  hosts: remote_host_2
  tasks:
    - name: Tasks 2
      shell: "{{item}}"
      with_items:
        - sleep 20
      async: 3600
      poll: 0
      register: wait_task2

然后最后的比赛是检查结果:

Then the final plays were to check the results:

- name: Verify Async Tasks 1
  hosts: remote_host_1
  tasks:
    - include: "/ansible/plays/check_results.yml wait={{wait_task1}}"


- name: Verify Async Tasks 2
  hosts: remote_host_2
  tasks:
    - include: "/ansible/plays/check_results.yml wait={{wait_task2}}"

使用这种机制的人看起来很熟悉检查结果:

And the check results looks familiar to people who have used this mechanism:

---
- name: Check Results
  async_status: jid={{ item.ansible_job_id }}
  register: task_result
  until: task_result.finished
  retries: 120
  delay: 30
  failed_when: task_result.rc != 0
  with_items: wait.results  

这会异步启动所有任务,然后我们可以在每次播放结束时收集结果.

This kicks off all tasks asynchronously and then we can gather the results at the end for each play.

这篇关于Ansible 将多个异步任务注册到同一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆