diff options
author | binary <me@rgoncalves.se> | 2020-11-04 21:18:39 +0100 |
---|---|---|
committer | binary <me@rgoncalves.se> | 2020-11-04 21:18:39 +0100 |
commit | 282be811d2ac07a76d56ac066df5bf33d748bd64 (patch) | |
tree | b7287f79b77604ab1c14600da2a63f39ec51a798 | |
parent | aa47761cf59f2f8944e961227053a98bb57c25fe (diff) | |
download | infrastructure-282be811d2ac07a76d56ac066df5bf33d748bd64.tar.gz |
Init basic action for vmm hypervisor
-rw-r--r-- | roles/vmm/tasks/download_iso.yml | 34 | ||||
-rw-r--r-- | roles/vmm/tasks/generate_disk.yml | 21 | ||||
-rw-r--r-- | roles/vmm/tasks/generate_vmconf.yml | 29 | ||||
-rw-r--r-- | roles/vmm/templates/vm.conf.j2 | 22 |
4 files changed, 106 insertions, 0 deletions
diff --git a/roles/vmm/tasks/download_iso.yml b/roles/vmm/tasks/download_iso.yml new file mode 100644 index 0000000..c767c05 --- /dev/null +++ b/roles/vmm/tasks/download_iso.yml @@ -0,0 +1,34 @@ + +# vmm ~~ tasks/download_iso.yml + +--- + +- meta: end_play + 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) }}" + +- name: Get latest iso + get_url: + url: "{{ url }}" + dest: "{{ dest }}" + force: "{{ force | default('no') }}" + checksum: "sha256:{{ sha256 }}" + +- name: Symlink latest iso + file: + src: "{{ dest }}" + dest: "{{ symlink }}" + state: link + diff --git a/roles/vmm/tasks/generate_disk.yml b/roles/vmm/tasks/generate_disk.yml new file mode 100644 index 0000000..c8ec091 --- /dev/null +++ b/roles/vmm/tasks/generate_disk.yml @@ -0,0 +1,21 @@ + +# vmm ~~ tasks/generate_disk.yml + +--- + +- name: Check existence of disk directory + file: + path: "{{ vmm.disk.dir }}" + state: directory + +- name: Get existing disk file size + stat: + path: "{{ vmm.disk.dir }}/{{ hostvars[vm].ansible_host }}.{{ vmm.disk.format }}" + register: st + +- name: Generate vm disk + shell: | + disk="{{ vmm.disk.dir }}/{{ hostvars[vm].ansible_host }}.{{ vmm.disk.format }}" + rm "${disk}" + vmctl create -s {{ hostvars[vm].size }} "${disk}" + when: not st.stat.exists or st.stat.size == 0 diff --git a/roles/vmm/tasks/generate_vmconf.yml b/roles/vmm/tasks/generate_vmconf.yml new file mode 100644 index 0000000..9f120fd --- /dev/null +++ b/roles/vmm/tasks/generate_vmconf.yml @@ -0,0 +1,29 @@ + +# vmm ~~ tasks/generate_vmconf.yml + +--- + +- 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 }}" + +- name: Restart and enable 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/templates/vm.conf.j2 b/roles/vmm/templates/vm.conf.j2 new file mode 100644 index 0000000..88be7fb --- /dev/null +++ b/roles/vmm/templates/vm.conf.j2 @@ -0,0 +1,22 @@ +#jinja2: trim_blocks: True, lstrip_blocks: True + +{% for vm in vm.hosts %} +{% set vm = hostvars[vm] %} + +vm "{{ vm.ansible_host }}" { + {% if vm.enabled %} + enable + {% else %} + disable + {% endif %} + memory {{ vm.memory }} + disk "{{ vmm.disk.dir }}/{{ vm.ansible_host }}.{{ vmm.disk.format }}" + interface { + switch "{{ vmm.config.switch.name }}" + } +} +{% endfor %} + +switch "{{ vmm.config.switch.name }}" { + interface {{ vmm.config.switch.interface }} +} |