Ansible Register Multiple Async Tasks to the Same Variable

前端 未结 2 736
失恋的感觉
失恋的感觉 2021-01-16 00:00

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          


        
相关标签:
2条回答
  • 2021-01-16 00:05

    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.

    0 讨论(0)
  • 2021-01-16 00:13

    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.

    0 讨论(0)
提交回复
热议问题