first draft

This commit is contained in:
Martin Wohlert 2021-06-06 21:46:18 +02:00
commit 09d4930cd2
14 changed files with 308 additions and 0 deletions

2
ansible.cfg Normal file
View File

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

3
group_vars/all.yml Normal file
View File

@ -0,0 +1,3 @@
ansible_ssh_private_key_file: private/keyfile
libvirt:
images: /var/lib/libvirt/images

View File

@ -0,0 +1,6 @@
virt:
host: localhost
image: fedora-34-x64.qcow2
cpus: 2
ram: 2048
hdd: 10g

0
host_vars/localhost Normal file
View File

9
hosts Normal file
View File

@ -0,0 +1,9 @@
[virt]
localhost ansible_connection=local
[docker_nodes]
docker[1:3]
[gitlab]
[zabbix]

View File

@ -0,0 +1,13 @@
---
- hosts: virt
become: true
gather_facts: false
roles:
- virt
- hosts: docker_nodes
gather_facts: false
serial: 1
become: true
roles:
- virt_vm

2
private/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*
!/.gitignore

31
roles/virt/tasks/main.yml Normal file
View File

@ -0,0 +1,31 @@
---
- name: fetch images
get_url:
dest: "{{ libvirt.images }}/{{ item.dest }}"
group: root
owner: root
mode: u=rw,g=r,o=r
url: "{{ item.url }}"
checksum: "{{ item.checksum }}"
with_items:
- dest: fedora-34-x64.qcow2
checksum: sha1:fc08ad81a893b8253ec3bf6e25273cbb4fc8eafb
url: https://download.fedoraproject.org/pub/fedora/linux/releases/34/Cloud/x86_64/images/Fedora-Cloud-Base-34-1.2.x86_64.qcow2
- name: virt_net "docker" present
virt_net:
autostart: yes
name: docker
state: present
xml: '{{ lookup("template", "network/docker.xml.j2") }}'
- name: virt_net "docker" autostart
virt_net:
name: docker
autostart: yes
- name: virt_net "docker" active
virt_net:
name: docker
state: active
autostart: yes

View File

@ -0,0 +1,10 @@
<network>
<name>docker</name>
<bridge name="virbr1" stp="on" delay="0"/>
<domain name="docker"/>
<ip address="192.168.100.1" netmask="255.255.255.0">
<dhcp>
<range start="192.168.100.101" end="192.168.100.200"/>
</dhcp>
</ip>
</network>

View File

@ -0,0 +1,71 @@
---
# VM PREPARATION
- name: create vm storage
delegate_to: "{{ virt.host }}"
copy:
src: "{{ libvirt.images }}/{{ virt.image }}"
dest: "{{ libvirt.images }}/{{ inventory_hostname }}.qcow2"
force: no
- name: set vm storage size
delegate_to: "{{ virt.host }}"
shell:
cmd: qemu-img resize "{{ libvirt.images }}/{{ inventory_hostname }}.qcow2" "{{ virt.hdd }}"
- name: define vm
delegate_to: "{{ virt.host }}"
virt:
name: "{{ inventory_hostname }}"
xml: '{{ lookup("template", "fedora-34.xml.j2") }}'
command: define
- name: make temp dir for cloud-init (CI)
delegate_to: "{{ virt.host }}"
tempfile:
state: directory
register: cidata
# CLOUD-INIT PREPARATION
- name: create CI-metadata
delegate_to: "{{ virt.host }}"
template:
src: ci-metadata.j2
dest: "{{ cidata.path }}/meta-data"
- name: create CI-userdata
delegate_to: "{{ virt.host }}"
template:
src: ci-userdata.j2
dest: "{{ cidata.path }}/user-data"
- name: create CI ISO
delegate_to: "{{ virt.host }}"
shell:
cmd: genisoimage -output "{{ libvirt.images }}/{{ inventory_hostname }}-cidata.iso" -volid cidata -joliet -rock user-data meta-data
chdir: "{{ cidata.path }}"
# VM INIT
- name: attach CI to vm
delegate_to: "{{ virt.host }}"
shell:
cmd: virsh attach-disk "{{ inventory_hostname }}" --source "{{ libvirt.images }}/{{ inventory_hostname }}-cidata.iso" --target sda --type cdrom --mode readonly --persistent
# CLOUD-INIT CLEANUP
- name: delete CI temp dir
delegate_to: "{{ virt.host }}"
file:
path: "{{ cidata.path }}"
state: absent
force: true
- name: delete CI ISO
delegate_to: "{{ virt.host }}"
file:
path: "{{ libvirt.images }}/{{ inventory_hostname }}-cidata.iso"
state: absent

View File

@ -0,0 +1,13 @@
---
- name: check vm existance
delegate_to: "{{ virt.host }}"
stat:
path: "{{ libvirt.images }}/{{ inventory_hostname }}.qcow2"
get_checksum: false
get_attributes: false
get_mime: false
register: vmimg
- name: create vm
include_tasks: create_vm.yml
when: not vmimg.stat.exists

