some refactoring + user config variables in playbook

autoautoinstall
Daniel Liu 1 year ago
parent 9d33c1aa93
commit 076dc6f191
  1. 2
      ansible.cfg
  2. 18
      hosts
  3. 19
      hosts.yml
  4. 56
      mirror-vm
  5. 2
      mirror-vm.yml
  6. 7
      playbook.yml
  7. 48
      roles/qemu-vm/tasks/main.yml
  8. 0
      roles/vm/files/meta-data
  9. 0
      roles/vm/files/user-data
  10. 106
      roles/vm/tasks/main-libvirt.yml
  11. 6
      roles/vm/tasks/main.yml
  12. 108
      roles/vm/tasks/vm.yml
  13. 12
      roles/vm/templates/network
  14. 14
      roles/vm/templates/network.xml
  15. 60
      roles/vm/templates/user-data

@ -1,2 +1,2 @@
[defaults]
inventory = hosts
inventory = hosts.yml

18
hosts

@ -1,18 +0,0 @@
[local]
localhost
[vm]
ubuntu@localhost
[local:vars]
ansible_connection=local
vm_name=mirror-env
vm_mem=2G
vm_disk_size=10G
ovmf_location=/usr/share/edk2-ovmf/x64/OVMF_CODE.fd
[vm:vars]
ansible_connection=ssh
ansible_port=7777
ansible_user=ubuntu
ansible_password=ubuntu

@ -0,0 +1,19 @@
local:
hosts:
localhost
vars:
ansible_connection: local
userdata: {{playbook_dir}}/userdata
virtual_machines:
- name: mirror-env
ram: 2G
disk_size: 10G
vm:
hosts:
ubuntu@localhost
vars:
ansible_connection: ssh
ansible_port: 7777
ansible_user: ubuntu
ansible_password: ubuntu

@ -1,56 +0,0 @@
#!/usr/bin/make -sf
# script for managing the vm for the csc mirror dev environment
# dependencies =-=-=-=-=-=-=-=
# qemu, genisoimage, ovmf (find the location of OVMF_CODE.fd on your system)
# user config variables =-=-=
disk_size=10G
vm_mem=2G
ovmf_location=/usr/share/edk2-ovmf/x64/OVMF_CODE.fd
install_iso=./ubuntu-20.04.2-live-server-amd64.iso
ssh_port=7777
.PHONY: help create clean run-install run
help:
echo help|create|clean|run-install|run
.ONESHELL:
create:
for i in {1..6}; do
qemu-img create -f qcow2 drive$$i ${disk_size}; done
genisoimage -output seed.iso -volid cidata -joliet -rock user-data meta-data
clean:
rm drive* seed.iso
run-install:
qemu-system-x86_64 -cdrom ${install_iso} -enable-kvm -boot order=d \
-drive file=drive1,if=virtio,id=a,media=disk,format=qcow2 \
-drive file=drive2,if=virtio,id=b,media=disk,format=qcow2 \
-drive file=drive3,if=virtio,id=c,media=disk,format=qcow2 \
-drive file=drive4,if=virtio,id=d,media=disk,format=qcow2 \
-drive file=drive5,if=virtio,id=e,media=disk,format=qcow2 \
-drive file=drive6,if=virtio,id=f,media=disk,format=qcow2 \
-drive file=seed.iso,if=virtio,format=raw \
-bios ${ovmf_location} \
-m ${vm_mem}
post-install:
run:
qemu-system-x86_64 -enable-kvm -boot order=d \
-drive file=drive1,if=virtio,id=a,media=disk,format=qcow2 \
-drive file=drive2,if=virtio,id=b,media=disk,format=qcow2 \
-drive file=drive3,if=virtio,id=c,media=disk,format=qcow2 \
-drive file=drive4,if=virtio,id=d,media=disk,format=qcow2 \
-drive file=drive5,if=virtio,id=e,media=disk,format=qcow2 \
-drive file=drive6,if=virtio,id=f,media=disk,format=qcow2 \
-bios ${ovmf_location} \
-m ${vm_mem} \
-net user,hostfwd=tcp::${ssh_port}-:22 \
-net nic

@ -2,4 +2,4 @@
- hosts: local
become: true
roles:
- qemu-vm
- vm

@ -1,7 +0,0 @@
---
- hosts: all
become: true
roles:
- setup
- zfs
- rsync

