Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using plugin with windows vm's #92

Open
dennisvanderveen opened this issue Oct 6, 2021 · 12 comments
Open

Using plugin with windows vm's #92

dennisvanderveen opened this issue Oct 6, 2021 · 12 comments

Comments

@dennisvanderveen
Copy link

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 #37 where windows is added to the connection plugin, but for me it's unclear how to use this.

ISSUE TYPE
  • Documentation Report
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
Copy link
Collaborator

csmart commented Jan 2, 2022

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
Copy link
Author

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
Copy link
Collaborator

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

executable:
description: Shell to use for execution inside container.
default: /bin/sh
vars:
- name: ansible_executable

@dennisvanderveen
Copy link
Author

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
Copy link
Collaborator

csmart commented Jan 28, 2022

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
Copy link
Contributor

daveol commented Jan 29, 2022

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
Copy link
Collaborator

csmart commented Jan 30, 2022

@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
Copy link
Author

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
Copy link
Collaborator

csmart commented Jan 31, 2022

@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
Copy link
Author

Will do Chris! Will let you know

@csmart
Copy link
Collaborator

csmart commented Jan 31, 2022

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

@dennisvanderveen
Copy link
Author

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"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants