--- - name: Shut down vms virt: name: "{{ item.name }}" command: shutdown state: shutdown uri: "{{ libvirt_uri }}" with_items: "{{ overcloud_nodes }}" - name: Evaluate overcloud VM libvirt names set_fact: libvirt_oc_names: >- {%- set names=[] -%} {%- for n in overcloud_nodes -%} {%- if names.append(hostvars[n['name']]['inventory_hostname']) -%}{%- endif -%} {%- endfor -%} {{ names | join('|') }} - name: Wait for vms to shutdown async: 300 poll: 0 shell: | while true do virsh list | grep -E '{{ libvirt_oc_names }}' > /dev/null || break done register: vms_shutdown changed_when: false - name: Check that vms are shutdown async_status: jid: "{{ vms_shutdown.ansible_job_id }}" register: job_result until: job_result.finished retries: 30 - name: Commit any changes if a snapshot already exists shell: | qemu-img commit {{ libvirt_volume_path }}/{{ item.name }}.qcow2 args: removes: "{{ libvirt_volume_path }}/{{ item.name }}.qcow2.bak" with_items: "{{ overcloud_nodes }}" when: snapshot_create|bool changed_when: true - name: Create snapshot shell: | mv {{ libvirt_volume_path }}/{{ item.name }}.qcow2 {{ libvirt_volume_path }}/{{ item.name }}.qcow2.bak qemu-img create -f qcow2 -b {{ libvirt_volume_path }}/{{ item.name }}.qcow2.bak {{ libvirt_volume_path }}/{{ item.name }}.qcow2 args: creates: "{{ libvirt_volume_path }}/{{ item.name }}.qcow2.bak" with_items: "{{ overcloud_nodes }}" when: snapshot_create|bool changed_when: true - name: Restore previous snapshot shell: | rm {{ libvirt_volume_path }}/{{ item.name }}.qcow2 qemu-img create -f qcow2 -b {{ libvirt_volume_path }}/{{ item.name }}.qcow2.bak {{ libvirt_volume_path }}/{{ item.name }}.qcow2 with_items: "{{ overcloud_nodes }}" when: snapshot_restore|bool changed_when: true - name: Start vms virt: name: "{{ item.name }}" command: start autostart: true state: running uri: "{{ libvirt_uri }}" with_items: "{{ overcloud_nodes }}" - name: "Wait for the vms to start" wait_for: host: "{{ hostvars[item.name]['ansible_host'] }}" port: 22 state: started with_items: "{{ overcloud_nodes }}"