add the current state of the vmsetup
This commit is contained in:
commit
d3675f34bf
19 changed files with 985 additions and 0 deletions
8
roles/create-vm/defaults/main.yml
Normal file
8
roles/create-vm/defaults/main.yml
Normal 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
|
17
roles/create-vm/files/macgen.py
Normal file
17
roles/create-vm/files/macgen.py
Normal 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())
|
29
roles/create-vm/tasks/create_hostvars.yml
Normal file
29
roles/create-vm/tasks/create_hostvars.yml
Normal 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 }}"
|
||||
|
73
roles/create-vm/tasks/main.yml
Normal file
73
roles/create-vm/tasks/main.yml
Normal 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
|
2
roles/create-vm/templates/meta-data.j2
Normal file
2
roles/create-vm/templates/meta-data.j2
Normal file
|
@ -0,0 +1,2 @@
|
|||
# cloud-init metadata
|
||||
# file must exist, but can be empty
|
11
roles/create-vm/templates/user-data.j2
Normal file
11
roles/create-vm/templates/user-data.j2
Normal 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
|
||||
|
8
roles/create-vm/templates/vm_hostvars.j2
Normal file
8
roles/create-vm/templates/vm_hostvars.j2
Normal 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 }}'
|
Loading…
Add table
Add a link
Reference in a new issue