ansible-collections / community.libvirt

Manage libvirt with Ansible
http://galaxy.ansible.com/community/libvirt
GNU General Public License v3.0
62 stars 43 forks source link

Using plugin with windows vm's #92

Open dennisvanderveen opened 3 years ago

dennisvanderveen commented 3 years ago
SUMMARY

How to use the inventory plugin with windows vm? when using ansible-inventory not many hostvars are showing to use, only ansible_connection and ansible_libvirt_uri

ansible-inventory -i kvm.yml --host demo1
{
    "ansible_connection": "community.libvirt.libvirt_qemu",
    "ansible_libvirt_uri": "qemu:///system"

when using ansible ad-hoc commands or using the inventory plugin in playbooks it defaults to /bin/sh

I found https://github.com/ansible-collections/community.libvirt/pull/37 where windows is added to the connection plugin, but for me it's unclear how to use this.

ISSUE TYPE
COMPONENT NAME

community.libvirt.libvirt_qemu connection plugin

ANSIBLE VERSION
ansible --version
ansible 2.9.26
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/eos/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Aug 12 2021, 07:06:15) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
csmart commented 2 years ago

Hi @dennisvanderveen, thanks for reporting this. I am not too familiar with Windows, but I'll see if I can set something up to replicate your issue.

In the mean time, can I check that you have QEMU guest agent up and running in your Windows VMs and that they have a serial device for it?

And can you manually run a command against your host to create a file in it at C:\123.txt?

virsh qemu-agent-command windows '{"execute":"guest-exec", "arguments":{"path":"cmd.exe","arg":["/c","echo 123 > c:\\123.txt"]}}' --pretty

Seems like the issue might be as you say, that the default command is /bin/sh rather than something like cmd.exe but it's not clear to me at a cursory glance how that's meant to be configured.

I'll have a look and see what I can find.

-c

dennisvanderveen commented 2 years ago

Qemu agent is up and running for the windows vm.

virsh qemu-agent-command w10 '{"execute":"guest-exec", "arguments":{"path":"cmd.exe","arg":["/c","echo 123 > c:\\123.txt"]}}' --pretty
{
  "return": {
    "pid": 1080
  }
}
odyssey4me commented 2 years ago

If you add the argument ansible_executable or perhaps just executable with the value cmd.exe (or perhaps the full path) then does that make it work? See https://github.com/ansible-collections/community.libvirt/blob/3c66295aae8d082f45829da62b2b2573c1b58036/plugins/connection/libvirt_qemu.py#L30-L34

dennisvanderveen commented 2 years ago

I tried and failed, with and without full path. Both variables. Also tried it as a variable in a play It doesn't pickup the executable and keeps returning to /bin/sh. Also checked if my libvirt_qemu.py was the correct version and it is.

csmart commented 2 years ago

I found the same thing and I'm not sure how this is mean to work. @daveol do you have any idea on this one and what we should try? Thanks!

daveol commented 2 years ago

If I remember this correctly (it has been a while), you will need to set ansible_shell_type to either cmd or powershell for it to work correctly (like openssh for windows).

csmart commented 2 years ago

@daveol thanks so much for the response, really appreciated! That seems to make the code hit the right path now, cheers.

@dennisvanderveen are you able to give that a go? I have done a basic test which fails due to differences in the Windows Python distribution I guess (I probably need to use a win_ module), but I expect that you'll know better than me.

$ ansible -i ../../kvm.yml all --limit win10 -m ping -e ansible_shell_type=cmd -e "ansible_python_interpreter=C:\Users\csmart\AppData\Local\Programs\Python\Python39\python.exe"
[WARNING]: You are running the development version of Ansible. You should only run Ansible from "devel" if you are modifying the Ansible engine, or trying out features under development. This is a rapidly changing source of code and can
become unstable at any point.
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ModuleNotFoundError: No module named 'grp'
win10 | FAILED! => {
    "changed": false,
    "module_stderr": "Traceback (most recent call last):\r\n  File \"C:\\Windows\\TEMP\\ansible-tmp-1643526789.9121494-73396-242370585012302\\AnsiballZ_ping.ps1\", line 107, in <module>\r\n    _ansiballz_main()\r\n  File \"C:\\Windows\\TEMP\\ansible-tmp-1643526789.9121494-73396-242370585012302\\AnsiballZ_ping.ps1\", line 99, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"C:\\Windows\\TEMP\\ansible-tmp-1643526789.9121494-73396-242370585012302\\AnsiballZ_ping.ps1\", line 44, in invoke_module\r\n    from ansible.module_utils import basic\r\n  File \"<frozen zipimport>\", line 259, in load_module\r\n  File \"C:\\Windows\\TEMP\\ansible_ping_payload_3ebhir5s\\ansible_ping_payload.zip\\ansible\\module_utils\\basic.py\", line 39, in <module>\r\nModuleNotFoundError: No module named 'grp'\r\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}
dennisvanderveen commented 2 years ago
ansible w10 -i kvm.yml -e ansible_shell_type=cmd -m win_ping
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
w10 | SUCCESS => {
    "changed": false,
    "ping": "pong"

also adding it to host_vars makes it work

cat host_vars/w10.yml 
ansible_shell_type: cmd
ansible w10 -i kvm.yml -m win_ping
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
w10 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

So thx for all the help. Maybe is good to add this to the comments of libvirt_qemu.py and mayby to ansible-doc aswell?

csmart commented 2 years ago

@dennisvanderveen if you have time, I'd appreciate if you could cast your eye over the PR linked above to clarify the docs. It seems to me that ansible_executable as mentioned in the ansible doc is not correct, so I've changed this. Cheers!

dennisvanderveen commented 2 years ago

Will do Chris! Will let you know

csmart commented 2 years ago

Thanks, in particular I think we should clarify whether powershell also works? If you are able to test that, it would be great!

dennisvanderveen commented 2 years ago

I can confirm using powershell works also

ansible w10 -i kvm.yml -e ansible_shell_type=powershell -m win_ping
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
w10 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}