attempt to fix libvirt playbook

This commit is contained in:
Andrew Wang 2021-09-16 00:47:36 -04:00
parent 06fc399142
commit 50146970e2
8 changed files with 101 additions and 203 deletions

View File

@ -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

View File

@ -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

View File

@ -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
View File

@ -1,5 +1,13 @@
[vm] [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 # ansible_ssh_private_key_file
[local] [local]

2
libvirt/clean.yml Normal file
View File

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

View File

@ -1,87 +1,87 @@
--- ---
# Setup mirror VM
- hosts: localhost - hosts: localhost
# what happened to the host?
tasks: tasks:
- name: Check if vm was created already - name: gather facts
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 ignore_errors: true
tasks:
- name: ubuntu iso exists
stat:
path: "{{ playbook_dir }}/vm/ubuntu20_04.iso"
register: iso_exists
# Setting up VM - name: ubuntu iso seed exists
- name: Create directory for VM 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
- 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: file:
path: "{{ playbook_dir }}/vm"
state: directory state: directory
path: {{userdata}}/{{vm.name}}
where: vmexists.stat.exists == false - name: fetch ubuntu iso
- name: Fetching ubuntu iso
get_url: get_url:
url: https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-live-server-amd64.iso url: https://releases.ubuntu.com/20.04.3/ubuntu-20.04.3-live-server-amd64.iso
dest: {{role_path}}/files/ubuntu20_04.iso dest: "{{ playbook_dir }}/vm/ubuntu20_04.iso"
when: isoexists.stat.exists == false when: iso_exists.stat.exists == false
- name: Create ubuntu iso # requires: cloud-localds (cloud-image-utils)
# install cloud-localds (cloud-image-utils)
# # Installing VMs from Ready Images
# Installing VMs from Ready Images # https://www.x386.xyz/index.php/2021/01/06/kvm-on-ubuntu-server-1/
# https://www.x386.xyz/index.php/2021/01/06/kvm-on-ubuntu-server-1/
# --network-config {{ playbook_dir }}/templates/network # apply network config
# --network-config {{ playbook_dir }}/templates/network
- name: create ubuntu iso seed
command: > command: >
cloud-localds cloud-localds
{{userdata}}/{{vm.name}}/seed.qcow2 {{ playbook_dir }}/vm/seed.qcow2
{{role_path}}/templates/user-data {{ playbook_dir }}/templates/user-data
when: seedexists.stat.exists == false when: seed_exists.stat.exists == false
- name: Create storage pool and virtual disks for mirror # get user to set net.ipv4.ip_forward = 1 ?
command: "virsh {{item}}" - name: create mirbr0 bridge network
with_items: command: "virsh {{ item }}"
- pool-define-as mirror dir --target="{{userdata}}/{{vm.name}}/" with_items:
- pool-build mirror - net-define {{ playbook_dir }}/templates/network.xml
- pool-start mirror - net-autostart mirbr0
- pool-autostart mirror - net-start mirbr0
command: "virsh vol-create-as mirror {{item}}" when: not net_exists.rc == 0
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 bridge network - name: create storage pool
# net.ipv4.ip_forward = 1 command: "virsh {{ item }}"
command: "virsh {{item}}" with_items:
with_items: - pool-define-as mirror dir --target="{{ playbook_dir }}/vm/"
- net-define {{role_path}}/templates/network.xml - pool-build mirror
- net-autostart mirbr0 - pool-start mirror
- net-start mirbr0 - pool-autostart mirror
when: not netexists.rc == 0 when: not pool_exists.rc == 0
- name: Create mirror VM # 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 vm
command: > command: >
virt-install virt-install
--name=mirror --name=mirror
@ -89,7 +89,7 @@
--vcpus=1 --vcpus=1
--boot uefi --boot uefi
--os-type linux --os-variant ubuntu20.04 --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_root1.qcow2
--disk vol=mirror/mirror_root2.qcow2 --disk vol=mirror/mirror_root2.qcow2
--disk vol=mirror/mirror_disk1.qcow2 --disk vol=mirror/mirror_disk1.qcow2
@ -99,19 +99,18 @@
--network bridge=mirbr0 --network bridge=mirbr0
--graphics vnc,port=5911,listen=127.0.0.1 --graphics vnc,port=5911,listen=127.0.0.1
--noautoconsole --noautoconsole
when: not vmexists.rc == 0 when: not vm_exists.rc == 0
# in the vm to disable cloud-init run # in the vm to disable cloud-init run
# sudo touch /etc/cloud/cloud-init.disabled # 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 # copy over pub key into /root/.ssh/authorized_keys
# add line to ssh config that allows ssh as root # add line to ssh config that allows ssh as root
# Configure mirror VM - hosts: 192.168.123.2
- hosts: mirror
roles: roles:
- zfs - zfs
# - index # - index
# - nginx # - nginx
- rsync # - rsync
# - ftp # - ftp

View File

@ -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

View File

@ -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 #cloud-config
version: 2 version: 2
ethernets: ethernets:
enp1s0: enp1s0:
dhcp4: false dhcp4: false
addresses: [ 192.168.0.221/24 ] addresses: [ 192.168.123.2/24 ]
gateway4: 192.168.0.1 gateway4: 192.168.123.1
nameservers: 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