From 333eff6a0aeefcbf573bbf5e725fc27b5b4cb9f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Gon=C3=A7alves?= Date: Mon, 6 Dec 2021 20:10:25 +0000 Subject: roles/vmm: Setup and create vms on openbsd --- roles/vmm/tasks/disk.yml | 15 +++++++++++ roles/vmm/tasks/facts.yml | 14 ++++++++++ roles/vmm/tasks/iso.yml | 7 +++++ roles/vmm/tasks/main.yml | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 roles/vmm/tasks/disk.yml create mode 100644 roles/vmm/tasks/facts.yml create mode 100644 roles/vmm/tasks/iso.yml create mode 100644 roles/vmm/tasks/main.yml (limited to 'roles/vmm/tasks') diff --git a/roles/vmm/tasks/disk.yml b/roles/vmm/tasks/disk.yml new file mode 100644 index 0000000..24dd491 --- /dev/null +++ b/roles/vmm/tasks/disk.yml @@ -0,0 +1,15 @@ +- name: create disks + command: + cmd: vmctl create -s "{{ item.size }}" "{{ item.name }}.{{ vmm_disk_format }}" + chdir: "{{ vmm_disk_dir }}" + loop: "{{ __vms }}" + register: result + changed_when: result.rc == 0 + failed_when: + - result.rc != 0 + - "'File exists' not in result.stderr" + +- name: retrieve existing disks + find: + path: "{{ vmm_disk_dir }}" + patterns: "*.{{ vmm_disk_format }}" diff --git a/roles/vmm/tasks/facts.yml b/roles/vmm/tasks/facts.yml new file mode 100644 index 0000000..7cf4f0c --- /dev/null +++ b/roles/vmm/tasks/facts.yml @@ -0,0 +1,14 @@ +- name: generate lladdr variable for virtual machines + set_fact: + tmp_vms: > + {{ tmp_vms | default([]) + [ item | combine({ + 'lladdr': item.name + | hash('sha1') + | truncate(12, True, '') + | ansible.netcommon.hwaddr('unix') + }) ] }} + loop: "{{ __vms }}" + +- name: save variables + set_fact: + __vms: "{{ tmp_vms }}" diff --git a/roles/vmm/tasks/iso.yml b/roles/vmm/tasks/iso.yml new file mode 100644 index 0000000..0811ac1 --- /dev/null +++ b/roles/vmm/tasks/iso.yml @@ -0,0 +1,7 @@ +- name: download latest iso files + get_url: + url: "{{ item.url }}" + dest: "{{ vmm_iso_dir }}/{{ item.name }}-latest.iso" + checksum: "{{ item.checksum }}" + tags: task_iso + loop: "{{ vmm_iso }}" diff --git a/roles/vmm/tasks/main.yml b/roles/vmm/tasks/main.yml new file mode 100644 index 0000000..3c5a462 --- /dev/null +++ b/roles/vmm/tasks/main.yml @@ -0,0 +1,69 @@ +- name: create vmm directories + file: + path: "{{ item }}" + owner: 0 + group: 0 + mode: 0770 + state: directory + loop: + - "{{ vmm_root_dir }}" + - "{{ vmm_iso_dir }}" + - "{{ vmm_disk_dir }}" + +- name: include facts generation + include_tasks: facts.yml + +- name: include iso management + include_tasks: iso.yml + tags: task_iso + +- name: include disk management + include_tasks: disk.yml + tags: task_disk + +- name: start ip forwarding + command: sysctl net.inet.ip{{ item }}.forwarding=1 + loop: "{{ vmm_network_forwarded_ips }}" + +- name: enable ip forwarding + lineinfile: + path: /etc/sysctl.conf + regexp: "^net.inet.ip{{ item }}.forwarding=" + line: "net.inet.ip{{ item }}.forwarding=1" + owner: 0 + group: 0 + mode: 0640 + create: true + loop: "{{ vmm_network_forwarded_ips }}" + +- name: create network switch + lineinfile: + path: "/etc/hostname.{{ vmm_network_switch.interface }}" + regexp: &network_line "add {{ ansible_default_ipv4.interface }}" + line: *network_line + owner: 0 + group: 0 + mode: 0640 + create: true + +- name: start network switch + command: "sh /etc/netstart {{ vmm_network_switch.interface }}" + +- name: generate vmm configuration + template: + src: vm.conf.j2 + dest: "{{ vmm_configuration_file }}" + owner: 0 + group: 0 + mode: 0640 + register: result + +- name: lint vmm configuration + command: "vmd -nf {{ vmm_configuration_file }}" + when: result.changed + +- name: restart and enable vmd + service: + name: vmd + state: restarted + enabled: true -- cgit v1.2.3