简体   繁体   English

VM 对使用 ansible shell 任务执行的 virsh 命令不可见

[英]VM's are not Visible to virsh command executed using ansible shell task

I am trying to save the state of all running VM's before I can shut down the host.我试图在关闭主机之前保存所有正在运行的 VM 的状态。

virsh save <domain-name> <filetosave>

The idea is to restore the VM's when I bring back the host again.这个想法是当我再次带回主机时恢复虚拟机。

I am using ansible virt module to determine the running VM's.我正在使用 ansible virt模块来确定正在运行的 VM。 There is no command available for saving the virtual machine using virt module hence using shell module like below没有可用于使用 virt 模块保存虚拟机的命令,因此使用如下所示的 shell 模块

 tasks:

  - name: Gathering Facts
    setup:

  # Listing VMs
  - name: list all VMs
    virt:
      command: list_vms
    register: all_vms

  - name: list only running VMs
    virt:
      command: list_vms
      state: running
    register: running_vms

  - name: Print All VM'state
    debug:
      msg: |
        VM's: {{ all_vms.list_vms }},
        Active VM's: {{ running_vms.list_vms }}

  - name: Save Running VM's
    shell: >
      virsh save {{ item }} ~/.mykvm/{{item}}.save
    args:
      chdir: /home/sharu
      executable: /bin/bash
    loop:
      "{{ running_vms.list_vms }}"
    register: save_result
  - debug:
      var: save_result

Output:输出:

TASK [list all VMs] 
*********
ok: [192.168.0.113]

TASK [list only running VMs] 
*********
ok: [192.168.0.113]

TASK [Print All VM'state] 
*********
ok: [192.168.0.113] => {
    "msg": "VM's: [u'win10-2', u'ubuntu18.04', u'ubuntu18.04-2', u'win10'],\nActive VM's: [u'win10-2']\n"
}

TASK [Save Running VM's] 
*********
failed: [192.168.0.113] (item=win10-2) => {"ansible_loop_var": "item", "changed": true, "cmd": "virsh save win10-2 ~/.mykvm/win10-2.save\n", "delta": "0:00:00.101916", "end": "2019-12-30 01:19:32.205584", "item": "win10-2", "msg": "non-zero return code", "rc": 1, "start": "2019-12-30 01:19:32.103668", "stderr": "error: failed to get domain 'win10-2'", "stderr_lines": ["error: failed to get domain 'win10-2'"], "stdout": "", "stdout_lines": []}

PLAY RECAP 
******************
192.168.0.113              : ok=5    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Notice the virt module was able to get the information regarding the running domain's(VM's) but the shell command failed to get the domain name.请注意,virt 模块能够获取有关正在运行的域(VM)的信息,但 shell 命令无法获取域名。 It seems like the VM's are not visible for Ansible's shell task Ansible 的 shell 任务似乎看不到 VM

To debug further I executed the Ansible ad-hoc command below but got the blank result为了进一步调试,我执行了下面的 Ansible ad-hoc 命令,但得到了空白结果

$ ansible -i ../hosts.ini RIG3600 -m shell -a "virsh list --all"
192.168.0.113 | CHANGED | rc=0 >>
 Id   Name   State
--------------------

The same command works fine in the Linux shell相同的命令在 Linux shell 中运行良好

$ virsh list --all

 Id   Name            State
--------------------------------
 3    win10-2         running
 -    ubuntu18.04     shut off
 -    ubuntu18.04-2   shut off
 -    win10           shut off

Am using Ansible version 2.8.3 Not sure am I missing something here, any help is much appreciated.我正在使用 Ansible 版本 2.8.3 不确定我在这里遗漏了什么,非常感谢任何帮助。

The virt module, as documented , defaults to using the libvirt URI qemu:///system . virt模块, 如文档所示,默认使用 libvirt URI qemu:///system

The virsh command, on the other hand, when run as a non-root user, defaults to qemu:///session .另一方面, virsh命令在以非 root 用户身份运行时,默认为qemu:///session

In other words, your virsh command is not talking to the same libvirt instance as your virt tasks.换句话说,您的virsh命令与您的virt任务不与同一个 libvirt 实例对话。

You can provide an explicit URI to the virsh command using the -c ( --connect ) option:您可以使用-c ( --connect ) 选项为virsh命令提供显式 URI:

virsh -c qemu:///system ...

Or by setting the LIBVIRT_DEFAULT_URI environment variable:或者通过设置LIBVIRT_DEFAULT_URI环境变量:

  - name: Save Running VM's
    shell: >
      virsh save {{ item }} ~/.mykvm/{{item}}.save
    args:
      chdir: /home/sharu
      executable: /bin/bash
    environment:
      LIBVIRT_DEFAULT_URI: qemu:///system
    loop:
      "{{ running_vms.list_vms }}"
    register: save_result

(You can also set environment on the play instead of on an individual task). (您也可以在游戏中设置environment而不是单个任务)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM