mirror-env/libvirt/main.yml

124 lines
3.7 KiB
YAML
Raw Normal View History

2021-09-14 01:12:52 -04:00
---
2021-09-17 01:02:45 -04:00
- name: setup mirror vm
hosts: 127.0.0.1
2021-09-12 16:32:12 -04:00
tasks:
2021-09-17 01:02:45 -04:00
- name: ubuntu iso exists
stat:
path: "{{ playbook_dir }}/vm/ubuntu20_04.iso"
register: iso_exists
2021-09-15 00:46:18 -04:00
ignore_errors: true
2021-09-16 00:47:36 -04:00
2021-09-17 01:02:45 -04:00
- name: ubuntu iso seed exists
stat:
path: "{{ playbook_dir }}/vm/seed.qcow2"
register: seed_exists
ignore_errors: true
2021-09-16 00:47:36 -04:00
2021-09-17 01:02:45 -04:00
- name: mirror vm exists
command: "virsh dumpxml mirror > /dev/null 2>&1"
register: vm_exists
ignore_errors: true
2021-09-14 01:12:52 -04:00
2021-09-17 01:02:45 -04:00
- name: storage pool exists
command: "virsh pool-dumpxml mirror > /dev/null 2>&1"
register: pool_exists
ignore_errors: true
2021-09-16 00:47:36 -04:00
2021-09-17 01:02:45 -04:00
- name: mirbr0 network exists
command: "virsh net-dumpxml mirbr0 > /dev/null 2>&1"
register: net_exists
ignore_errors: true
2021-09-16 00:47:36 -04:00
- name: create directory for vm
2021-09-15 00:46:18 -04:00
file:
2021-09-16 00:47:36 -04:00
path: "{{ playbook_dir }}/vm"
2021-09-15 00:46:18 -04:00
state: directory
2021-09-16 00:47:36 -04:00
- name: fetch ubuntu iso
2021-09-15 00:46:18 -04:00
get_url:
url: https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-live-server-amd64.iso
2021-09-16 00:47:36 -04:00
dest: "{{ playbook_dir }}/vm/ubuntu20_04.iso"
when: iso_exists.stat.exists == false
2021-09-14 01:12:52 -04:00
2021-09-16 00:47:36 -04:00
# 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
2021-09-15 00:46:18 -04:00
command: >
cloud-localds
2021-09-16 00:47:36 -04:00
{{ 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
2021-09-14 01:12:52 -04:00
2021-09-16 00:47:36 -04:00
- 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
2021-09-14 01:12:52 -04:00
2021-09-16 00:47:36 -04:00
# 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
2021-09-17 01:02:45 -04:00
# could just check for each if they are created
2021-09-16 00:47:36 -04:00
- 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
2021-09-14 01:12:52 -04:00
2021-09-16 00:47:36 -04:00
- name: create vm
2021-09-15 00:46:18 -04:00
command: >
virt-install
--name=mirror
--memory=2048
--vcpus=1
--boot uefi
--os-type linux --os-variant ubuntu20.04
2021-09-16 00:47:36 -04:00
--disk path={{ playbook_dir }}/vm/seed.qcow2,device=cdrom
2021-09-15 00:46:18 -04:00
--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
2021-09-16 00:47:36 -04:00
when: not vm_exists.rc == 0
2021-09-15 00:46:18 -04:00
# 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-16 00:47:36 -04:00
# wait ? seconds (hope that vm is made in foreground)
2021-09-15 00:46:18 -04:00
# 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-17 01:02:45 -04:00
# roles are called relative to playbook
- name: setup mirror services
hosts: 192.168.123.2
include_role: "../roles/{{ item }}"
with_items:
2021-09-15 00:46:18 -04:00
- zfs
# - index
# - nginx
2021-09-16 00:47:36 -04:00
# - rsync
2021-09-15 00:46:18 -04:00
# - ftp