-1

I have a the following snippet where I install OS on a virtual machine using ansible, and after it finishes it stops the VM so I can continue the rest of the tasks, I am collecting facts from the red hat virtualization manager regarding the state the vm, and I want to keep waiting until the status of the VM changes from up to down so I can proceed, how can I code this?:

# I am kickstarting the VM
- name:  Installing OS
    ovirt_vms:
      state: running
      name: "{{ vm_name }}"
      initrd_path: iso://initrd.img
      kernel_path: iso://vmlinuz
      kernel_params: initrd=initrd.img inst.stage2=cdrom inst.ks=ftp://10.0.1.2/pub/ks.cfg net.ifnames=0 biosdevname=0 BOOT_IMAGE=vmlinuz

# Getting facts about the VM    
- name: Gather VM Status
    ovirt_vms_facts:
      pattern: name={{ vm_name}}
- name: Register VM Status
  debug:
    msg: "{{ ovirt_vms[0].status }}"
  register: vm_status
#Should Keep probing the value of vm_status until it changes from up to down.
           ????????????? --> What should I do here?
#When Status change continue the work book

I tried to parse the ovirt_vms I gathered from ovirt_vms_facts, and I got the following:

{
    "_ansible_parsed": true,
    "invocation": {
        "module_args": {
            "all_content": false,
            "pattern": "name=as-vm-type1",
            "nested_attributes": [],
            "case_sensitive": true,
            "fetch_nested": false,
            "max": null
        }
    },
    "changed": false,
    "_ansible_no_log": false,
    "ansible_facts": {
        "ovirt_vms": [
            {
                "disk_attachments": [],
                "origin": "ovirt",
                "sso": {
                    "methods": []
                },
                "affinity_labels": [],
                "placement_policy": {
                    "affinity": "migratable"
                },
                "watchdogs": [],
                "creation_time": "2018-07-15 13:54:10.565000+02:00",
                "snapshots": [],
                "graphics_consoles": [],
                "cluster": {
                    "href": "/ovirt-engine/api/clusters/a5272863-38a8-469d-998e-c1e1f26f4f5a",
                    "id": "a5272863-38a8-469d-998e-c1e1f26f4f5a"
                },
                "href": "/ovirt-engine/api/vms/08406dad-5173-4241-8d42-904ddf3d096a",
                "migration": {
                    "auto_converge": "inherit",
                    "compressed": "inherit"
                },
                "io": {
                    "threads": 0
                },
                "migration_downtime": -1,
                "id": "08406dad-5173-4241-8d42-904ddf3d096a",
                "high_availability": {
                    "priority": 0,
                    "enabled": false
                },
                "cdroms": [],
                "statistics": [],
                "usb": {
                    "enabled": false
                },
                "display": {
                    "allow_override": false,
                    "disconnect_action": "LOCK_SCREEN",
                    "file_transfer_enabled": true,
                    "copy_paste_enabled": true,
                    "secure_port": 5900,
                    "smartcard_enabled": false,
                    "single_qxl_pci": false,
                    "type": "spice",
                    "monitors": 1,
                    "address": "10.254.148.74"
                },
                "nics": [],
                "tags": [],
                "name": "as-vm-type1",
                "bios": {
                    "boot_menu": {
                        "enabled": false
                    }
                },
                "stop_time": "2018-07-15 13:54:10.569000+02:00",
                "template": {
                    "href": "/ovirt-engine/api/templates/00000000-0000-0000-0000-000000000000",
                    "id": "00000000-0000-0000-0000-000000000000"
                },
                "memory": 42949672960,
                "type": "server",
                "katello_errata": [],
                "numa_tune_mode": "interleave",
                "status": "up",
                "next_run_configuration_exists": false,
                "delete_protected": false,
                "sessions": [],
                "start_time": "2018-07-15 13:54:14.079000+02:00",
                "quota": {
                    "id": "ad014a63-fd76-42da-8369-57dae2dd5979"
                },
                "applications": [],
                "host": {
                    "href": "/ovirt-engine/api/hosts/56a65d3b-1c0a-4b2a-9c6c-aa96262d9502",
                    "id": "56a65d3b-1c0a-4b2a-9c6c-aa96262d9502"
                },
                "memory_policy": {
                    "max": 171798691840,
                    "guaranteed": 42949672960
                },
                "numa_nodes": [],
                "permissions": [],
                "stateless": false,
                "reported_devices": [],
                "large_icon": {
                    "href": "/ovirt-engine/api/icons/2971ddbe-1dbf-4af8-b86a-078cbbe66419",
                    "id": "2971ddbe-1dbf-4af8-b86a-078cbbe66419"
                },
                "storage_error_resume_behaviour": "auto_resume",
                "cpu_profile": {
                    "href": "/ovirt-engine/api/cpuprofiles/34000c79-d669-41ef-8d2a-d37d7f925c3c",
                    "id": "34000c79-d669-41ef-8d2a-d37d7f925c3c"
                },
                "time_zone": {
                    "name": "Etc/GMT"
                },
                "run_once": true,
                "original_template": {
                    "href": "/ovirt-engine/api/templates/00000000-0000-0000-0000-000000000000",
                    "id": "00000000-0000-0000-0000-000000000000"
                },
                "start_paused": false,
                "host_devices": [],
                "small_icon": {
                    "href": "/ovirt-engine/api/icons/28054380-4723-42db-a8e5-fed8a3778199",
                    "id": "28054380-4723-42db-a8e5-fed8a3778199"
                },
                "os": {
                    "boot": {
                        "devices": [
                            "hd",
                            "cdrom"
                        ]
                    },
                    "type": "rhel_7x64"
                },
                "cpu": {
                    "architecture": "x86_64",
                    "topology": {
                        "cores": 1,
                        "threads": 1,
                        "sockets": 8
                    }
                },
                "cpu_shares": 1024
            }
        ]
    }
}
A Hassan
  • 21
  • 5

