如何使ansible定期探测事实值并仅在值发生变化时继续?
我在下面的代码片段中使用ansible在虚拟机上安装操作系统,完成后会停止VM,以便继续执行其余任务,我正在从red hat virtualization manager收集有关VM状态的事实,我想一直等待,直到虚拟机的状态从上到下发生变化,这样我才能继续,我如何编写代码如何使ansible定期探测事实值并仅在值发生变化时继续?,ansible,ovirt,Ansible,Ovirt,我在下面的代码片段中使用ansible在虚拟机上安装操作系统,完成后会停止VM,以便继续执行其余任务,我正在从red hat virtualization manager收集有关VM状态的事实,我想一直等待,直到虚拟机的状态从上到下发生变化,这样我才能继续,我如何编写代码 # I am kickstarting the VM - name: Installing OS ovirt_vms: state: running name: "{{ vm_name }}"
# 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_事实中收集的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
}
]
}
}
您可以按以下方式执行:
-名称:注册虚拟机状态
调试:
msg:{{ovirt_vms[0]。状态}”
寄存器:vm_状态
直到:vm_status.stdout.find(“down”)!=-1.
重试次数:10次
延迟:5次
在这里,它将以5秒的延迟重试10次。您可以按如下方式执行:
-name:等待虚拟机关闭
ovirt_vms_事实:
认证:{{ovirt_auth}}”
模式:“name={{vm_name}}”
直到:“ovirt_vms[0]。状态==‘关闭’”
重试次数:5次
延迟:10
这只是循环调试任务。他需要循环执行ovirt\u vms\u facts
任务并执行,直到在那里进行条件检查。这里我假设变量“{ovirt\u vms[0].status}”动态更改。ovirt_vms_facts只是一个事实收集器,它会在轮询其VM状态后,预测其VM状态是向上还是向下。属性为list,直到失败,并出现以下错误:致命:[localhost]:失败!=>{“msg”:“条件检查'vm\u status.stdout.find(\“down\”)!=-1'失败。错误是:计算条件时出错(vm\u status.stdout.find(\“down\”)!=-1:'dict object'没有属性'stdout',我有点困惑,我应该怎么办?我尝试了:直到:vm\u status | down,但我得到了以下错误:致命:[localhost]:FAILED!=>{“msg”:“条件检查'vm|status | down'失败。错误是:模板字符串时模板错误:没有名为'down'的筛选器。字符串:{%if-vm|status | down%}True{%else%}False{%endif%}我试图使用var:解析ovirt|vms,得到以下结果: