From 06fc399142d9bb848df2f847e7a558e1bf22f56a Mon Sep 17 00:00:00 2001 From: Andrew Wang Date: Wed, 15 Sep 2021 00:46:18 -0400 Subject: [PATCH] libvirt + restructure --- group_vars/all.yml | 9 ++ group_vars/local.yml | 1 + group_vars/vm.yml | 5 + hosts | 6 ++ hosts.yml | 31 ++---- libvirt/files/meta-data | 2 - libvirt/files/user-data | 59 ----------- libvirt/main.yml | 205 +++++++++++++++++++------------------ roles/rsync/tasks/main.yml | 21 ++-- roles/zfs/tasks/main.yml | 7 ++ 10 files changed, 152 insertions(+), 194 deletions(-) create mode 100644 group_vars/all.yml create mode 100644 group_vars/local.yml create mode 100644 group_vars/vm.yml create mode 100644 hosts delete mode 100644 libvirt/files/meta-data delete mode 100644 libvirt/files/user-data diff --git a/group_vars/all.yml b/group_vars/all.yml new file mode 100644 index 0000000..7d197c0 --- /dev/null +++ b/group_vars/all.yml @@ -0,0 +1,9 @@ +userdata: "{{playbook_dir}}/userdata" +# ovmf: /usr/share/edk2-ovmf/x64/OVMF_CODE.fd # not required for libvirt +# default value should be nothing to force user to set +ovmf: +vm_ram: 2G +vm_disk_size: 10G +# why is the ssh port on 7777 ? +# we want the user to be able to access this after the install easily +# vm_ssh_port: 7777 \ No newline at end of file diff --git a/group_vars/local.yml b/group_vars/local.yml new file mode 100644 index 0000000..ada649d --- /dev/null +++ b/group_vars/local.yml @@ -0,0 +1 @@ +ansible_connection: local \ No newline at end of file diff --git a/group_vars/vm.yml b/group_vars/vm.yml new file mode 100644 index 0000000..68b78b8 --- /dev/null +++ b/group_vars/vm.yml @@ -0,0 +1,5 @@ +ansible_connection: ssh +# why is the ssh port on 7777 ? +# ansible_port: 7777 +ansible_user: ubuntu +ansible_password: ubuntu \ No newline at end of file diff --git a/hosts b/hosts new file mode 100644 index 0000000..537aed9 --- /dev/null +++ b/hosts @@ -0,0 +1,6 @@ +[vm] +mirror ansible_connection=ssh ansible_user=root +# ansible_ssh_private_key_file + +[local] +localhost ansible_connection=local \ No newline at end of file diff --git a/hosts.yml b/hosts.yml index 3d8d588..163dc43 100644 --- a/hosts.yml +++ b/hosts.yml @@ -1,26 +1,7 @@ all: - vars: - userdata: "{{playbook_dir}}/userdata" - # ovmf: /usr/share/edk2-ovmf/x64/OVMF_CODE.fd # not required for libvirt - # default value should be nothing to force user to set - ovmf: - vm_ram: 2G - vm_disk_size: 10G - # why is the ssh port on 7777 ? - # we want the user to be able to access this after the install easily - # vm_ssh_port: 7777 - children: - local: - hosts: - localhost: - vars: - ansible_connection: local - vm: - hosts: - ubuntu@localhost: - vars: - ansible_connection: ssh - # why is the ssh port on 7777 ? - # ansible_port: 7777 - ansible_user: ubuntu - ansible_password: ubuntu + local: + hosts: + localhost: + vm: + hosts: + ubuntu@localhost: diff --git a/libvirt/files/meta-data b/libvirt/files/meta-data deleted file mode 100644 index 694312b..0000000 --- a/libvirt/files/meta-data +++ /dev/null @@ -1,2 +0,0 @@ -instance-id: mirror-dev -local-hostname: mirror-dev diff --git a/libvirt/files/user-data b/libvirt/files/user-data deleted file mode 100644 index ec63fff..0000000 --- a/libvirt/files/user-data +++ /dev/null @@ -1,59 +0,0 @@ -#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/libvirt/main.yml b/libvirt/main.yml index de0f4df..f366722 100644 --- a/libvirt/main.yml +++ b/libvirt/main.yml @@ -1,110 +1,117 @@ --- -- name: Configure all VMs +# Setup mirror VM +- hosts: localhost # what happened to the host? tasks: - - 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.qcow2 - register: seedexists - ignore_errors: true - - name: Check if storage pool exists - command: virsh pool-info mirror - register: poolexists - ignore_errors: true - - name: Check if mirbr0 network exists - command: virsh net-info mirbr0 - register: netexists - ignore_errors: true - - name: Check if mirror VM exists - command: virsh dumpxml mirror - register: vmexists - ignore_errors: true + - name: Check if vm was created already + ansible.buildin.stat: + path: {{ userdata }}/{{ vm.name }} + register: vmexists + - 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.qcow2 + register: seedexists + ignore_errors: true + - name: Check if storage pool exists + command: virsh pool-info mirror + register: poolexists + ignore_errors: true + - name: Check if mirbr0 network exists + command: virsh net-info mirbr0 + register: netexists + ignore_errors: true + - name: Check if mirror VM exists + command: virsh dumpxml mirror + register: vmexists + ignore_errors: true -# Setting up VM -- name: Create directory for VM - file: - state: directory - path: {{userdata}}/{{vm.name}} - where: vmexists.stat.exists == false + # Setting up VM + - name: Create directory for VM + file: + state: directory + path: {{userdata}}/{{vm.name}} + where: vmexists.stat.exists == false -- name: Fetching ubuntu iso - get_url: - url: https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-live-server-amd64.iso - dest: {{role_path}}/files/ubuntu20_04.iso - when: isoexists.stat.exists == false + - name: Fetching ubuntu iso + get_url: + url: https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-live-server-amd64.iso + dest: {{role_path}}/files/ubuntu20_04.iso + when: isoexists.stat.exists == false -- name: Create ubuntu iso - # install cloud-localds (cloud-image-utils) - # - # Installing VMs from Ready Images - # https://www.x386.xyz/index.php/2021/01/06/kvm-on-ubuntu-server-1/ - command: > - cloud-localds - # --network-config {{ playbook_dir }}/templates/network - {{userdata}}/{{vm.name}}/seed.qcow2 - {{role_path}}/templates/user-data - when: seedexists.stat.exists == false + - name: Create ubuntu iso + # install cloud-localds (cloud-image-utils) + # + # Installing VMs from Ready Images + # https://www.x386.xyz/index.php/2021/01/06/kvm-on-ubuntu-server-1/ + # --network-config {{ playbook_dir }}/templates/network + command: > + cloud-localds + {{userdata}}/{{vm.name}}/seed.qcow2 + {{role_path}}/templates/user-data + when: seedexists.stat.exists == false -- name: Create storage pool and virtual disks for mirror - command: "virsh {{item}}" - with_items: - - pool-define-as mirror dir --target="{{userdata}}/{{vm.name}}/" - - pool-build mirror - - pool-start mirror - - pool-autostart mirror - command: "virsh vol-create-as mirror {{item}}" - with_items: - - mirror_root1.qcow2 {{vm.disk_size}} - - mirror_root2.qcow2 {{vm.disk_size}} - - mirror_disk1.qcow2 {{vm.disk_size}} - - mirror_disk2.qcow2 {{vm.disk_size}} - - mirror_disk3.qcow2 {{vm.disk_size}} - - mirror_disk4.qcow2 {{vm.disk_size}} - when: not poolexists.rc == 0 + - name: Create storage pool and virtual disks for mirror + command: "virsh {{item}}" + with_items: + - pool-define-as mirror dir --target="{{userdata}}/{{vm.name}}/" + - pool-build mirror + - pool-start mirror + - pool-autostart mirror + command: "virsh vol-create-as mirror {{item}}" + with_items: + - mirror_root1.qcow2 {{vm.disk_size}} + - mirror_root2.qcow2 {{vm.disk_size}} + - mirror_disk1.qcow2 {{vm.disk_size}} + - mirror_disk2.qcow2 {{vm.disk_size}} + - mirror_disk3.qcow2 {{vm.disk_size}} + - mirror_disk4.qcow2 {{vm.disk_size}} + when: not poolexists.rc == 0 -- name: Create bridge network - # net.ipv4.ip_forward = 1 - command: "virsh {{item}}" - with_items: - - net-define {{role_path}}/templates/network.xml - - net-autostart mirbr0 - - net-start mirbr0 - when: not netexists.rc == 0 + - name: Create bridge network + # net.ipv4.ip_forward = 1 + command: "virsh {{item}}" + with_items: + - net-define {{role_path}}/templates/network.xml + - net-autostart mirbr0 + - net-start mirbr0 + when: not netexists.rc == 0 -- name: Create mirror VM - command: > - virt-install - --name=mirror - --memory=2048 - --vcpus=1 - --boot uefi - --os-type linux --os-variant ubuntu20.04 - --disk path={{userdata}}/{{vm.name}}/seed.qcow2,device=cdrom - --disk vol=mirror/mirror_root1.qcow2 - --disk vol=mirror/mirror_root2.qcow2 - --disk vol=mirror/mirror_disk1.qcow2 - --disk vol=mirror/mirror_disk2.qcow2 - --disk vol=mirror/mirror_disk3.qcow2 - --disk vol=mirror/mirror_disk4.qcow2 - --network bridge=mirbr0 - --graphics vnc,port=5911,listen=127.0.0.1 - --noautoconsole - when: not vmexists.rc == 0 - # in the vm to disable cloud-init run - # sudo touch /etc/cloud/cloud-init.disabled + - name: Create mirror VM + command: > + virt-install + --name=mirror + --memory=2048 + --vcpus=1 + --boot uefi + --os-type linux --os-variant ubuntu20.04 + --disk path={{userdata}}/{{vm.name}}/seed.qcow2,device=cdrom + --disk vol=mirror/mirror_root1.qcow2 + --disk vol=mirror/mirror_root2.qcow2 + --disk vol=mirror/mirror_disk1.qcow2 + --disk vol=mirror/mirror_disk2.qcow2 + --disk vol=mirror/mirror_disk3.qcow2 + --disk vol=mirror/mirror_disk4.qcow2 + --network bridge=mirbr0 + --graphics vnc,port=5911,listen=127.0.0.1 + --noautoconsole + when: not vmexists.rc == 0 + # in the vm to disable cloud-init run + # sudo touch /etc/cloud/cloud-init.disabled - # next - # wait ? seconds (hope that vm is made in forgroud) - # update ssh config to allow root login (and add passwd to root) - # copy pub key and relogin as root + # wait ? seconds (hope that vm is made in forgroud) + # copy over pub key into /root/.ssh/authorized_keys + # add line to ssh config that allows ssh as root +# Configure mirror VM +- hosts: mirror + roles: + - zfs +# - index +# - nginx + - rsync +# - ftp diff --git a/roles/rsync/tasks/main.yml b/roles/rsync/tasks/main.yml index 32d2765..6660ae5 100755 --- a/roles/rsync/tasks/main.yml +++ b/roles/rsync/tasks/main.yml @@ -1,20 +1,23 @@ +- name: rsync + apt: + name: rsync + state: latest + update_cache: true + - name: enable and start rsync daemon systemd: name: rsync enabled: true state: started -- name: add rsync config +- name: copy rsync configs template: - src: ../templates/rsyncd.conf.j2 - dest: /etc/rsyncd.conf - owner: root - group: root - mode: 0644 - template: - src: ../templates/rsyncd-filter.conf.j2 - dest: /etc/rsyncd-filter.conf + src: templates/{{ item.src }} + dest: /etc/{{ item.dest }} owner: root group: root mode: 0644 + with_items: + - { src: "rsyncd.conf.j2", dest: "rsyncd.conf" } + - { src: "rsyncd-filter.conf.j2", dest: "rsyncd-filter.conf" } notify: restart rsync diff --git a/roles/zfs/tasks/main.yml b/roles/zfs/tasks/main.yml index 035283e..09aa3ef 100755 --- a/roles/zfs/tasks/main.yml +++ b/roles/zfs/tasks/main.yml @@ -1,3 +1,9 @@ +- name: install zfs + apt: + name: zfsutils-linux + state: latest + update_cache: true + - name: create zfs mountpoint file: path: /mirror/root/.cscmirror @@ -13,3 +19,4 @@ commmand: zpool create -f -m /mirror/root/.cscmirror cscmirror raidz2 /dev/vdc /dev/vdd /dev/vde /dev/vdf # may need to mount it (also check that it will automount on boot) # zfs mount -a + # need check for if zpool already exists