add the current state of the vmsetup
This commit is contained in:
commit
d3675f34bf
19 changed files with 985 additions and 0 deletions
29
roles/create-vm/tasks/create_hostvars.yml
Normal file
29
roles/create-vm/tasks/create_hostvars.yml
Normal file
|
@ -0,0 +1,29 @@
|
|||
---
|
||||
- name: create mac address
|
||||
local_action:
|
||||
module: command
|
||||
cmd: python macgen.py
|
||||
chdir: "{{ role_path }}/files"
|
||||
register: macgen
|
||||
check_mode: false
|
||||
|
||||
- name: create folder for new vm
|
||||
local_action:
|
||||
module: file
|
||||
path: "./host_vars/{{ vmname }}"
|
||||
state: directory
|
||||
|
||||
- name: create hostvars for new vm
|
||||
local_action:
|
||||
module: template
|
||||
src: vm_hostvars.j2
|
||||
dest: ./host_vars/{{ vmname }}/vars.yml
|
||||
#mode: 0666
|
||||
|
||||
- name: add vm to hosts
|
||||
local_action:
|
||||
module: lineinfile
|
||||
path: hosts
|
||||
insertafter: '^\[vms\]'
|
||||
line: "{{ vmname }}"
|
||||
|
73
roles/create-vm/tasks/main.yml
Normal file
73
roles/create-vm/tasks/main.yml
Normal file
|
@ -0,0 +1,73 @@
|
|||
---
|
||||
- name: check if vm name exists in hostvars
|
||||
local_action: stat path="host_vars/{{ vmname }}"
|
||||
register: register_name
|
||||
|
||||
- name: Stop if host_vars exist
|
||||
debug:
|
||||
msg: "The file or directory exists"
|
||||
failed_when: register_name.stat.exists
|
||||
when:
|
||||
- register_name.stat.exists
|
||||
- not recreate
|
||||
|
||||
- name: add new vm to hostvars
|
||||
include_tasks: create_hostvars.yml
|
||||
when: not recreate
|
||||
|
||||
- name: install libvirt and co
|
||||
package:
|
||||
name:
|
||||
- libvirt-daemon-system
|
||||
- qemu-system
|
||||
- virtinst
|
||||
- qemu-utils
|
||||
- libvirt-clients
|
||||
- genisoimage
|
||||
state: latest
|
||||
become: true
|
||||
|
||||
- name: Create images directory
|
||||
file:
|
||||
path: '{{ images_dir }}'
|
||||
state: directory
|
||||
owner: libvirt-qemu
|
||||
group: libvirt-qemu
|
||||
become: true
|
||||
|
||||
- name: check for cloudimage
|
||||
find:
|
||||
age: -26w
|
||||
path: '{{ images_dir }}'
|
||||
pattern: '{{ base_image }}'
|
||||
register: recent_cloudimage
|
||||
|
||||
- debug:
|
||||
msg: "{{ recent_cloudimage.matched }}"
|
||||
|
||||
- name: download cloud image template, if none found or to old
|
||||
get_url:
|
||||
url: '{{ image_url }}'
|
||||
dest: '{{ images_dir }}'
|
||||
checksum: 'sha512:{{ image_checksum }}'
|
||||
when: not recent_cloudimage.matched
|
||||
|
||||
- name: Create VM image from base image
|
||||
command: qemu-img create -b {{ base_image }} -f qcow2 -F qcow2 {{ images_dir }}{{ vmname }}.img {{ image_capacity }}
|
||||
|
||||
- name: Create user-data
|
||||
template:
|
||||
src: user-data.j2
|
||||
dest: '{{ images_dir }}/user-data'
|
||||
|
||||
- name: Create meta-data
|
||||
template:
|
||||
src: meta-data.j2
|
||||
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: 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
|
Loading…
Add table
Add a link
Reference in a new issue