attempt to fix libvirt playbook

autoautoinstall
Andrew Wang 1 year ago
parent 06fc399142
commit 50146970e2
  1. 9
      group_vars/all.yml
  2. 11
      group_vars/local.yml
  3. 5
      group_vars/vm.yml
  4. 10
      hosts
  5. 2
      libvirt/clean.yml
  6. 149
      libvirt/main.yml
  7. 106
      libvirt/main.yml.orig
  8. 12
      libvirt/templates/network

@ -1,9 +0,0 @@
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

@ -1 +1,10 @@
ansible_connection: local
# not required for libvirt
# ovmf: /usr/share/edk1-ovmf/x64/OVMF_CODE.fd
ovmf:
vm_ram: 1G
userdata: "{{ playbook_dir }}/userdata"
vm_disk_size: 9G
vm_ssh_port: 22
# need to redo variables
# not being called correctly in playbooks

@ -1,5 +0,0 @@
ansible_connection: ssh
# why is the ssh port on 7777 ?
# ansible_port: 7777
ansible_user: ubuntu
ansible_password: ubuntu

10
hosts

@ -1,5 +1,13 @@
[vm]
mirror ansible_connection=ssh ansible_user=root
192.168.123.2
host=mirror
ansible_connection=ssh
ansible_port=22
ansible_user=ubuntu
ansible_password=ubuntu
ansible_become_user=root
ansible_become_password=ubuntu
# ansible_ssh_private_key_file
[local]

@ -0,0 +1,2 @@
---
- hosts: localhost

@ -1,87 +1,87 @@
---
# Setup mirror VM
- hosts: localhost
# what happened to the host?
tasks:
- 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
- name: gather facts
ignore_errors: true
tasks:
- name: ubuntu iso exists
stat:
path: "{{ playbook_dir }}/vm/ubuntu20_04.iso"
register: iso_exists
- name: ubuntu iso seed exists
stat:
path: "{{ playbook_dir }}/vm/seed.qcow2"
register: seed_exists
- name: mirror vm exists
command: "virsh dumpxml mirror > /dev/null 2>&1"
register: vm_exists
# Setting up VM
- name: Create directory for VM
- name: storage pool exists
command: "virsh pool-dumpxml mirror > /dev/null 2>&1"
register: pool_exists
- name: mirbr0 network exists
command: "virsh net-dumpxml mirbr0 > /dev/null 2>&1"
register: net_exists
- name: create directory for vm
file:
path: "{{ playbook_dir }}/vm"
state: directory
path: {{userdata}}/{{vm.name}}
where: vmexists.stat.exists == false
- name: Fetching ubuntu iso
- name: fetch 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
dest: "{{ playbook_dir }}/vm/ubuntu20_04.iso"
when: iso_exists.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
# 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
{{userdata}}/{{vm.name}}/seed.qcow2
{{role_path}}/templates/user-data
when: seedexists.stat.exists == false
{{ 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 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
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
- 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
# 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
- 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 mirror VM
- name: create vm
command: >
virt-install
--name=mirror
@ -89,7 +89,7 @@
--vcpus=1
--boot uefi
--os-type linux --os-variant ubuntu20.04
--disk path={{userdata}}/{{vm.name}}/seed.qcow2,device=cdrom
--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
@ -99,19 +99,18 @@
--network bridge=mirbr0
--graphics vnc,port=5911,listen=127.0.0.1
--noautoconsole
when: not vmexists.rc == 0
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 forgroud)
# 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
# Configure mirror VM
- hosts: mirror
- hosts: 192.168.123.2
roles:
- zfs
# - index
# - nginx
- rsync
# - rsync
# - ftp

@ -1,106 +0,0 @@
```
- name: Create mirror VM
hosts: localhost
connection: local
tasks:
- name: Check for ubuntu iso
stat:
path: {{ playbook_dir }}/ubuntu.iso
register: isoexists
ignore_errors: true
- name: Check for ubuntu iso seed
stat:
path: {{ playbook_dir }}/ubuntu-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: enable and start libvirt daemon
systemd:
name: libvirtd
enabled: true
state: started
- name: Download ubuntu iso
# make sure curl is installed ?
command: >
curl
-o {{ playbook_dir }}/ubuntu.iso
https://releases.ubuntu.com/20.04/ubuntu-20.04.2-live-server-amd64.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
{{ playbook_dir }}/ubuntu-seed.qcow2
{{ playbook_dir }}/templates/user-data
when: seedexists.stat.exists == False
- name: Create storage pool and virtual disks for mirror
file:
path: {{ playbook_dir }}/disks
state: directory
owner: root
group: root
mode: 0711
command: "virsh {{ item }}"
with_items:
- pool-define-as mirror dir --target="{{ playbook_dir }}/disks"
- pool-build mirror
- pool-start mirror
- pool-autostart mirror
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
when: not poolexists.rc == 0
- name: Create bridge network
# net.ipv4.ip_forward = 1
command: "virsh {{ item }}"
with_items:
- net-define {{ playbook_dir }}/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={{ playbook_dir }}/ubuntu-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

@ -1,12 +1,12 @@
# set up static ip
# https://askubuntu.com/questions/1029531/how-to-setup-a-static-ip-on-ubuntu-server-18-04
#cloud-config
version: 2
ethernets:
enp1s0:
dhcp4: false
addresses: [ 192.168.0.221/24 ]
gateway4: 192.168.0.1
addresses: [ 192.168.123.2/24 ]
gateway4: 192.168.123.1
nameservers:
addresses: [ 192.168.0.1,8.8.8.8 ]
addresses: [ 1.1.1.1 ]
# set up static ip
# https://askubuntu.com/questions/1029531/how-to-setup-a-static-ip-on-ubuntu-server-18-04

Loading…
Cancel
Save