From d5b90baecf5790a13e07d84647119d36c8e7d628 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Sun, 12 Sep 2021 16:59:27 -0400 Subject: [PATCH] added back qemu vm --- hosts.yml | 13 ++-- mirror-vm.yml | 2 +- roles/{vm => vm-libvirt}/files/meta-data | 0 roles/{vm => vm-libvirt}/files/user-data | 0 .../{vm => vm-libvirt}/tasks/main-libvirt.yml | 0 roles/{vm => vm-libvirt}/tasks/main.yml | 0 roles/{vm => vm-libvirt}/tasks/vm.yml | 0 roles/{vm => vm-libvirt}/templates/network | 0 .../{vm => vm-libvirt}/templates/network.xml | 0 roles/{vm => vm-libvirt}/templates/user-data | 0 roles/vm-qemu/files/meta-data | 2 + roles/vm-qemu/files/user-data | 59 +++++++++++++++++++ roles/vm-qemu/tasks/main.yml | 6 ++ roles/vm-qemu/tasks/vm.yml | 40 +++++++++++++ 14 files changed, 114 insertions(+), 8 deletions(-) rename roles/{vm => vm-libvirt}/files/meta-data (100%) rename roles/{vm => vm-libvirt}/files/user-data (100%) rename roles/{vm => vm-libvirt}/tasks/main-libvirt.yml (100%) rename roles/{vm => vm-libvirt}/tasks/main.yml (100%) rename roles/{vm => vm-libvirt}/tasks/vm.yml (100%) rename roles/{vm => vm-libvirt}/templates/network (100%) rename roles/{vm => vm-libvirt}/templates/network.xml (100%) rename roles/{vm => vm-libvirt}/templates/user-data (100%) create mode 100644 roles/vm-qemu/files/meta-data create mode 100644 roles/vm-qemu/files/user-data create mode 100644 roles/vm-qemu/tasks/main.yml create mode 100644 roles/vm-qemu/tasks/vm.yml diff --git a/hosts.yml b/hosts.yml index 12d4e47..9b2ef50 100644 --- a/hosts.yml +++ b/hosts.yml @@ -1,17 +1,16 @@ local: hosts: - localhost + localhost: vars: ansible_connection: local - userdata: {{playbook_dir}}/userdata + userdata: "{{playbook_dir}}/userdata" virtual_machines: - - name: mirror-env - ram: 2G - disk_size: 10G - + - name: mirror-env + ram: 2G + disk_size: 10G vm: hosts: - ubuntu@localhost + ubuntu@localhost: vars: ansible_connection: ssh ansible_port: 7777 diff --git a/mirror-vm.yml b/mirror-vm.yml index 5a8fce1..1225d89 100644 --- a/mirror-vm.yml +++ b/mirror-vm.yml @@ -2,4 +2,4 @@ - hosts: local become: true roles: - - vm + - vm-qemu diff --git a/roles/vm/files/meta-data b/roles/vm-libvirt/files/meta-data similarity index 100% rename from roles/vm/files/meta-data rename to roles/vm-libvirt/files/meta-data diff --git a/roles/vm/files/user-data b/roles/vm-libvirt/files/user-data similarity index 100% rename from roles/vm/files/user-data rename to roles/vm-libvirt/files/user-data diff --git a/roles/vm/tasks/main-libvirt.yml b/roles/vm-libvirt/tasks/main-libvirt.yml similarity index 100% rename from roles/vm/tasks/main-libvirt.yml rename to roles/vm-libvirt/tasks/main-libvirt.yml diff --git a/roles/vm/tasks/main.yml b/roles/vm-libvirt/tasks/main.yml similarity index 100% rename from roles/vm/tasks/main.yml rename to roles/vm-libvirt/tasks/main.yml diff --git a/roles/vm/tasks/vm.yml b/roles/vm-libvirt/tasks/vm.yml similarity index 100% rename from roles/vm/tasks/vm.yml rename to roles/vm-libvirt/tasks/vm.yml diff --git a/roles/vm/templates/network b/roles/vm-libvirt/templates/network similarity index 100% rename from roles/vm/templates/network rename to roles/vm-libvirt/templates/network diff --git a/roles/vm/templates/network.xml b/roles/vm-libvirt/templates/network.xml similarity index 100% rename from roles/vm/templates/network.xml rename to roles/vm-libvirt/templates/network.xml diff --git a/roles/vm/templates/user-data b/roles/vm-libvirt/templates/user-data similarity index 100% rename from roles/vm/templates/user-data rename to roles/vm-libvirt/templates/user-data diff --git a/roles/vm-qemu/files/meta-data b/roles/vm-qemu/files/meta-data new file mode 100644 index 0000000..694312b --- /dev/null +++ b/roles/vm-qemu/files/meta-data @@ -0,0 +1,2 @@ +instance-id: mirror-dev +local-hostname: mirror-dev diff --git a/roles/vm-qemu/files/user-data b/roles/vm-qemu/files/user-data new file mode 100644 index 0000000..ec63fff --- /dev/null +++ b/roles/vm-qemu/files/user-data @@ -0,0 +1,59 @@ +#cloud-config +autoinstall: + version: 1 + identity: + hostname: mirror + username: ubuntu + # password is just ubuntu + password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0" + locale: en_US + ssh: + allow-pw: true + authorized-keys: [] + install-server: true + storage: + config: + # disks =-=-=-=-=-=-= + - {id: vda, name: '', path: /dev/vda, type: disk, ptable: gpt, + preserve: false, grub_device: false} + - {id: vdb, name: '', path: /dev/vdb, type: disk, ptable: gpt, + preserve: false, grub_device: false} + # partitions =-=-=-=-=-= + # vda1 + - {id: vda1, type: partition, size: 500M, device: vda, number: 1, + preserve: false, wipe: superblock, flag: boot, grub_device: true} + - {id: vda1-format, type: format, volume: vda1, + fstype: fat32, preserve: false} + # vda2 + - {id: vda2, type: partition, size: 9G, device: vda, number: 2, + preserve: false, wipe: superblock, flag: '', grub_device: false} + # vdb1 + - {id: vdb1, type: partition, size: 500M, device: vdb, number: 1, + preserve: false, wipe: superblock, flag: boot, grub_device: true} + - {id: vdb1-format, type: format, volume: vdb1, + fstype: fat32, preserve: false} + # vdb2 + - {id: vdb2, type: partition, size: 9G, device: vdb, number: 2, + preserve: false, wipe: superblock, flag: '', grub_device: false} + # raid =-=-=-=-=-= + - id: md0 + name: md0 + type: raid + raidlevel: raid1 + devices: [vda2, vdb2] + spare_devices: [] + preserve: false + # lvm =-=-=-=-=-= + - id: vg0 + name: vg0 + type: lvm_volgroup + devices: [md0] + preserve: false + # partition + - {id: vg0-root, name: root, type: lvm_partition, size: 8G, + preserve: false, volgroup: vg0} + - {id: vg0-root-format, type: format, volume: vg0-root, + fstype: ext4, preserve: false} + # mount points =-=-=-=-=-= + - {id: boot-mount, type: mount, path: /boot/efi, device: vda1-format} + - {id: root-mount, type: mount, path: /, device: vg0-root-format} diff --git a/roles/vm-qemu/tasks/main.yml b/roles/vm-qemu/tasks/main.yml new file mode 100644 index 0000000..d3e6c28 --- /dev/null +++ b/roles/vm-qemu/tasks/main.yml @@ -0,0 +1,6 @@ +--- +- name: Configure all VMs + include_tasks: vm.yml + loop: "{{virtual_machines}}" + loop_control: + loop_var: vm diff --git a/roles/vm-qemu/tasks/vm.yml b/roles/vm-qemu/tasks/vm.yml new file mode 100644 index 0000000..f5c5569 --- /dev/null +++ b/roles/vm-qemu/tasks/vm.yml @@ -0,0 +1,40 @@ +--- +# quite a lot of duplication, probably fix later +# Gather info +- name: Check if vm was created already + stat: + path: "{{userdata}}/{{vm.name}}" + register: vmexists + ignore_errors: true +- name: Check for ubuntu iso + stat: + path: "{{role_path}}/files/ubuntu20_04.iso" + register: isoexists + ignore_errors: true +- 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 + 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 + 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 + 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 + 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 +