View File

@ -0,0 +1,2 @@
instance-id: {{ inventory_hostname }}
local-hostname: {{ inventory_hostname }}

View File

@ -0,0 +1,9 @@
#cloud-config
users:
- name: ansible
ssh_authorized_keys:
- {{ lookup("file", "private/keyfile.pub") }}
sudo: ['ALL=(ALL) NOPASSWD:ALL']
groups: sudo
shell: /bin/bash

View File

@ -0,0 +1,137 @@
<domain type="kvm">
<name>{{ inventory_hostname }}</name>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://oracle.com/ol/8.2"/>
</libosinfo:libosinfo>
</metadata>
<memory unit="MiB">{{ virt.ram }}</memory>
<currentMemory unit="MiB">{{ virt.ram }}</currentMemory>
<vcpu placement="static">{{ virt.cpus }}</vcpu>
<os>
<type arch="x86_64" machine="pc-q35-5.2">hvm</type>
<boot dev="hd"/>
</os>
<features>
<acpi/>
<apic/>
<vmport state="off"/>
</features>
<cpu mode="host-model" check="partial"/>
<clock offset="utc">
<timer name="rtc" tickpolicy="catchup"/>
<timer name="pit" tickpolicy="delay"/>
<timer name="hpet" present="no"/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled="no"/>
<suspend-to-disk enabled="no"/>
</pm>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<disk type="file" device="disk">
<driver name="qemu" type="qcow2" discard="unmap" detect_zeroes="unmap"/>
<source file="{{ libvirt.images }}/{{ inventory_hostname }}.qcow2"/>
<target dev="vda" bus="virtio"/>
<address type="pci" domain="0x0000" bus="0x04" slot="0x00" function="0x0"/>
</disk>
<controller type="usb" index="0" model="qemu-xhci" ports="15">
<address type="pci" domain="0x0000" bus="0x02" slot="0x00" function="0x0"/>
</controller>
<controller type="sata" index="0">
<address type="pci" domain="0x0000" bus="0x00" slot="0x1f" function="0x2"/>
</controller>
<controller type="pci" index="0" model="pcie-root"/>
<controller type="pci" index="1" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="1" port="0x10"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x0" multifunction="on"/>
</controller>
<controller type="pci" index="2" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="2" port="0x11"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x1"/>
</controller>
<controller type="pci" index="3" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="3" port="0x12"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x2"/>
</controller>
<controller type="pci" index="4" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="4" port="0x13"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x3"/>
</controller>
<controller type="pci" index="5" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="5" port="0x14"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x4"/>
</controller>
<controller type="pci" index="6" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="6" port="0x15"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x5"/>
</controller>
<controller type="pci" index="7" model="pcie-root-port">
<model name="pcie-root-port"/>
<target chassis="7" port="0x16"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x02" function="0x6"/>
</controller>
<controller type="virtio-serial" index="0">
<address type="pci" domain="0x0000" bus="0x03" slot="0x00" function="0x0"/>
</controller>
<interface type="network">
<source network="docker"/>
<model type="virtio"/>
<address type="pci" domain="0x0000" bus="0x01" slot="0x00" function="0x0"/>
</interface>
<serial type="pty">
<target type="isa-serial" port="0">
<model name="isa-serial"/>
</target>
</serial>
<console type="pty">
<target type="serial" port="0"/>
</console>
<channel type="unix">
<target type="virtio" name="org.qemu.guest_agent.0"/>
<address type="virtio-serial" controller="0" bus="0" port="1"/>
</channel>
<channel type="spicevmc">
<target type="virtio" name="com.redhat.spice.0"/>
<address type="virtio-serial" controller="0" bus="0" port="2"/>
</channel>
<input type="tablet" bus="usb">
<address type="usb" bus="0" port="1"/>
</input>
<input type="mouse" bus="ps2"/>
<input type="keyboard" bus="ps2"/>
<graphics type="spice" autoport="yes">
<listen type="address"/>
<image compression="off"/>
</graphics>
<sound model="ich9">
<address type="pci" domain="0x0000" bus="0x00" slot="0x1b" function="0x0"/>
</sound>
<video>
<model type="qxl" ram="65536" vram="65536" vgamem="16384" heads="1" primary="yes"/>
<address type="pci" domain="0x0000" bus="0x00" slot="0x01" function="0x0"/>
</video>
<redirdev bus="usb" type="spicevmc">
<address type="usb" bus="0" port="2"/>
</redirdev>
<redirdev bus="usb" type="spicevmc">
<address type="usb" bus="0" port="3"/>
</redirdev>
<memballoon model="virtio">
<address type="pci" domain="0x0000" bus="0x05" slot="0x00" function="0x0"/>
</memballoon>
<rng model="virtio">
<backend model="random">/dev/urandom</backend>
<address type="pci" domain="0x0000" bus="0x06" slot="0x00" function="0x0"/>
</rng>
</devices>
</domain>