mirror-env/libvirt/main.yml

113 lines
3.8 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-26 16:22:29 -04:00
gather_facts: false
2021-09-12 16:32:12 -04:00
tasks:
2021-09-17 16:42:15 -04:00
- name: mirbr0 network exists
2021-09-26 16:22:29 -04:00
command: "virsh net-dumpxml mirbr0"
2021-09-22 00:50:14 -04:00
changed_when: false
2021-09-17 01:02:45 -04:00
ignore_errors: true
2021-09-22 00:50:14 -04:00
register: net_exists
2021-09-14 01:12:52 -04:00
2021-09-17 01:02:45 -04:00
- name: storage pool exists
2021-09-26 16:22:29 -04:00
command: "virsh pool-dumpxml mirror"
2021-09-22 00:50:14 -04:00
changed_when: false
2021-09-17 01:02:45 -04:00
ignore_errors: true
2021-09-22 00:50:14 -04:00
register: pool_exists
2021-09-16 00:47:36 -04:00
2021-09-17 16:42:15 -04:00
- name: mirror vm exists
2021-09-26 16:22:29 -04:00
command: "virsh dumpxml mirror"
2021-09-22 00:50:14 -04:00
changed_when: false
2021-09-17 01:02:45 -04:00
ignore_errors: true
2021-09-22 00:50:14 -04:00
register: vm_exists
2021-09-16 00:47:36 -04:00
- name: create directory for vm
2021-09-15 00:46:18 -04:00
file:
2021-09-17 16:42:15 -04:00
path: "{{ playbook_dir }}/vm/disks"
2021-09-15 00:46:18 -04:00
state: directory
2021-09-17 16:42:15 -04:00
recurse: yes
2021-09-16 00:47:36 -04:00
- name: fetch ubuntu iso
2021-09-15 00:46:18 -04:00
get_url:
2021-09-27 22:49:49 -04:00
url: "https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-live-server-amd64.iso"
dest: "{{ playbook_dir }}/vm/ubuntu20_04.iso"
2021-09-16 00:47:36 -04:00
2021-09-27 22:49:49 -04:00
# autoinstall not working for libvirt
# https://manintheit.org/en/posts/automation/ubuntu-autoinstall/
# https://ubuntu.com/server/docs/install/autoinstall-quickstart
#
2021-09-27 22:24:05 -04:00
# --network-config {{ playbook_dir }}/templates/network
# - name: create ubuntu iso seed
# command:
# cmd: >
# cloud-localds
# {{ playbook_dir }}/vm/seed.iso
# {{ playbook_dir }}/templates/user-data
# {{ playbook_dir }}/templates/meta-data
# creates: "{{ playbook_dir }}/vm/seed.iso"
# - name: create ubuntu iso seed
# command:
# cmd: >
# genisoimage
# -output {{ playbook_dir }}/vm/cidata.iso
# -V cidata -r -J
# {{ playbook_dir }}/templates/user-data
# {{ playbook_dir }}/templates/meta-data
# creates: "{{ playbook_dir }}/vm/seed.iso"
2021-09-16 00:47:36 -04:00
- name: create mirbr0 bridge network
command: "virsh {{ item }}"
2021-09-17 16:42:15 -04:00
loop:
2021-09-16 00:47:36 -04:00
- net-define {{ playbook_dir }}/templates/network.xml
- net-autostart mirbr0
- net-start mirbr0
2021-09-26 16:22:29 -04:00
when: 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 }}"
2021-09-17 16:42:15 -04:00
loop:
- pool-define-as mirror dir --target="{{ playbook_dir }}/vm/disks"
2021-09-16 00:47:36 -04:00
- pool-build mirror
- pool-autostart mirror
2021-09-22 00:50:14 -04:00
- pool-start mirror
2021-09-26 16:22:29 -04:00
when: pool_exists.rc != 0
2021-09-14 01:12:52 -04:00
2021-09-27 22:49:49 -04:00
# TODO: allow user to create any number of disks with any size
2021-09-16 00:47:36 -04:00
- name: create virtual disks
2021-09-17 16:42:15 -04:00
command:
cmd: "virsh vol-create-as mirror {{ item.name }} {{ item.size }}"
creates: "{{ playbook_dir }}/vm/disks/{{ item.name }}"
loop:
- { name: mirror_root1.qcow2, size: 10G }
- { name: mirror_root2.qcow2, size: 10G }
- { name: mirror_disk1.qcow2, size: 10G }
- { name: mirror_disk2.qcow2, size: 10G }
- { name: mirror_disk3.qcow2, size: 10G }
- { name: mirror_disk4.qcow2, size: 10G }
2021-09-14 01:12:52 -04:00
2021-09-26 16:22:29 -04:00
# does not exist yet
# --os-variant ubuntu20.04
2021-09-27 22:24:05 -04:00
# --cdrom path={{ playbook_dir }}/vm/ubuntu20_04.iso
# --disk path={{ playbook_dir }}/vm/seed.iso,format=raw,bus=virtio
2021-09-27 22:49:49 -04:00
# --cloud-init user-data={{ playbook_dir }}/templates/user-data,meta-data={{ playbook_dir }}/templates/meta-data
# --disk path={{ playbook_dir }}/vm/focal-server-cloudimg-amd64.img
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
2021-09-26 16:22:29 -04:00
--os-type linux
2021-09-27 22:49:49 -04:00
--cdrom {{ playbook_dir }}/vm/ubuntu20_04.iso
2021-09-27 22:24:05 -04:00
--disk vol=mirror/mirror_root1.qcow2,bus=virtio
--disk vol=mirror/mirror_root2.qcow2,bus=virtio
--disk vol=mirror/mirror_disk1.qcow2,bus=virtio
--disk vol=mirror/mirror_disk2.qcow2,bus=virtio
--disk vol=mirror/mirror_disk3.qcow2,bus=virtio
--disk vol=mirror/mirror_disk4.qcow2,bus=virtio
--network bridge=mirbr0,model=virtio
2021-09-15 00:46:18 -04:00
--graphics vnc,port=5911,listen=127.0.0.1
--noautoconsole
2021-09-27 22:49:49 -04:00
when: vm_exists.rc != 0