aboutsummaryrefslogtreecommitdiffstats
path: root/roles/vmm/tasks
diff options
context:
space:
mode:
authorbinary <me@rgoncalves.se>2020-11-07 20:38:07 +0100
committerbinary <me@rgoncalves.se>2020-11-07 20:38:07 +0100
commit352c98322771af117a85904d580e1c062062d634 (patch)
treed3bca9d27d6ee5d678264d83fc22030d274f30bf /roles/vmm/tasks
parent282be811d2ac07a76d56ac066df5bf33d748bd64 (diff)
downloadinfrastructure-352c98322771af117a85904d580e1c062062d634.tar.gz
Refactor vmm role with automation
Diffstat (limited to 'roles/vmm/tasks')
-rw-r--r--roles/vmm/tasks/boot_alpine.yml43
-rw-r--r--roles/vmm/tasks/debug.yml5
-rw-r--r--roles/vmm/tasks/download_iso.yml30
-rw-r--r--roles/vmm/tasks/generate_disk.yml12
-rw-r--r--roles/vmm/tasks/generate_vmconf.yml27
-rw-r--r--roles/vmm/tasks/init_hypervisor.yml12
-rw-r--r--roles/vmm/tasks/main.yml9
-rw-r--r--roles/vmm/tasks/set_facts.yml19
8 files changed, 112 insertions, 45 deletions
diff --git a/roles/vmm/tasks/boot_alpine.yml b/roles/vmm/tasks/boot_alpine.yml
new file mode 100644
index 0000000..da4e91e
--- /dev/null
+++ b/roles/vmm/tasks/boot_alpine.yml
@@ -0,0 +1,43 @@
+
+# vmm ~~ tasks/boot_alpine.yml
+# Boot an alpine image and init its installation set.
+# Required :
+# - guest : vm to be be bootup and initialized
+
+---
+
+- name: Check arguments
+ fail:
+ msg: "arguments : guest"
+ when: guest is not defined
+
+- set_fact:
+ iso: "{{ hostvars[guest].iso }}"
+ disk_file : "{{ vmm.disk.dir }}/{{ guest }}.{{ vmm.disk.format }}"
+
+- include: set_facts.yml
+
+- name: Start vm
+ shell: |
+ vmctl stop dummy
+ sleep 2
+ vmctl start -d {{ iso_latest | quote }} \
+ -d {{ disk_file | quote }} \
+ -n {{ vmm.config.switch.name | quote }} \
+ -m {{ hostvars[guest].memory | quote }} dummy
+
+ sleep 2
+ tty=$(vmctl show | grep dummy | tr -s " " " " | cut -d " " -f 7)
+ sleep 20
+
+ cat << EOF > /dev/${tty}
+ echo "mkdir /root/.ssh"
+ echo "echo $(cat /root/.ssh/authorized_keys) > /root/.ssh/authorized_keys" > /dev/ttyp1
+ echo "apk add openssh ; rc-update add sshd ; /etc/init.d/sshd start" /dev/${tty}
+ EOF
+
+ exit 0
+
+ args:
+ executable: /bin/sh
+
diff --git a/roles/vmm/tasks/debug.yml b/roles/vmm/tasks/debug.yml
deleted file mode 100644
index c078e11..0000000
--- a/roles/vmm/tasks/debug.yml
+++ /dev/null
@@ -1,5 +0,0 @@
-
-# vmm ~~ debug.yml
-
-- debug:
- var: vmm.iso.openbsd.version
diff --git a/roles/vmm/tasks/download_iso.yml b/roles/vmm/tasks/download_iso.yml
index c767c05..6f0c5f6 100644
--- a/roles/vmm/tasks/download_iso.yml
+++ b/roles/vmm/tasks/download_iso.yml
@@ -1,34 +1,28 @@
# vmm ~~ tasks/download_iso.yml
+# Download an iso file to hypervisor.
+# Required :
+# - iso : name of an iso file defined in vars/main.yml
---
-- meta: end_play
+- name: Check arguments
+ fail:
+ msg: "arguments : iso"
when: iso is not defined
-- set_fact:
- symlink: "{{ vmm.iso.dir }}/{{ vmm.iso[iso].name }}-latest.iso"
-
-- set_fact:
- dest: "{{ symlink | regex_replace('latest', vmm.iso[iso].version) }}"
- url: "{{ vmm.iso[iso].url \
- | regex_replace('VERSIONDIR', vmm.iso[iso].versiondir) \
- | regex_replace('VERSION', vmm.iso[iso].version) }}"
-
- sha256: "{{ vmm.iso[iso].sha256 \
- | regex_replace('VERSIONDIR', vmm.iso[iso].versiondir) \
- | regex_replace('VERSION', vmm.iso[iso].version) }}"
+- include: set_facts.yml
- name: Get latest iso
get_url:
- url: "{{ url }}"
- dest: "{{ dest }}"
+ url: "{{ iso_url }}"
+ dest: "{{ iso_file }}"
force: "{{ force | default('no') }}"
- checksum: "sha256:{{ sha256 }}"
+ checksum: "sha256:{{ iso_checksum }}"
- name: Symlink latest iso
file:
- src: "{{ dest }}"
- dest: "{{ symlink }}"
+ src: "{{ iso_file }}"
+ dest: "{{ iso_latest }}"
state: link
diff --git a/roles/vmm/tasks/generate_disk.yml b/roles/vmm/tasks/generate_disk.yml
index c8ec091..1c44471 100644
--- a/roles/vmm/tasks/generate_disk.yml
+++ b/roles/vmm/tasks/generate_disk.yml
@@ -5,17 +5,17 @@
- name: Check existence of disk directory
file:
- path: "{{ vmm.disk.dir }}"
+ path: "{{ vmm.disk_dir }}"
state: directory
- name: Get existing disk file size
stat:
- path: "{{ vmm.disk.dir }}/{{ hostvars[vm].ansible_host }}.{{ vmm.disk.format }}"
+ path: "{{ vmm.disk_dir }}/{{ guest.name }}.{{ vmm.disk_format }}"
register: st
-- name: Generate vm disk
+- name: Generate vm disk for {{ guest.name }}
shell: |
- disk="{{ vmm.disk.dir }}/{{ hostvars[vm].ansible_host }}.{{ vmm.disk.format }}"
+ disk="{{ vmm.disk_dir }}/{{ guest.name }}.{{ vmm.disk_format }}"
rm "${disk}"
- vmctl create -s {{ hostvars[vm].size }} "${disk}"
- when: not st.stat.exists or st.stat.size == 0
+ vmctl create -s {{ guest.size }} "${disk}"
+ when: not st.stat.exists or st.stat.size < 32
diff --git a/roles/vmm/tasks/generate_vmconf.yml b/roles/vmm/tasks/generate_vmconf.yml
index 9f120fd..c4e80d8 100644
--- a/roles/vmm/tasks/generate_vmconf.yml
+++ b/roles/vmm/tasks/generate_vmconf.yml
@@ -1,29 +1,24 @@
# vmm ~~ tasks/generate_vmconf.yml
+# Generate vm.conf on hypervisor.
---
+- name: Create network switch
+ template:
+ src: templates/hostname.j2
+ dest: "/etc/hostname.{{ vmm.switch.interface }}"
+
+- name: Start network switch
+ shell: "sh /etc/netstart {{ vmm.switch.interface }}"
+
- name: Generate vmm configuration
template:
src: templates/vm.conf.j2
- dest: "{{ vmm.config.file }}"
-
-- name: Create empty disk
- file:
- path: "{{ vmm.disk.dir }}/{{ hostvars[item].ansible_host }}.{{ vmm.disk.format }}"
- state: touch
- loop: "{{ vm.hosts }}"
+ dest: "{{ vmm.config_file }}"
-- name: Restart and enable vmd
+- name: Restart vmd
service:
name: vmd
state: restarted
enabled: true
- when: vm.enabled
-
-- name: Stop and disable vmd
- service:
- name: vmd
- state: stopped
- enabled: false
- when: not vm.enabled
diff --git a/roles/vmm/tasks/init_hypervisor.yml b/roles/vmm/tasks/init_hypervisor.yml
new file mode 100644
index 0000000..c963bbb
--- /dev/null
+++ b/roles/vmm/tasks/init_hypervisor.yml
@@ -0,0 +1,12 @@
+
+# vmm ~~ tasks/init_hypervisor.yml
+
+---
+
+- include_tasks: download_iso.yml
+ vars:
+ iso="{{ item.name }}"
+ with_items: "{{ vmm.iso }}"
+
+- include: generate_vmconf.yml guest="{{ item }}"
+ loop: "{{ hostvars[ansible_host] }}.vm.hosts"
diff --git a/roles/vmm/tasks/main.yml b/roles/vmm/tasks/main.yml
index 8d5609a..9011497 100644
--- a/roles/vmm/tasks/main.yml
+++ b/roles/vmm/tasks/main.yml
@@ -3,3 +3,12 @@
---
+- name: Download all iso files
+ include: download_iso.yml iso={{ item }}
+ with_items: "{{ vmm.iso }}"
+
+- name: Generate disks for all vms
+ include: generate_disk.yml guest={{ item }}
+ with_items: "{{ vms }}"
+
+- include: generate_vmconf.yml
diff --git a/roles/vmm/tasks/set_facts.yml b/roles/vmm/tasks/set_facts.yml
new file mode 100644
index 0000000..2acc458
--- /dev/null
+++ b/roles/vmm/tasks/set_facts.yml
@@ -0,0 +1,19 @@
+
+# vmm ~~ tasks/set_fact.yml
+
+---
+
+- set_fact:
+ iso_latest: "{{ vmm.iso_dir }}/{{ vmm.iso[iso].name }}-latest.iso"
+ when: iso is defined
+
+- set_fact:
+ iso_file: "{{ iso_latest | regex_replace('latest', vmm.iso[iso].version) }}"
+ iso_url: "{{ vmm.iso[iso].url \
+ | regex_replace('VERSIONDIR', vmm.iso[iso].versiondir) \
+ | regex_replace('VERSION', vmm.iso[iso].version) }}"
+ iso_checksum: "{{ vmm.iso[iso].sha256 \
+ | regex_replace('VERSIONDIR', vmm.iso[iso].versiondir) \
+ | regex_replace('VERSION', vmm.iso[iso].version) }}"
+ when: iso is defined
+
remember that computers suck.