add the current state of the vmsetup

This commit is contained in:
Carl Suckfuell 2022-06-16 20:43:06 +02:00
commit d3675f34bf
19 changed files with 985 additions and 0 deletions

View file

@ -0,0 +1,8 @@
---
image_url: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-nocloud-amd64.qcow2
image_checksum: fd77540aa77f4f5ed3a817d530adfc52d142e93e61c73a85f15422a68c56dcbd39799e5bb2195e521f99a8fa301fa6bf07a478cd27bd380d4c7054901b4c8256
base_image: "{{ image_url | urlsplit('path') | basename }}"
images_dir: /var/vm/
os: debian11
vm_host_bridge_interface: eno1
recreate: false

View file

@ -0,0 +1,17 @@
#!/usr/bin/python
# macgen.py script to generate a MAC address for guests on Xen
# from https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Virtualization/sect-Virtualization-Tips_and_tricks-Generating_a_new_unique_MAC_address.html
# usefull to generate the mac for a virtual machine
#
# run
# python2 macgen.py
import random
#
def randomMAC():
mac = [ 0x00, 0x16, 0x3e,
random.randint(0x00, 0x7f),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff) ]
return ':'.join(map(lambda x: "%02x" % x, mac))
#
print(randomMAC())

View file

@ -0,0 +1,29 @@
---
- name: create mac address
local_action:
module: command
cmd: python macgen.py
chdir: "{{ role_path }}/files"
register: macgen
check_mode: false
- name: create folder for new vm
local_action:
module: file
path: "./host_vars/{{ vmname }}"
state: directory
- name: create hostvars for new vm
local_action:
module: template
src: vm_hostvars.j2
dest: ./host_vars/{{ vmname }}/vars.yml
#mode: 0666
- name: add vm to hosts
local_action:
module: lineinfile
path: hosts
insertafter: '^\[vms\]'
line: "{{ vmname }}"

View file

@ -0,0 +1,73 @@
---
- name: check if vm name exists in hostvars
local_action: stat path="host_vars/{{ vmname }}"
register: register_name
- name: Stop if host_vars exist
debug:
msg: "The file or directory exists"
failed_when: register_name.stat.exists
when:
- register_name.stat.exists
- not recreate
- name: add new vm to hostvars
include_tasks: create_hostvars.yml
when: not recreate
- name: install libvirt and co
package:
name:
- libvirt-daemon-system
- qemu-system
- virtinst
- qemu-utils
- libvirt-clients
- genisoimage
state: latest
become: true
- name: Create images directory
file:
path: '{{ images_dir }}'
state: directory
owner: libvirt-qemu
group: libvirt-qemu
become: true
- name: check for cloudimage
find:
age: -26w
path: '{{ images_dir }}'
pattern: '{{ base_image }}'
register: recent_cloudimage
- debug:
msg: "{{ recent_cloudimage.matched }}"
- name: download cloud image template, if none found or to old
get_url:
url: '{{ image_url }}'
dest: '{{ images_dir }}'
checksum: 'sha512:{{ image_checksum }}'
when: not recent_cloudimage.matched
- name: Create VM image from base image
command: qemu-img create -b {{ base_image }} -f qcow2 -F qcow2 {{ images_dir }}{{ vmname }}.img {{ image_capacity }}
- name: Create user-data
template:
src: user-data.j2
dest: '{{ images_dir }}/user-data'
- name: Create meta-data
template:
src: meta-data.j2
dest: '{{ images_dir }}/meta-data'
- name: Create cloud-init configuration image
command: genisoimage -output {{ images_dir }}/{{ vmname }}-cidata.iso -V cidata -r -J {{ images_dir }}/user-data {{ images_dir }}/meta-data
become: true
- name: Create the VM
command: virt-install --name={{ vmname }} --ram={{ ram }} --vcpus={{ vcpus }} --import --disk path={{ images_dir }}{{ vmname }}.img,format=qcow2 --disk path={{ images_dir }}{{ vmname }}-cidata.iso,device=cdrom --os-variant {{ os }} --network bridge=br0,model=virtio --graphics vnc,listen=0.0.0.0 --noautoconsole

View file

@ -0,0 +1,2 @@
# cloud-init metadata
# file must exist, but can be empty

View file

@ -0,0 +1,11 @@
# cloud-config
locale: "de_DE.UTF-8"
packages:
- screen
- ripgrep
- ranger
users:
- default:
ssh_authorized_keys:
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEGThAm6K/gH+36Q616He7Hykd3HEMVMifsbSlXuw9j7 carl@work

View file

@ -0,0 +1,8 @@
# autmomatically created via create-vm.yml
---
ansible_host:
mac: '{{ macgen.stdout }}'
cpu: '{{ vcpus }}'
ram: '{{ ram }}'
hdd: '{{image_capacity}}'
server: '{{ ansible_hostname }}'

View file

@ -0,0 +1,2 @@
---
vm_host_bridge_interface: eno1

View file

@ -0,0 +1,63 @@
---
- name: install libvirt and tools
package:
name:
- libvirt-daemon-system
- qemu-system
state: latest
- name: Make sure config directory exists
file:
path: /etc/systemd/network
state: directory
- name: copy network config
template:
src: "{{ item }}"
dest: "/etc/systemd/network/"
with_items:
- "br0.netdev"
- "br0.network"
- "ethernet.network"
- name: check if /etc/network/interfaces exists
stat: path="/etc/network/interfaces"
register: interfaces
- name: disable /etc/network/interfaces
command: mv /etc/network/interfaces /etc/network/interfaces.disabled
when: interfaces.stat.exists
- name: start and enable systemd-networkd
service: name=systemd-networkd state=restarted enabled=yes
- name: start and enable systemd-resolved
service: name=systemd-resolved state=restarted enabled=yes
- name: Disable old network scripts and NetworkManager
service:
name: "{{ item }}"
enabled: no
state: stopped
with_items:
- networking
- network-manager
- NetworkManager
ignore_errors: true
- name: Make a symlink for resolv.conf to use systemd-resolved
file:
dest: /etc/resolv.conf
src: /run/systemd/resolve/stub-resolv.conf
- name: Create libvirt bridge config
copy:
dest: /etc/qemu/bridge.conf
content: "allow br0"
- name: setuid for qemu-bridge-helper
file:
path: /usr/lib/qemu/qemu-bridge-helper
mode: 04755

View file

@ -0,0 +1,3 @@
[NetDev]
Name=br0
Kind=bridge

View file

@ -0,0 +1,9 @@
[Match]
Name=br0
[Link]
MACAddress={{ mac }}
[Network]
DHCP=yes
LinkLocalAddressing=ipv6

View file

@ -0,0 +1,5 @@
[Match]
Name={{ vm_host_bridge_interface }}
[Network]
Bridge=br0