ansible-community / molecule-libvirt Goto Github PK
View Code? Open in Web Editor NEWMolecule LibVirt Provider
Home Page: https://github.com/ansible-community/molecule-libvirt/issues/13#issuecomment-1372615418
License: MIT License
Molecule LibVirt Provider
Home Page: https://github.com/ansible-community/molecule-libvirt/issues/13#issuecomment-1372615418
License: MIT License
When libvirt is not yet correctly configured for current user, you will get an error like:
TASK [Create network molecule network] ***********************************************************************************************************************
failed: [localhost] (item=instance) => {"ansible_loop_var": "item", "changed": false, "item": {"name": "instance"}, "msg": "authentication unavailable: no polkit agent available to authenticate action 'org.libvirt.unix.manage'"}
This is usually fixed by running:
usermod --append --groups libvirt `whoami`
# second command is really needed otherwise current session will not get the new groups.
# it can get even worse when using ssh as even closing the session and restarting it may not work due to ssh connection caching in the client
newgrp libvirt
# i even had to reboot a machine to convince it to list libvirt when running `groups`
Can we do something to make molecule more aware of broken setups and hint the user about ways to fix his environment?
We should add some code inside the driver sanity check method that checks for this and prevent even starting execution of a scenario if libvirt is broken.
I am unable to maintain this driver as I am not using it, so I am looking for maintainers.
I can keep providing support for integration with molecule itself but not around driver specifics.
I would work noting that the top priority would be to setup CI jobs.
Hi guys,
I have simple issue, task 'Get instance(s) ip address' in create.yml playbook always fail on my working station. It could be some performance issue on my side, but if I increase number of retries=20 or delay=5 my problem is solved. Could you please increase some of that? It will help me a lot.
Hi,
At first, thank you for providing this plugin, and secondly, I noticed that "item." is missing a couple of places, so that some definitions in molecule.yml are ignored when creating vms.
grep -ir "default(" | grep -v item
molecule_libvirt/playbooks/create.yml: {{ disk_size | default(default_disk_size ) }}"
molecule_libvirt/playbooks/templates/vm/vm.xml.j2: <memory unit='GiB'>{{ memory_size | default(default_memory_size) }}</memory>
molecule_libvirt/playbooks/templates/vm/vm.xml.j2: <currentMemory unit='GiB'>{{ memory_size | default(default_memory_size) }}</currentMemory>
molecule_libvirt/playbooks/templates/vm/vm.xml.j2: <vcpu placement='static'>{{ vcpu | default(default_vcpu) }}</vcpu>
Also, would you consider adding (or accepting) a couple of miscellaneous start and stop functions corresponding to virsh start and shutdown.
Regards,
Lars Melwyn
Hi!
I need to customize the user-data.j2
file. What's the best way to do this? I'm using python's virtualenv.
I'm currently changing the file directly in $PROJECT_FOLDER/.venv/lib/python3.10/site-packages/molecule_libvirt/playbooks/templates/user-data.j2
, but I'd like to leave the change versioned in git.
Another issue related to user permissions
TASK [Start molecule vm(s)] **********************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: libvirt.libvirtError: Cannot access storage file '/home/ssbarnea/.local/molecule/default/vms/instance.qcow2' (as uid:107, gid:107): Permission denied
failed: [localhost] (item=instance) => {"ansible_loop_var": "item", "changed": false, "item": {"name": "instance"}, "msg": "Cannot access storage file '/home/ssbarnea/.local/molecule/default/vms/instance.qcow2' (as uid:107, gid:107): Permission denied"}
This happened after running newgrp ssbarnea & molecule create
on fresh scenario.
[ssbarnea@leno molecule-libvirt]$ ls -la /home/ssbarnea/.local/molecule/default/vms/
total 659684
drwxr-xr-x. 1 ssbarnea ssbarnea 74 May 16 11:09 .
drwxr-xr-x. 1 ssbarnea ssbarnea 6 May 15 17:43 ..
-rw-r--r--. 1 ssbarnea libvirt 374784 May 16 11:09 instance-cloud-init.iso
-rw-r--r--. 1 ssbarnea libvirt 16108814336 May 16 11:09 instance.qcow2
It seems that the qcow2 image was created with wrong permissions as the libvirt group does not have write access to it and it obviously need that.
umask reports 0022
for my current user which explains why the group did not had access but in out case we do want to use a mask 0002
at least for this file.
I'm getting the error
"The
libvirt
module is not importable. Check the requirements."
in the TASK [Get list of all instances]
. How to solve this?
collections:
- name: community.general
version: ">=2,<3"
- name: community.libvirt
- name: community.crypto
- name: ansible.posix
- name: ansible.netcommon
---
dependency:
name: galaxy
driver:
name: libvirt
platforms:
- name: instance
cpu: 2
cpu_model: "qemu64"
image_url: "https://mirror.cedia.org.ec/fedora/linux/releases/35/Cloud/x86_64/images/Fedora-Cloud-Base-35-1.2.x86_64.qcow2"
disk_size: "10G"
ssh_port: 22
memory_size: "1" # in GB
provisioner:
name: ansible
verifier:
name: ansible
$ ansible --version
ansible [core 2.13.0]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/myuser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/myuser/projetos/ansible-setup-workstation/.venv/lib/python3.10/site-packages/ansible
ansible collection location = /home/myuser/.ansible/collections:/usr/share/ansible/collections
executable location = /home/myuser/projetos/ansible-setup-workstation/.venv/bin/ansible
python version = 3.10.4 (main, Mar 25 2022, 00:00:00) [GCC 11.2.1 20220127 (Red Hat 11.2.1-9)]
jinja version = 3.1.2
libyaml = True
$ molecule --version
molecule 3.6.1 using python 3.10
ansible:2.13.0
delegated:3.6.1 from molecule
docker:1.1.0 from molecule_docker requiring collections: community.docker>=1.9.1
libvirt:0.0.5 from molecule_libvirt
$ python --version
Python 3.10.4
OS verion: Fedora Workstation 35
CI jobs needs to be run ASAP, prefering Travis as we already have a template to follow.
If we discover that Travis does not allow us to use libvirt we can fallback to something else, including using rdo and zuul.
The virtual machine does not start.
FAILED - RETRYING: [localhost]: Get instance(s) ip address (1 retries left).Result was: {
"attempts": 10,
"changed": true,
"cmd": "virsh --connect qemu:///system domifaddr instance | grep 52:54:00:ab:cd: | awk '{ print $4 }' | sed 's/\\/.*//'\n",
"delta": "0:00:00.028730",
"end": "2022-06-16 19:10:10.203963",
"invocation": {
"module_args": {
"_raw_params": "virsh --connect qemu:///system domifaddr instance | grep 52:54:00:ab:cd: | awk '{ print $4 }' | sed 's/\\/.*//'\n",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": false
}
},
"msg": "",
"rc": 0,
"retries": 11,
"start": "2022-06-16 19:10:10.175233",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
<127.0.0.1> EXEC /bin/sh -c 'echo ~mhagnumdw && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/mhagnumdw/.ansible/tmp `"&& mkdir "` echo /home/mhagnumdw/.ansible/tmp/ansible-tmp-1655417412.2326565-58511-220510289016393 `" && echo ansible-tmp-1655417412.2326565-58511-220510289016393="` echo /home/mhagnumdw/.ansible/tmp/ansible-tmp-1655417412.2326565-58511-220510289016393 `" ) && sleep 0'
Using module file /home/mhagnumdw/projetos/ansible-setup-workstation/.venv/lib/python3.10/site-packages/ansible/modules/command.py
<127.0.0.1> PUT /home/mhagnumdw/.ansible/tmp/ansible-local-57192ogu4wtzr/tmpw43ce8a3 TO /home/mhagnumdw/.ansible/tmp/ansible-tmp-1655417412.2326565-58511-220510289016393/AnsiballZ_command.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/mhagnumdw/.ansible/tmp/ansible-tmp-1655417412.2326565-58511-220510289016393/ /home/mhagnumdw/.ansible/tmp/ansible-tmp-1655417412.2326565-58511-220510289016393/AnsiballZ_command.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/home/mhagnumdw/projetos/ansible-setup-workstation/.venv/bin/python /home/mhagnumdw/.ansible/tmp/ansible-tmp-1655417412.2326565-58511-220510289016393/AnsiballZ_command.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/mhagnumdw/.ansible/tmp/ansible-tmp-1655417412.2326565-58511-220510289016393/ > /dev/null 2>&1 && sleep 0'
failed: [localhost] (item=instance) => {
"ansible_loop_var": "item",
"attempts": 10,
"changed": true,
"cmd": "virsh --connect qemu:///system domifaddr instance | grep 52:54:00:ab:cd: | awk '{ print $4 }' | sed 's/\\/.*//'\n",
"delta": "0:00:00.026336",
"end": "2022-06-16 19:10:12.422628",
"invocation": {
"module_args": {
"_raw_params": "virsh --connect qemu:///system domifaddr instance | grep 52:54:00:ab:cd: | awk '{ print $4 }' | sed 's/\\/.*//'\n",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": false
}
},
"item": {
"cpu": 2,
"cpu_model": "qemu64",
"disk_size": "10G",
"image_url": "https://download.fedoraproject.org/pub/fedora/linux/releases/34/Cloud/x86_64/images/Fedora-Cloud-Base-34-1.2.x86_64.qcow2",
"memory_size": "1",
"name": "instance",
"ssh_port": 22
},
"msg": "",
"rc": 0,
"start": "2022-06-16 19:10:12.396292",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
The virtual machine is stopped at boot.
$ ls -lah /home/mhagnumdw/.local/molecule/default/vms/
total 2,9M
drwxr-xr-x. 1 mhagnumdw mhagnumdw 110 jun 16 19:09 .
drwxr-xr-x. 1 mhagnumdw mhagnumdw 6 jun 16 18:18 ..
-rw-r--r--. 1 qemu qemu 366K jun 16 19:09 instance-cloud-init.iso
-rw-rw-r--. 1 mhagnumdw mhagnumdw 0 jun 16 19:09 .instance-expanded
-rw-r--r--. 1 qemu qemu 16G jun 16 19:09 instance.qcow2
sudo virsh dumpxml instance
<domain type='kvm' id='6'>
<name>instance</name>
<uuid>b4e255d0-c7f2-48be-a49c-8b929ffc80f2</uuid>
<memory unit='KiB'>1048576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
<vcpu placement='static'>1</vcpu>
<resource>
<partition>/machine</partition>
</resource>
<os>
<type arch='x86_64' machine='pc-i440fx-6.1'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
<vmport state='off'/>
</features>
<cpu mode='custom' match='exact' check='full'>
<model fallback='forbid'>qemu64</model>
<feature policy='require' name='x2apic'/>
<feature policy='require' name='hypervisor'/>
<feature policy='require' name='lahf_lm'/>
<feature policy='disable' name='svm'/>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/home/mhagnumdw/.local/molecule/default/vms/instance.qcow2' index='2'/>
<backingStore/>
<target dev='vda' bus='virtio'/>
<alias name='virtio-disk0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/home/mhagnumdw/.local/molecule/default/vms/instance-cloud-init.iso' index='1'/>
<backingStore/>
<target dev='hda' bus='ide'/>
<readonly/>
<alias name='ide0-0-0'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<controller type='usb' index='0' model='piix3-uhci'>
<alias name='usb'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'>
<alias name='pci.0'/>
</controller>
<controller type='ide' index='0'>
<alias name='ide'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='virtio-serial' index='0'>
<alias name='virtio-serial0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</controller>
<interface type='network'>
<mac address='52:54:00:ab:cd:47'/>
<source network='molecule-libvirt' portid='99ec45a6-5fd6-4126-8aa5-26690c66f065' bridge='molecule-br0'/>
<target dev='vnet5'/>
<model type='virtio'/>
<alias name='net0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<source path='/dev/pts/4'/>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
<alias name='serial0'/>
</serial>
<console type='pty' tty='/dev/pts/4'>
<source path='/dev/pts/4'/>
<target type='serial' port='0'/>
<alias name='serial0'/>
</console>
<channel type='unix'>
<source mode='bind' path='/var/lib/libvirt/qemu/channel/target/domain-6-instance/org.qemu.guest_agent.0'/>
<target type='virtio' name='org.qemu.guest_agent.0' state='disconnected'/>
<alias name='channel0'/>
<address type='virtio-serial' controller='0' bus='0' port='1'/>
</channel>
<channel type='spicevmc'>
<target type='virtio' name='com.redhat.spice.0' state='disconnected'/>
<alias name='channel1'/>
<address type='virtio-serial' controller='0' bus='0' port='2'/>
</channel>
<input type='tablet' bus='usb'>
<alias name='input0'/>
<address type='usb' bus='0' port='1'/>
</input>
<input type='mouse' bus='ps2'>
<alias name='input1'/>
</input>
<input type='keyboard' bus='ps2'>
<alias name='input2'/>
</input>
<graphics type='spice' port='5900' autoport='yes' listen='127.0.0.1'>
<listen type='address' address='127.0.0.1'/>
<image compression='off'/>
</graphics>
<sound model='ich6'>
<alias name='sound0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</sound>
<audio id='1' type='spice'/>
<video>
<model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
<alias name='video0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<alias name='balloon0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</memballoon>
</devices>
<seclabel type='dynamic' model='selinux' relabel='yes'>
<label>system_u:system_r:svirt_t:s0:c54,c201</label>
<imagelabel>system_u:object_r:svirt_image_t:s0:c54,c201</imagelabel>
</seclabel>
<seclabel type='dynamic' model='dac' relabel='yes'>
<label>+107:+107</label>
<imagelabel>+107:+107</imagelabel>
</seclabel>
</domain>
Same as the README project homepage.
---
dependency:
name: galaxy
driver:
name: libvirt
platforms:
- name: instance
cpu: 2
cpu_model: "qemu64"
image_url: "https://download.fedoraproject.org/pub/fedora/linux/releases/34/Cloud/x86_64/images/Fedora-Cloud-Base-34-1.2.x86_64.qcow2"
disk_size: "10G"
ssh_port: 22
memory_size: "1" # in GB
provisioner:
name: ansible
verifier:
name: ansible
$ molecule --version
molecule 3.6.1 using python 3.10
ansible:2.13.0
delegated:3.6.1 from molecule
docker:1.1.0 from molecule_docker requiring collections: community.docker>=1.9.1
libvirt:0.0.5 from molecule_libvirt
molecule -vvv --debug converge
Can someone help me?
Hello! I'm having an issue in task "Expand original image on the new sized volume" with volume cloud images of CentOS. For example CentOS-7-x86_64-GenericCloud-2111.qcow2 have volume with name 'vda1'. So it will never changed, becase it's hardcoded 'sda1'.
I'm prepared pull request with fix #52
List of small chores we should take care of:
I've been introducing myself to molecule and the vagrant integration worked pretty good. As I move to make this something working on my infrastructure, vagrant was not a solution so I tried to use this libvirt module. Since I had to use a "remote" libvirt connection, I hit a few walls. I'd like to contribute here and maybe use this issue as a discussion.
First thing, making libvirt accessible remotely was not well documented. That might be #1 thing to document. Through lot of searches, finally found out how to make it happen.
BTW, I'm doing this from OSX at this time, not quite on linux yet.
1 - Add ?socket=/var/run/libvirt/libvirt-sock
to the qemu line was necessary. But that changes was needed everywhere in create/destroy yaml files.
vars:
qemu_uri: "{{ 'qemu+ssh://'+item.libvirt_user+'@'+item.libvirt_host+'/system' if item.libvirt_host is defined else libvirt_uri }}?socket=/var/run/libvirt/libvirt-sock"
2 - I had to remove the sanity_checks. The problem is that those sanity checks are done "locally" and not on the remote server.
3 - vmport feature in the vm.xml template is not working on Centos 7.
I haven't been able to create a VM yet successfully yet, it is stuck in "booting". Can't find the harddrive.
I'm not sure if this needs changed by default. I'm not sure if it's intended that the create/destroy playbooks have to be modified by the user. Again, I'm new to molecule.
Apparently I went into:
TASK [Create an image shell with the desired size] ***********************************************************************************************************
changed: [localhost] => (item=instance)
TASK [Expand original image on the new sized volume] *********************************************************************************************************
failed: [localhost] (item=instance) => {"ansible_loop_var": "item", "changed": true, "cmd": "umask 0002\nvirt-resize --quiet --expand /dev/sda1 instance.qcow2 instance-temp.qcow2\n", "delta": "0:00:02.332616", "end": "2021-05-16 11:42:46.732044", "item": {"name": "instance"}, "msg": "non-zero return code", "rc": 1, "start": "2021-05-16 11:42:44.399428", "stderr": "virt-resize: error: You cannot use --expand when there is no surplus space \nto expand into. You need to make the target disk larger by at least 32.0K.\n\nIf reporting bugs, run virt-resize with debugging enabled and include the \ncomplete output:\n\n virt-resize -v -x [...]", "stderr_lines": ["virt-resize: error: You cannot use --expand when there is no surplus space ", "to expand into. You need to make the target disk larger by at least 32.0K.", "", "If reporting bugs, run virt-resize with debugging enabled and include the ", "complete output:", "", " virt-resize -v -x [...]"], "stdout": "", "stdout_lines": []}
If I understand correctly, resize should be called only when needed and calling it when image is already at correct size would produce this error.
$ molecule test -s kvm
TASK [Allow qemu process read qcow2 images in home directory] ******************
failed: [localhost -> localhost] (item=centos7-instance) => {"ansible_loop_var": "item", "changed": false, "cmd": "/usr/bin/setfacl --test -m user:qemu:x /home/user", "item": {"image_url": "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2", "name": "centos7-instance"}, "msg": "setfacl:
Option -m: Invalid argument near character 6", "rc": 2, "stderr": "setfacl: Option -m: Invalid argument near character 6\n", "stderr_lines": ["setfacl: Option -m: Invalid argument near character 6"], "stdout": "", "stdout_lines": []}
from running molecule.yml with a minimum of params:
cat molecule/kvm/molecule.yml
---
dependency:
name: galaxy
options:
ignore-certs: True
ignore-errors: True
driver:
name: libvirt
platforms:
- name: centos7-instance
image_url: "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2"
...
Other infro:
$ ansible --version
ansible 2.10.17
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/user/.local/lib/python3.8/site-packages/ansible
executable location = /home/user/.local/bin//ansible
python version = 3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 9.4.0]
$ ls -lh /var/lib | grep libvirt
drwxr-xr-x 1 root root 56 апр 28 16:45 libvirt
$ ls -lh /var/lib/libvirt | grep images
drwx--x--x 1 root root 0 сен 27 2020 images
# Well, I have changed permissions of the folders, but it didn't helped. Still failed on -m option
$ sudo chmod 755 /var/lib/libvirt
$ sudo chmod 755 /var/lib/libvirt/images
$ cat /etc/group | grep qemu
libvirt-qemu:x:64055:libvirt-qemu
$ cat /etc/group | grep user | grep qemu
libvirt-qemu:x:64055:libvirt-qemu,user
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.4 LTS
Release: 20.04
Codename: focal
Everything works fine from virt-manager, but molecule-libvirt not.
May be I'll check this later, but at first here is a bug report.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.