mirror-env/libvirt/main.yml

118 lines
3.7 KiB
YAML
Raw Normal View History

2021-09-14 01:12:52 -04:00
---
2021-09-15 00:46:18 -04:00
# Setup mirror VM
- hosts: localhost
2021-09-14 01:12:52 -04:00
# what happened to the host?
2021-09-12 16:32:12 -04:00
tasks:
2021-09-15 00:46:18 -04:00
- 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
2021-09-14 01:12:52 -04:00
2021-09-15 00:46:18 -04:00
# Setting up VM
- name: Create directory for VM
file:
state: directory
path: {{userdata}}/{{vm.name}}
where: vmexists.stat.exists == false
2021-09-14 01:12:52 -04:00
2021-09-15 00:46:18 -04:00
- 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
2021-09-14 01:12:52 -04:00
2021-09-15 00:46:18 -04:00
- 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
2021-09-14 01:12:52 -04:00
2021-09-15 00:46:18 -04:00
- 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
2021-09-14 01:12:52 -04:00
2021-09-15 00:46:18 -04:00
- 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
2021-09-14 01:12:52 -04:00
2021-09-15 00:46:18 -04:00
- 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
2021-09-12 16:32:12 -04:00
2021-09-15 00:46:18 -04:00
# 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
2021-09-14 01:12:52 -04:00
2021-09-15 00:46:18 -04:00
# Configure mirror VM
- hosts: mirror
roles:
- zfs
# - index
# - nginx
- rsync
# - ftp