@ -1,48 +0,0 @@
---
- name: "{{vm_name}} - Gathering info"
stat:
path: "{{playbook_dir}}/vm/{{vm_name}}"
register: vm_stat
- name: "{{vm_name}} - Checking if VM already exists"
meta: end_play
when: vm_stat.stat.exists == true
# Start vm install
- name: "{{vm_name}} - Create directory for VM"
file:
state: directory
path: "{{playbook_dir}}/vm/{{vm_name}}"
- name: "{{vm_name}} - Create disk images"
shell:
cmd: "for i in {1..6}; do qemu-img create -f qcow2 {{playbook_dir}}/vm/{{vm_name}}/drive${i} {{vm_disk_size}}; done"
- name: "{{vm_name}} - Creating cloud-init iso"
command: "genisoimage -output {{playbook_dir}}/vm/{{vm_name}}/seed.iso -volid cidata -joliet -rock {{role_path}}/files/user-data {{role_path}}/files/meta-data"
# Autoinstall ubuntu on vm
- name: "{{vm-name}} - Check if iso exists"
stat:
path: "{{role_path}}/files/ubuntu20_04.iso"
register: iso_stat
- name: "{{vm-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: iso_stat.stat.exists == false
- name: "{{vm_name}} - Starting VM install"
command: >
qemu-system-x86_64 -cdrom {{role_path}}/files/ubuntu20_04.iso -enable-kvm -boot order=d
-drive file={{playbook_dir}}/vm/{{vm_name}}/drive1,if=virtio,id=a,media=disk,format=qcow2
-drive file={{playbook_dir}}/vm/{{vm_name}}/drive2,if=virtio,id=b,media=disk,format=qcow2
-drive file={{playbook_dir}}/vm/{{vm_name}}/drive3,if=virtio,id=c,media=disk,format=qcow2
-drive file={{playbook_dir}}/vm/{{vm_name}}/drive4,if=virtio,id=d,media=disk,format=qcow2
-drive file={{playbook_dir}}/vm/{{vm_name}}/drive5,if=virtio,id=e,media=disk,format=qcow2
-drive file={{playbook_dir}}/vm/{{vm_name}}/drive6,if=virtio,id=f,media=disk,format=qcow2
-drive file={{playbook_dir}}/vm/{{vm_name}}/seed.iso,if=virtio,format=raw
-bios {{ovmf_location}}
-m {{vm_mem}}

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

@ -0,0 +1,6 @@
---
- name: Configure all VMs
include_tasks: vm.yml
loop: {{ virtual_machines }}
loop_control:
loop_var: vm

@ -0,0 +1,108 @@
---
# Gather info
- name: Check if vm was created already
stat:
path: {{userdata}}/{{vm.name}}
register: vmexists
ignore_errors: true
- 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
# Setting up VM
- name: Create directory for VM
file:
state: directory
path: {{userdata}}/{{vm.name}}
where: vmexists.stat.exists == false
- 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
- 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
{{userdata}}/{{vm.name}}/seed.qcow2
{{role_path}}/templates/user-data
when: seedexists.stat.exists == false
- 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 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
- 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
# 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

@ -0,0 +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
nameservers:
addresses: [ 192.168.0.1,8.8.8.8 ]

@ -0,0 +1,14 @@
<network>
<name>mirbr0</name>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='mirbr0' stp='on' delay='0'/>
<ip address='192.168.123.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.123.2' end='192.168.123.254'/>
</dhcp>
</ip>
</network>

@ -0,0 +1,60 @@
#cloud-config
autoinstall:
version: 1
identity:
hostname: mirror
username: ubuntu
# mkpasswd --method=SHA-512 --rounds=4096
# password is just ubuntu
password: "$6$exDY1mhS4KUYCE/2$zmn9ToZwTKLhCw.b4/b.ZRTIZM30JZ4QrOQ2aOXJ8yk96xpcCof0kxKwuX1kqLG/ygbJ1f8wxED22bTL4F46P0"
locale: en_US
ssh:
allow-pw: true
authorized-keys: []
install-server: true
storage:
config:
# disks =-=-=-=-=-=-=
- {id: vda, name: '', path: /dev/vda, type: disk, ptable: gpt,
preserve: false, grub_device: false}
- {id: vdb, name: '', path: /dev/vdb, type: disk, ptable: gpt,
preserve: false, grub_device: false}
# partitions =-=-=-=-=-=
# vda1
- {id: vda1, type: partition, size: 500M, device: vda, number: 1,
preserve: false, wipe: superblock, flag: boot, grub_device: true}
- {id: vda1-format, type: format, volume: vda1,
fstype: fat32, preserve: false}
# vda2
- {id: vda2, type: partition, size: 9G, device: vda, number: 2,
preserve: false, wipe: superblock, flag: '', grub_device: false}
# vdb1
- {id: vdb1, type: partition, size: 500M, device: vdb, number: 1,
preserve: false, wipe: superblock, flag: boot, grub_device: true}
- {id: vdb1-format, type: format, volume: vdb1,
fstype: fat32, preserve: false}
# vdb2
- {id: vdb2, type: partition, size: 9G, device: vdb, number: 2,
preserve: false, wipe: superblock, flag: '', grub_device: false}
# raid =-=-=-=-=-=
- id: md0
name: md0
type: raid
raidlevel: raid1
devices: [vda2, vdb2]
spare_devices: []
preserve: false
# lvm =-=-=-=-=-=
- id: vg0
name: vg0
type: lvm_volgroup
devices: [md0]
preserve: false
# partition
- {id: vg0-root, name: root, type: lvm_partition, size: 8G,
preserve: false, volgroup: vg0}
- {id: vg0-root-format, type: format, volume: vg0-root,
fstype: ext4, preserve: false}
# mount points =-=-=-=-=-=
- {id: boot-mount, type: mount, path: /boot/efi, device: vda1-format}
- {id: root-mount, type: mount, path: /, device: vg0-root-format}
Loading…
Cancel
Save