run playbook

This commit is contained in:
Daniel Liu 2021-09-12 18:24:22 -04:00
parent 07153e8ecc
commit 8e0aaab8dd
6 changed files with 71 additions and 21 deletions

View File

@ -1,18 +1,23 @@
local:
hosts:
localhost:
all:
vars:
ansible_connection: local
userdata: "{{playbook_dir}}/userdata"
ovmf: /usr/share/edk2-ovmf/x64/OVMF_CODE.fd # not required for libvirt
virtual_machines:
- name: mirror-env
ram: 2G
ram: 3G
disk_size: 10G
vm:
hosts:
ubuntu@localhost:
vars:
ansible_connection: ssh
ansible_port: 7777
ansible_user: ubuntu
ansible_password: ubuntu
ssh_port: 7777
children:
local:
hosts:
localhost:
vars:
ansible_connection: local
vm:
hosts:
ubuntu@localhost:
vars:
ansible_connection: ssh
ansible_port: 7777
ansible_user: ubuntu
ansible_password: ubuntu

View File

@ -1,5 +1,9 @@
---
- hosts: local
become: true
roles:
- vm-qemu
- run-vm
- hosts: vm
roles:
- post-install

View File

@ -1,3 +1,4 @@
---
- name: install dependencies
apt:
pkg:

View File

@ -0,0 +1,6 @@
---
- name: Run all VMs
include_tasks: vm.yml
loop: "{{virtual_machines}}"
loop_control:
loop_var: vm

18
roles/run-vm/tasks/vm.yml Normal file
View File

@ -0,0 +1,18 @@
---
- name: "{{vm.name}} - Start VM"
async: 10000
poll: 0
shell:
cmd: "qemu-system-x86_64 \
-enable-kvm -boot order=d \
-drive file={{userdata}}/{{vm.name}}/drive1,if=virtio,id=a,media=disk,format=qcow2 \
-drive file={{userdata}}/{{vm.name}}/drive2,if=virtio,id=b,media=disk,format=qcow2 \
-drive file={{userdata}}/{{vm.name}}/drive3,if=virtio,id=c,media=disk,format=qcow2 \
-drive file={{userdata}}/{{vm.name}}/drive4,if=virtio,id=d,media=disk,format=qcow2 \
-drive file={{userdata}}/{{vm.name}}/drive5,if=virtio,id=e,media=disk,format=qcow2 \
-drive file={{userdata}}/{{vm.name}}/drive6,if=virtio,id=f,media=disk,format=qcow2 \
-drive file={{userdata}}/{{vm.name}}/seed.iso,if=virtio,format=raw \
-bios {{ovmf}} \
-m {{vm.ram}} \
-net user,hostfwd=tcp::{{vm.ssh_port}}-:22 \
-net nic"

View File

@ -1,40 +1,56 @@
---
# quite a lot of duplication, probably fix later
# Gather info
- name: Check if vm was created already
- name: "{{vm.name}} - Check if vm was created already"
stat:
path: "{{userdata}}/{{vm.name}}"
register: vmexists
ignore_errors: true
- name: Check for ubuntu iso
- name: "{{vm.name}} - Check for ubuntu iso"
stat:
path: "{{role_path}}/files/ubuntu20_04.iso"
register: isoexists
ignore_errors: true
- name: Check for ubuntu iso seed
- name: "{{vm.name}} - Check for ubuntu iso seed"
stat:
path: "{{userdata}}/{{vm.name}}/seed.iso"
register: seedexists
ignore_errors: true
# Setting up VM
- name: Create directory for VM
- name: "{{vm.name}} - Create directory for VM"
file:
state: directory
path: "{{userdata}}/{{vm.name}}"
when: vmexists.stat.exists == false
# didn't use get_url module since it broke with symlinks
- name: Fetching ubuntu iso
- name: "{{vm.name}} - Fetching ubuntu iso"
command: curl -o "{{role_path}}/files/ubuntu20_04.iso" https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-live-server-amd64.iso
when: isoexists.stat.exists == false
- name: Create disk images
- name: "{{vm.name}} - Create disk images"
shell:
cmd: "for i in {1..6}; do qemu-img create -f qcow2 {{userdata}}/{{vm.name}}/drive${i} {{vm.disk_size}}; done"
when: vmexists.stat.exists == false
- name: Creating cloud-init iso
- name: "{{vm.name}} - Creating cloud-init iso"
command: "genisoimage -output {{userdata}}/{{vm.name}}/seed.iso -volid cidata -joliet -rock {{role_path}}/files/user-data {{role_path}}/files/meta-data"
when: seedexists.stat.exists == false
- name: "{{vm.name}} - Starting autoinstallation"
shell:
cmd: "qemu-system-x86_64 -cdrom {{role_path}}/files/ubuntu20_04.iso \
-enable-kvm -boot order=d \
-drive file={{userdata}}/{{vm.name}}/drive1,if=virtio,id=a,media=disk,format=qcow2 \
-drive file={{userdata}}/{{vm.name}}/drive2,if=virtio,id=b,media=disk,format=qcow2 \
-drive file={{userdata}}/{{vm.name}}/drive3,if=virtio,id=c,media=disk,format=qcow2 \
-drive file={{userdata}}/{{vm.name}}/drive4,if=virtio,id=d,media=disk,format=qcow2 \
-drive file={{userdata}}/{{vm.name}}/drive5,if=virtio,id=e,media=disk,format=qcow2 \
-drive file={{userdata}}/{{vm.name}}/drive6,if=virtio,id=f,media=disk,format=qcow2 \
-drive file={{userdata}}/{{vm.name}}/seed.iso,if=virtio,format=raw \
-bios {{ovmf}} \
-m {{vm.ram}}"
# todo: find a way to not ask for confirmation to start autoinstall
# bug: autoinstall will not shutdown properly so the ansible task will never finish