mirror-env/libvirt/main.yml

124 lines
3.7 KiB
YAML

---
- name: setup mirror vm
hosts: 127.0.0.1
tasks:
- name: ubuntu iso exists
stat:
path: "{{ playbook_dir }}/vm/ubuntu20_04.iso"
register: iso_exists
ignore_errors: true
- name: ubuntu iso seed exists
stat:
path: "{{ playbook_dir }}/vm/seed.qcow2"
register: seed_exists
ignore_errors: true
- name: mirror vm exists
command: "virsh dumpxml mirror > /dev/null 2>&1"
register: vm_exists
ignore_errors: true
- name: storage pool exists
command: "virsh pool-dumpxml mirror > /dev/null 2>&1"
register: pool_exists
ignore_errors: true
- name: mirbr0 network exists
command: "virsh net-dumpxml mirbr0 > /dev/null 2>&1"
register: net_exists
ignore_errors: true
- name: create directory for vm
file:
path: "{{ playbook_dir }}/vm"
state: directory
- name: fetch ubuntu iso
get_url:
url: https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-live-server-amd64.iso
dest: "{{ playbook_dir }}/vm/ubuntu20_04.iso"
when: iso_exists.stat.exists == false
# requires: cloud-localds (cloud-image-utils)
# Installing VMs from Ready Images
# https://www.x386.xyz/index.php/2021/01/06/kvm-on-ubuntu-server-1/
# apply network config
# --network-config {{ playbook_dir }}/templates/network
- name: create ubuntu iso seed
command: >
cloud-localds
{{ playbook_dir }}/vm/seed.qcow2
{{ playbook_dir }}/templates/user-data
when: seed_exists.stat.exists == false
# get user to set net.ipv4.ip_forward = 1 ?
- name: create mirbr0 bridge network
command: "virsh {{ item }}"
with_items:
- net-define {{ playbook_dir }}/templates/network.xml
- net-autostart mirbr0
- net-start mirbr0
when: not net_exists.rc == 0
- name: create storage pool
command: "virsh {{ item }}"
with_items:
- pool-define-as mirror dir --target="{{ playbook_dir }}/vm/"
- pool-build mirror
- pool-start mirror
- pool-autostart mirror
when: not pool_exists.rc == 0
# any way to check skip virtual disks that are already created?
# for now just hardcoding size as 10G since will need to mod user-data to use different values
# could just check for each if they are created
- name: create virtual disks
command: "virsh vol-create-as mirror {{ item }}"
with_items:
- mirror_root1.qcow2 10G
- mirror_root2.qcow2 10G
- mirror_disk1.qcow2 10G
- mirror_disk2.qcow2 10G
- mirror_disk3.qcow2 10G
- mirror_disk4.qcow2 10G
- name: create vm
command: >
virt-install
--name=mirror
--memory=2048
--vcpus=1
--boot uefi
--os-type linux --os-variant ubuntu20.04
--disk path={{ playbook_dir }}/vm/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 vm_exists.rc == 0
# in the vm to disable cloud-init run
# sudo touch /etc/cloud/cloud-init.disabled
# wait ? seconds (hope that vm is made in foreground)
# copy over pub key into /root/.ssh/authorized_keys
# add line to ssh config that allows ssh as root
# roles are called relative to playbook
- name: setup mirror services
hosts: 192.168.123.2
include_role: "../roles/{{ item }}"
with_items:
- zfs
# - index
# - nginx
# - rsync
# - ftp