get it actually working, with the correct cloudimage and stuff ;P

This commit is contained in:
Carl Suckfuell 2022-06-29 14:15:52 +02:00
parent 67c9db90a0
commit c7a431916f
6 changed files with 48 additions and 31 deletions

View file

@ -11,7 +11,7 @@
- name: vmname - name: vmname
prompt: Name of the VM prompt: Name of the VM
private: no private: no
- name: image_capacity - name: hdd
prompt: Disk capacity prompt: Disk capacity
private: no private: no
default: 10G default: 10G
@ -24,5 +24,6 @@
private: no private: no
default: 1 default: 1
become: false become: false
gather_subset: ['!all']
roles: roles:
- create-vm - create-vm

View file

@ -1,8 +0,0 @@
---
image_url: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-nocloud-amd64.qcow2
image_checksum: fd77540aa77f4f5ed3a817d530adfc52d142e93e61c73a85f15422a68c56dcbd39799e5bb2195e521f99a8fa301fa6bf07a478cd27bd380d4c7054901b4c8256
base_image: "{{ image_url | urlsplit('path') | basename }}"
images_dir: /var/vm/
os: debian11
vm_host_bridge_interface: eno1
recreate: false

View file

@ -18,7 +18,6 @@
module: template module: template
src: vm_hostvars.j2 src: vm_hostvars.j2
dest: ./host_vars/{{ vmname }}/vars.yml dest: ./host_vars/{{ vmname }}/vars.yml
#mode: 0666
- name: add vm to hosts - name: add vm to hosts
local_action: local_action:

View file

@ -1,4 +1,6 @@
--- ---
- include_vars: vault
- name: check if vm name exists in hostvars - name: check if vm name exists in hostvars
local_action: stat path="host_vars/{{ vmname }}" local_action: stat path="host_vars/{{ vmname }}"
register: register_name register: register_name
@ -7,13 +9,9 @@
debug: debug:
msg: "The file or directory exists" msg: "The file or directory exists"
failed_when: register_name.stat.exists failed_when: register_name.stat.exists
when:
- register_name.stat.exists
- not recreate
- name: add new vm to hostvars - name: add new vm to hostvars
include_tasks: create_hostvars.yml include_tasks: create_hostvars.yml
when: not recreate
- name: install libvirt and co - name: install libvirt and co
package: package:
@ -50,10 +48,12 @@
url: '{{ image_url }}' url: '{{ image_url }}'
dest: '{{ images_dir }}' dest: '{{ images_dir }}'
checksum: 'sha512:{{ image_checksum }}' checksum: 'sha512:{{ image_checksum }}'
owner: libvirt-qemu
group: libvirt-qemu
when: not recent_cloudimage.matched when: not recent_cloudimage.matched
- name: Create VM image from base image - name: Create VM image from base image
command: qemu-img create -b {{ base_image }} -f qcow2 -F qcow2 {{ images_dir }}{{ vmname }}.img {{ image_capacity }} command: qemu-img create -b {{ base_image }} -f qcow2 -F qcow2 {{ images_dir }}{{ vmname }}.img {{ hdd }}
- name: Create user-data - name: Create user-data
template: template:
@ -65,16 +65,11 @@
src: meta-data.j2 src: meta-data.j2
dest: '{{ images_dir }}/meta-data' dest: '{{ images_dir }}/meta-data'
- name: Create cloud-init configuration image
command: genisoimage -output {{ images_dir }}/{{ vmname }}-cidata.iso -V cidata -r -J {{ images_dir }}/user-data {{ images_dir }}/meta-data
become: true
- name: make sure setuid for qemu-bridge-helper is set - name: make sure setuid for qemu-bridge-helper is set
file: file:
path: /usr/lib/qemu/qemu-bridge-helper path: /usr/lib/qemu/qemu-bridge-helper
mode: 04755 mode: 04755
become: true become: true
- name: Create the VM - name: Create the VM
command: virt-install --name={{ vmname }} --ram={{ ram }} --vcpus={{ vcpus }} --import --disk path={{ images_dir }}{{ vmname }}.img,format=qcow2 --disk path={{ images_dir }}{{ vmname }}-cidata.iso,device=cdrom --os-variant {{ os }} --network bridge=br0,model=virtio --graphics vnc,listen=0.0.0.0 --noautoconsole command: virt-install --name={{ vmname }} --ram={{ ram }} --vcpus={{ vcpus }} --import --disk path={{ images_dir }}{{ vmname }}.img,format=qcow2 --cloud-init meta-data={{ images_dir }}meta-data,user-data={{ images_dir }}user-data --os-variant {{ os }} --network bridge=br0,model=virtio --graphics vnc,listen=0.0.0.0 --noautoconsole

View file

@ -1,11 +1,32 @@
#cloud-config #cloud-config
locale: "de_DE.UTF-8" hostname: '{{ vmname }}'
packages: ssh_pwauth: false
- screen # sets authorized ssh-keys keys for probably all users
- ripgrep
- ranger
users:
- default:
ssh_authorized_keys: ssh_authorized_keys:
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEGThAm6K/gH+36Q616He7Hykd3HEMVMifsbSlXuw9j7 carl@work {% for key in ssh_pubkeys %}
- {{ key }}
# sets the default's users password
password:{{ vms_pass }}
##
# use the following, to set individual user passwords
##
# chpasswd:
# expire: false
# list:
# - user:{{ vms_pass }}
# - root:{{ vms_pass }}
#
# users:
# - name: user
# groups: sudo
# shell: /bin/bash
# ssh_authorized_keys:
#{% for key in ssh_pubkeys %}
# - {{ key }}
#{% endfor %}
# - name: root
# ssh_authorized_keys:
#{% for key in ssh_pubkeys %}
# - {{ key }}
#{% endfor %}

View file

@ -0,0 +1,9 @@
---
image_url: https://cloud.debian.org/images/cloud/bullseye/latest/debian-11-generic-amd64.qcow2
image_checksum: b90e042b6d1ada948a34fda8fb8ae6d41358f490512f963e9abdd6f7864d97d0394f08a2df974d31df44bd19b84df8a9bfc396d5628ee7b2f695b4565db410e5
base_image: "{{ image_url | urlsplit('path') | basename }}"
images_dir: /var/vm/
os: debian11
vm_host_bridge_interface: eno1
# create a new vault with this key in vars, to change the default login pass
vms_pass: "{{ vaulted_vms_pass }}"