Initial commit
This commit is contained in:
commit
d0dad3fbc4
50
README.md
Normal file
50
README.md
Normal file
|
@ -0,0 +1,50 @@
|
|||
kvm_provision Role
|
||||
=========
|
||||
|
||||
Deployes libvirt virtual machines from various template sources.
|
||||
|
||||
|
||||
Requirements
|
||||
------------
|
||||
|
||||
Community libvirt:
|
||||
|
||||
```
|
||||
ansible-galaxy collection install community.libvirt
|
||||
```
|
||||
|
||||
Example Playbook
|
||||
----------------
|
||||
|
||||
```
|
||||
- name: Deploys VM based on template image
|
||||
hosts: localhost
|
||||
gather_facts: yes
|
||||
become: yes
|
||||
vars:
|
||||
pool_dir: "/var/lib/libvirt/images"
|
||||
vm: debian_12
|
||||
vcpus: 2
|
||||
ram_mb: 2048
|
||||
cleanup: no
|
||||
vm_user: beegyoshi
|
||||
vm_pass: stinky
|
||||
net: bridged-network
|
||||
|
||||
tasks:
|
||||
- name: KVM Provision role
|
||||
include_role:
|
||||
name: kvm_provision
|
||||
vars:
|
||||
libvirt_pool_dir: "{{ pool_dir }}"
|
||||
vm_name: "{{ vm }}"
|
||||
vm_vcpus: "{{ vcpus }}"
|
||||
vm_ram_mb: "{{ ram_mb }}"
|
||||
vm_net: "{{ net }}"
|
||||
cleanup_tmp: "{{ cleanup }}"
|
||||
|
||||
```
|
||||
Author Information
|
||||
------------------
|
||||
|
||||
leander@one-button.org
|
12
defaults/main.yml
Normal file
12
defaults/main.yml
Normal file
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
# defaults file for kvm_provision
|
||||
base_image_name: debian12-template.qcow2
|
||||
base_image_dir: "/var/lib/libvirt/images"
|
||||
libvirt_pool_dir: "/var/lib/libvirt/images"
|
||||
vm_name: debain_12_dev
|
||||
vm_vcpus: 2
|
||||
vm_ram_mb: 2048
|
||||
vm_net: bridged-network
|
||||
vm_user: bigppman
|
||||
vm_pass: stoopid
|
||||
cleanup_tmp: no
|
52
meta/main.yml
Normal file
52
meta/main.yml
Normal file
|
@ -0,0 +1,52 @@
|
|||
galaxy_info:
|
||||
author: your name
|
||||
description: your role description
|
||||
company: your company (optional)
|
||||
|
||||
# If the issue tracker for your role is not on github, uncomment the
|
||||
# next line and provide a value
|
||||
# issue_tracker_url: http://example.com/issue/tracker
|
||||
|
||||
# Choose a valid license ID from https://spdx.org - some suggested licenses:
|
||||
# - BSD-3-Clause (default)
|
||||
# - MIT
|
||||
# - GPL-2.0-or-later
|
||||
# - GPL-3.0-only
|
||||
# - Apache-2.0
|
||||
# - CC-BY-4.0
|
||||
license: license (GPL-2.0-or-later, MIT, etc)
|
||||
|
||||
min_ansible_version: 2.1
|
||||
|
||||
# If this a Container Enabled role, provide the minimum Ansible Container version.
|
||||
# min_ansible_container_version:
|
||||
|
||||
#
|
||||
# Provide a list of supported platforms, and for each platform a list of versions.
|
||||
# If you don't wish to enumerate all versions for a particular platform, use 'all'.
|
||||
# To view available platforms and versions (or releases), visit:
|
||||
# https://galaxy.ansible.com/api/v1/platforms/
|
||||
#
|
||||
# platforms:
|
||||
# - name: Fedora
|
||||
# versions:
|
||||
# - all
|
||||
# - 25
|
||||
# - name: SomePlatform
|
||||
# versions:
|
||||
# - all
|
||||
# - 1.0
|
||||
# - 7
|
||||
# - 99.99
|
||||
|
||||
galaxy_tags: []
|
||||
# List tags for your role here, one per line. A tag is a keyword that describes
|
||||
# and categorizes the role. Users find roles by searching for tags. Be sure to
|
||||
# remove the '[]' above, if you add tags to this list.
|
||||
#
|
||||
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
|
||||
# Maximum 20 tags per role.
|
||||
|
||||
dependencies: []
|
||||
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
|
||||
# if you add dependencies to this list.
|
57
tasks/main.yml
Normal file
57
tasks/main.yml
Normal file
|
@ -0,0 +1,57 @@
|
|||
---
|
||||
# tasks file for kvm_provision
|
||||
|
||||
- name: Ensure requirements in place
|
||||
package:
|
||||
name:
|
||||
- guestfs-tools
|
||||
- python3-libvirt
|
||||
state: present
|
||||
become: yes
|
||||
|
||||
- name: Get VMs list
|
||||
community.libvirt.virt:
|
||||
command: list_vms
|
||||
register: existing_vms
|
||||
changed_when: no
|
||||
|
||||
- name: Create VM if not exists
|
||||
block:
|
||||
|
||||
- name: Copy base image to libvirt directory
|
||||
copy:
|
||||
dest: "{{ libvirt_pool_dir }}/{{ vm_name }}.qcow2"
|
||||
src: "{{ libvirt_pool_dir }}/{{ base_image_name }}"
|
||||
force: no
|
||||
remote_src: yes
|
||||
mode: 0660
|
||||
register: copy_results
|
||||
- name: Define vm
|
||||
community.libvirt.virt:
|
||||
command: define
|
||||
xml: "{{ lookup('template', 'vm-template.xml.j2') }}"
|
||||
- name: Configure the image
|
||||
command: |
|
||||
virt-customize -a {{ libvirt_pool_dir }}/{{ vm_name }}.qcow2 \
|
||||
--hostname {{ vm_name }} \
|
||||
--password {{ vm_user }}:password:{{ vm_pass }} \
|
||||
--firstboot-command '/usr/bin/ssh-keygen -A'
|
||||
when: copy_results is changed
|
||||
|
||||
when: "vm_name not in existing_vms.list_vms"
|
||||
|
||||
|
||||
- name: Ensure VM is started
|
||||
community.libvirt.virt:
|
||||
name: "{{ vm_name }}"
|
||||
state: running
|
||||
register: vm_start_results
|
||||
until: "vm_start_results is success"
|
||||
retries: 15
|
||||
delay: 2
|
||||
|
||||
- name: Ensure temporary file is deleted
|
||||
file:
|
||||
path: "/tmp/{{ base_image_name }}"
|
||||
state: absent
|
||||
when: cleanup_tmp | bool
|
52
templates/vm-template.xml.j2
Normal file
52
templates/vm-template.xml.j2
Normal file
|
@ -0,0 +1,52 @@
|
|||
<domain type='kvm'>
|
||||
<name>{{ vm_name }}</name>
|
||||
<memory unit='MiB'>{{ vm_ram_mb }}</memory>
|
||||
<vcpu placement='static'>{{ vm_vcpus }}</vcpu>
|
||||
<os>
|
||||
<type arch='x86_64' machine='pc-q35-5.2'>hvm</type>
|
||||
<boot dev='hd'/>
|
||||
</os>
|
||||
<cpu mode='host-model' check='none'/>
|
||||
<devices>
|
||||
<emulator>/usr/bin/qemu-system-x86_64</emulator>
|
||||
<disk type='file' device='disk'>
|
||||
<driver name='qemu' type='qcow2'/>
|
||||
<source file='{{ libvirt_pool_dir }}/{{ vm_name }}.qcow2'/>
|
||||
<target dev='vda' bus='virtio'/>
|
||||
<address type='pci' domain='0x0000' bus='0x05' slot='0x00' function='0x0'/>
|
||||
</disk>
|
||||
<interface type='network'>
|
||||
<source network='{{ vm_net }}'/>
|
||||
<model type='virtio'/>
|
||||
<address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
|
||||
</interface>
|
||||
<channel type='unix'>
|
||||
<target type='virtio' name='org.qemu.guest_agent.0'/>
|
||||
<address type='virtio-serial' controller='0' bus='0' port='1'/>
|
||||
</channel>
|
||||
<channel type='spicevmc'>
|
||||
<target type='virtio' name='com.redhat.spice.0'/>
|
||||
<address type='virtio-serial' controller='0' bus='0' port='2'/>
|
||||
</channel>
|
||||
<input type='tablet' bus='usb'>
|
||||
<address type='usb' bus='0' port='1'/>
|
||||
</input>
|
||||
<input type='mouse' bus='ps2'/>
|
||||
<input type='keyboard' bus='ps2'/>
|
||||
<graphics type='spice' autoport='yes'>
|
||||
<listen type='address'/>
|
||||
<image compression='off'/>
|
||||
</graphics>
|
||||
<video>
|
||||
<model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
|
||||
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x0'/>
|
||||
</video>
|
||||
<memballoon model='virtio'>
|
||||
<address type='pci' domain='0x0000' bus='0x06' slot='0x00' function='0x0'/>
|
||||
</memballoon>
|
||||
<rng model='virtio'>
|
||||
<backend model='random'>/dev/urandom</backend>
|
||||
<address type='pci' domain='0x0000' bus='0x07' slot='0x00' function='0x0'/>
|
||||
</rng>
|
||||
</devices>
|
||||
</domain>
|
2
tests/inventory
Normal file
2
tests/inventory
Normal file
|
@ -0,0 +1,2 @@
|
|||
localhost
|
||||
|
5
tests/test.yml
Normal file
5
tests/test.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
- hosts: localhost
|
||||
remote_user: root
|
||||
roles:
|
||||
- kvm_provision
|
Loading…
Reference in New Issue
Block a user