Job slicing & extra vars (AWX)

Greetings all!

I want to rack some of your brains and find out if my goal is possible.

I have a playbook for cloning VM’s with the proxmox_kvm community module, currently I loop through an extra var “{{ vm_name }}” that is split up if I include more than one hostname. This works fine, but it is sequential, and runtime is a few minutes. I started thinking about running my clones in parallel, after reading the docs I determined Job Slicing is the correct route to make this happen. However I may be misunderstanding how Slices work with extra vars, and maybe this isn’t possible with how I have my playbook currently written.

Is it possible to make a slice for each iteration of a loop based on my “{{ vm_name }}” extra var? If not, is there an alternative way to achieve this goal, a workflow perhaps?

---
- name: Clone VM's from template and configure with cloud-init
  hosts: localhost
  gather_facts: false
  vars:
    working_node: '{{ pve_node }}'

  module_defaults:
    proxmox_kvm:
      api_user: '{{ lookup("env", "PROXMOX_USER") }}'
      api_password: '{{ lookup("env", "PROXMOX_PASSWORD") }}'
      api_host: '{{ lookup("env", "PROXMOX_HOST") }}'

  tasks:
    - name: Loop through vm_name
      include_tasks: ../../pve/clone-template.yml
      loop: '{{ vm_name.split(",") }}'

Thanks in advance and happy holidays!

Hi, a possible solution is using the async option and poll=0, have a look here:
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_async.html#run-tasks-concurrently-poll-0

a quick example is:

---

- hosts: localhost
  connection: local
  gather_facts: false
  vars:
    list:
      - one
      - two
      - three
  tasks:
    - name: Test task
      ansible.builtin.shell: /bin/sleep 3 && echo {{ item }}
      async: 45
      poll: 0
      loop: "{{ list }}"
      register: async_results
    - name: Check sync status
      async_status:
        jid: "{{ async_result_item.ansible_job_id }}"
      loop: "{{ async_results.results }}"
      loop_control:
        loop_var: "async_result_item"
      register: async_poll_results
      until: async_poll_results.finished
      retries: 30

I think it should work for your case

1 Like

I had looked at async, because of my use of include_tasks and the structure of the playbook it was going to take a bit of a rewrite. Well I got it rewritten last night and everything is working great! The async status checks also allowed me to ditch some pauses I had in between proxmox_kvm module tasks saving even more time. Appreciate the suggestion and thank you very much. I went from 7 minutes to spin up 5 VM’s to 58 seconds for 10. :saluting_face:

1 Like