如何使ansible定期探查一个事实值,并且仅在该值发生更改时继续进行操作?

时间:2018-07-14 11:54:18

标签: ansible ovirt

我有以下代码片段,其中我使用ansible在虚拟机上安装了操作系统,完成后它停止了VM,因此我可以继续其余的任务,我正在从Red Hat虚拟化管理器中收集有关状态虚拟机,我想一直等到虚拟机的状态从上到下变化,以便继续进行操作,如何编写此代码?:

# 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

我试图解析从ovirt_vms_facts收集的ovirt_vms,并且得到以下信息:

{
    "_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
            }
        ]
    }
}

2 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

- 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 :(得分:-1)

您可以执行以下操作:       sys.stdout = use_the_default_one()

此处重试10次,延迟5秒。