diff --git a/hosts.yml b/hosts.yml index 9b2ef50..2a0f45c 100644 --- a/hosts.yml +++ b/hosts.yml @@ -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 diff --git a/mirror-vm.yml b/mirror-vm.yml index 1225d89..af85f68 100644 --- a/mirror-vm.yml +++ b/mirror-vm.yml @@ -1,5 +1,9 @@ --- - hosts: local - become: true roles: - vm-qemu + - run-vm + +- hosts: vm + roles: + - post-install diff --git a/roles/setup/tasks/main.yml b/roles/post-install/tasks/main.yml similarity index 97% rename from roles/setup/tasks/main.yml rename to roles/post-install/tasks/main.yml index b98fe34..e978f39 100755 --- a/roles/setup/tasks/main.yml +++ b/roles/post-install/tasks/main.yml @@ -1,3 +1,4 @@ +--- - name: install dependencies apt: pkg: diff --git a/roles/run-vm/tasks/main.yml b/roles/run-vm/tasks/main.yml new file mode 100644 index 0000000..c622685 --- /dev/null +++ b/roles/run-vm/tasks/main.yml @@ -0,0 +1,6 @@ +--- +- name: Run all VMs + include_tasks: vm.yml + loop: "{{virtual_machines}}" + loop_control: + loop_var: vm diff --git a/roles/run-vm/tasks/vm.yml b/roles/run-vm/tasks/vm.yml new file mode 100644 index 0000000..b99b2a7 --- /dev/null +++ b/roles/run-vm/tasks/vm.yml @@ -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" diff --git a/roles/vm-qemu/tasks/vm.yml b/roles/vm-qemu/tasks/vm.yml index f5c5569..c7bb2e1 100644 --- a/roles/vm-qemu/tasks/vm.yml +++ b/roles/vm-qemu/tasks/vm.yml @@ -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