2 Answers2

0

You can do it as follows:

- name: Wait for VMs to be down ovirt_vms_facts: auth: "{{ ovirt_auth }}" pattern: "name={{ vm_name }}" until: "ovirt_vms[0].status == 'down'" retries: 5 delay: 10

-1

You can do it as below:: - name: Register VM Status debug: msg: "{{ ovirt_vms[0].status }}" register: vm_status until: vm_status.stdout.find("down") != -1 retries: 10 delay: 5

Here it is retrying for 10 times with a delay of 5 seconds.

  • This is just looping the debug task. He'll need to loop through `ovirt_vms_facts` task and perform the `until` conditional check there. – Himal Jul 14 '18 at 18:31
  • Here i am assuming variable "{{ ovirt_vms[0].status }}" getting changed dynamically. ovirt_vms_facts is just a fact collector and after wards it polls for its VM status, where it predicts whether its up or down. – Random_Automation Jul 14 '18 at 19:20
  • The attribute is list and until is failing with this error: fatal: [localhost]: FAILED! => { "msg": "The conditional check 'vm_status.stdout.find(\"down\") != -1' failed. The error was: error while evaluating conditional (vm_status.stdout.find(\"down\") != -1): 'dict object' has no attribute 'stdout', i am a bit confused, what should I do ? – A Hassan Jul 15 '18 at 11:16
  • I tried instead: until: vm_status|down, but I got the following error: fatal: [localhost]: FAILED! => { "msg": "The conditional check 'vm_status|down' failed. The error was: template error while templating string: no filter named 'down'. String: {% if vm_status|down %} True {% else %} False {% endif %}" } – A Hassan Jul 15 '18 at 11:29
  • I tried to parse ovirt_vms using var: and I got the following: – A Hassan Jul 15 '18 at 12:05
  • I have added in the main post the output of ovirt_vms – A Hassan Jul 15 '18 at 12:09
  • How can I loop through ovirt_vms_facts task and perform the until conditional check there – A Hassan Jul 15 '18 at 12:30