Ansible Register Multiple Async Tasks to the Same Variable

风格不统一 提交于 2019-12-30 12:03:08

问题


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

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?


回答1:


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.




回答2:


I think the only way you would be able to do what you want is if you rewrote your example along these lines:

- name: run tasks 
  shell: {{ item }}
  async: 30
  poll: 0
  register: db_wait
  with_items:
    -  echo "task 1"
    -  echo "task 2"

Since this is a single task that iterates over a loop of items then all the results will end up in the same register variable. In the example you provided, the variable will always be overwritten by the second task since tasks are distinct.



来源:https://stackoverflow.com/questions/32703825/ansible-register-multiple-async-tasks-to-the-same-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!