diff options
author | binary <me@rgoncalves.se> | 2020-12-02 18:36:25 +0100 |
---|---|---|
committer | binary <me@rgoncalves.se> | 2020-12-02 18:36:25 +0100 |
commit | c4edd14b8aaf65d1db86074b0109b89ae609d4b3 (patch) | |
tree | 94e123449ce228ce1bff2dcb701f72956b1ad72c /roles/vmm | |
parent | 8478e90356b6738a13eed3878000d0e112074960 (diff) | |
download | infrastructure-c4edd14b8aaf65d1db86074b0109b89ae609d4b3.tar.gz |
Refactor based on bhyve setup
Diffstat (limited to 'roles/vmm')
-rw-r--r-- | roles/vmm/tasks/disks.yml | 19 | ||||
-rw-r--r-- | roles/vmm/tasks/download_iso.yml | 28 | ||||
-rw-r--r-- | roles/vmm/tasks/generate_disk.yml | 21 | ||||
-rw-r--r-- | roles/vmm/tasks/generate_vmconf.yml | 37 | ||||
-rw-r--r-- | roles/vmm/tasks/init_hypervisor.yml | 13 | ||||
-rw-r--r-- | roles/vmm/tasks/init_vm_alpine.yml | 50 | ||||
-rw-r--r-- | roles/vmm/tasks/isos.yml | 45 | ||||
-rw-r--r-- | roles/vmm/tasks/main.yml | 64 | ||||
-rw-r--r-- | roles/vmm/tasks/set_facts.yml | 9 | ||||
-rw-r--r-- | roles/vmm/templates/vm.conf.j2 | 2 | ||||
-rw-r--r-- | roles/vmm/vars/main.yml | 6 |
11 files changed, 122 insertions, 172 deletions
diff --git a/roles/vmm/tasks/disks.yml b/roles/vmm/tasks/disks.yml new file mode 100644 index 0000000..f70d563 --- /dev/null +++ b/roles/vmm/tasks/disks.yml @@ -0,0 +1,19 @@ + +# disks ~~ roles/vmm/tasks/disks.yml + +--- + +- name: get existing disk file size + stat: + path: "{{ vmm.disk_dir }}/{{ guest.name }}.{{ vmm.disk_format }}" + register: st + +- name: generate vm disk + shell: | + disk="{{ vmm.disk_dir }}/{{ guest.name }}_{{ disk.id }}.{{ vmm.disk_format }}" + rm "${disk}" + vmctl create -s {{ disk.size }} "${disk}" + #when: not st.stat.exists or st.stat.size < 32 + loop: "{{ guest.disks }}" + loop_control: + loop_var: disk diff --git a/roles/vmm/tasks/download_iso.yml b/roles/vmm/tasks/download_iso.yml deleted file mode 100644 index 6f0c5f6..0000000 --- a/roles/vmm/tasks/download_iso.yml +++ /dev/null @@ -1,28 +0,0 @@ - -# vmm ~~ tasks/download_iso.yml -# Download an iso file to hypervisor. -# Required : -# - iso : name of an iso file defined in vars/main.yml - ---- - -- name: Check arguments - fail: - msg: "arguments : iso" - when: iso is not defined - -- include: set_facts.yml - -- name: Get latest iso - get_url: - url: "{{ iso_url }}" - dest: "{{ iso_file }}" - force: "{{ force | default('no') }}" - checksum: "sha256:{{ iso_checksum }}" - -- name: Symlink latest iso - file: - src: "{{ iso_file }}" - dest: "{{ iso_latest }}" - state: link - diff --git a/roles/vmm/tasks/generate_disk.yml b/roles/vmm/tasks/generate_disk.yml deleted file mode 100644 index 1c44471..0000000 --- a/roles/vmm/tasks/generate_disk.yml +++ /dev/null @@ -1,21 +0,0 @@ - -# 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 }}/{{ guest.name }}.{{ vmm.disk_format }}" - register: st - -- name: Generate vm disk for {{ guest.name }} - shell: | - disk="{{ vmm.disk_dir }}/{{ guest.name }}.{{ vmm.disk_format }}" - rm "${disk}" - 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 deleted file mode 100644 index e8f8245..0000000 --- a/roles/vmm/tasks/generate_vmconf.yml +++ /dev/null @@ -1,37 +0,0 @@ - -# vmm ~~ tasks/generate_vmconf.yml -# Generate vm.conf on hypervisor. - ---- - -- name: Start ip forwarding - shell: sysctl net.inet.ip{{ item }}.forwarding=1 - loop: - - "" - - "6" - -- name: Enable ip forwarding - lineinfile: - path: /etc/sysctl.conf - regexp: "^net.inet.ip{{ item }}.forwarding=" - line: "net.inet.ip{{ item }}.forwarding=1" - loop: - - "" - - "6" - -- name: Create network switch - shell: echo "add {{ hypervisor.interface }}" > /etc/hostname.{{ vmm.switch.iface }} - -- name: Start network switch - shell: "sh /etc/netstart {{ vmm.switch.iface }}" - -- name: Generate vmm configuration - template: - src: templates/vm.conf.j2 - dest: "{{ vmm.config_file }}" - -- name: Restart vmd - service: - name: vmd - state: restarted - enabled: true diff --git a/roles/vmm/tasks/init_hypervisor.yml b/roles/vmm/tasks/init_hypervisor.yml deleted file mode 100644 index b206279..0000000 --- a/roles/vmm/tasks/init_hypervisor.yml +++ /dev/null @@ -1,13 +0,0 @@ - -# 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/init_vm_alpine.yml b/roles/vmm/tasks/init_vm_alpine.yml deleted file mode 100644 index 0c66a44..0000000 --- a/roles/vmm/tasks/init_vm_alpine.yml +++ /dev/null @@ -1,50 +0,0 @@ - -# vmm ~~ tasks/init_vm_alpine.yml - ---- - -- set_fact: - iso: "{{ vms | selectattr('name', 'equalto', guest) | map(attribute='iso') | first }}" - -- include: set_facts.yml - -- name: Check for existing drive - stat: - path: "{{ disk_file }}" - register: st_disk - -- fail: - msg: "No empty disk detected ! You need to generated disks via hypervisor playbook" - when: not st_disk.stat.exists - -- fail: - msg: "Existing installation detected ! Manual action on host required" - when: st_disk.stat.size > 500000 - -- include_role: - name: serial - -- name: Copy vm init script - copy: - src: init_vm_alpine.py - dest: /data/python/init_vm_alpine.py - -- name: Stop vm if running - shell: vmctl stop "{{ guest }}" ; vmctl stop vm-tmp - ignore_errors: true - -- name: Start temporary vm - shell: vmctl start -r {{ iso_latest }} -d {{ disk_file }} -n {{ vmm.switch.name }} -m 1G vm-tmp - -- name: Pause 30 seconds for vm boot - pause: - seconds: 30 - -- name: Init vm via script - command: python3 /data/python/init_vm_alpine.py \ - {{ guest }} \ - {{ hostvars[guest].ip.out }} \ - {{ hypervisor.gateway }} \ - {{ hypervisor.mask }} \ - {{ _i.dns[0] }} \ - "{{ lookup('file', inventory_dir + '/files/pubkeys/rgoncalves.pub') }}" diff --git a/roles/vmm/tasks/isos.yml b/roles/vmm/tasks/isos.yml new file mode 100644 index 0000000..946b659 --- /dev/null +++ b/roles/vmm/tasks/isos.yml @@ -0,0 +1,45 @@ + +# vmm isos ~~ roles/vmm/tasks/isos.yml +# download an iso file to hypervisor. + +--- + +- name: Check arguments + fail: + msg: "arguments : iso" + when: iso is not defined + +- name: ensure guest exists + set_fact: + disk_file: "{{ vmm.disk_dir }}/{{ guest }}.{{ vmm.disk_format }}" + when: guest is defined + +- name: generate iso infos 1/2 + set_fact: + iso_latest: "{{ vmm.iso_dir }}/{{ vmm.iso[iso].name }}-latest.iso" + when: iso is defined + +- name: generate iso infos 2/2 + 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 + +- name: Get latest iso + get_url: + url: "{{ iso_url }}" + dest: "{{ iso_file }}" + force: "{{ force | default('no') }}" + checksum: "sha256:{{ iso_checksum }}" + +- name: Symlink latest iso + file: + src: "{{ iso_file }}" + dest: "{{ iso_latest }}" + state: link + diff --git a/roles/vmm/tasks/main.yml b/roles/vmm/tasks/main.yml index 81ddafc..3972e63 100644 --- a/roles/vmm/tasks/main.yml +++ b/roles/vmm/tasks/main.yml @@ -1,26 +1,60 @@ -# vmm_deploy ~~ tasks/main.yml +# vmm ~~ roles/vmm/tasks/main.yml +# vmm generation for hypervisor --- -- name: Check that iso directory exists +- name: ensure existence of vmm directories file: - path: "{{ vmm.iso_dir }}" + path: "{{ item }}" + owner: "{{ user_root }}" + group: "{{ group_root }}" + mode: 0770 state: directory - recurse: true + loop: + - "{{ vmm.dir }}" + - "{{ vmm.iso_dir }}" + - "{{ vmm.disk_dir }}" -- name: Check that disk directory exists - file: - path: "{{ vmm.disk_dir }}" - state: directory - recurse: true - -- name: Download all iso files - include: download_iso.yml iso={{ item }} +- name: download all iso files + include: isos.yml iso={{ item }} with_items: "{{ vmm.iso }}" -- name: Generate disks for all vms - include: generate_disk.yml guest={{ item }} +- name: generate disks for all vms + include: disks.yml guest={{ item }} with_items: "{{ vms }}" -- include: generate_vmconf.yml +- name: start ip forwarding + shell: sysctl net.inet.ip{{ item }}.forwarding=1 + loop: + - "" + - "6" + +- name: enable ip forwarding + lineinfile: + path: /etc/sysctl.conf + regexp: "^net.inet.ip{{ item }}.forwarding=" + line: "net.inet.ip{{ item }}.forwarding=1" + owner: "{{ user_root }}" + group: "{{ group_root }}" + create: yes + loop: + - "" + - "6" + +- name: create network switch + shell: echo "add {{ hypervisor.interface }}" > /etc/hostname.{{ vmm.switch.iface }} + +- name: start network switch + shell: "sh /etc/netstart {{ vmm.switch.iface }}" + +- name: generate vmm configuration + template: + src: templates/vm.conf.j2 + dest: "{{ vmm.config_file }}" + +- name: restart and enable vmd + service: + name: vmd + state: restarted + enabled: true diff --git a/roles/vmm/tasks/set_facts.yml b/roles/vmm/tasks/set_facts.yml index a38a63d..6b6dbfe 100644 --- a/roles/vmm/tasks/set_facts.yml +++ b/roles/vmm/tasks/set_facts.yml @@ -3,15 +3,18 @@ --- -- set_fact: +- name: ensure guest exists + set_facts: disk_file: "{{ vmm.disk_dir }}/{{ guest }}.{{ vmm.disk_format }}" when: guest is defined -- set_fact: +- name: generate iso infos 1/2 + set_fact: iso_latest: "{{ vmm.iso_dir }}/{{ vmm.iso[iso].name }}-latest.iso" when: iso is defined -- set_fact: +- name: generate iso infos 2/2 + 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) \ diff --git a/roles/vmm/templates/vm.conf.j2 b/roles/vmm/templates/vm.conf.j2 index f9fa6f5..ed20468 100644 --- a/roles/vmm/templates/vm.conf.j2 +++ b/roles/vmm/templates/vm.conf.j2 @@ -18,7 +18,7 @@ switch "{{ vmm.switch.name }}" { {% for vm in vms if hostvars[vm.name] is defined %} {% set guest = hostvars[vm.name] %} vm "{{ guest.ansible_host }}" { - {% if vm.enabled and vm.enabled is defined %} + {% if vm.enable is defined and vm.enable %} enable {% else %} disable diff --git a/roles/vmm/vars/main.yml b/roles/vmm/vars/main.yml index 8ad57a0..d8bee40 100644 --- a/roles/vmm/vars/main.yml +++ b/roles/vmm/vars/main.yml @@ -5,9 +5,7 @@ vmm: - user: "vmm" - group: "vmm" - + dir: "/data/vmm" iso_dir: "/data/vmm/iso.d" disk_dir: "/data/vmm/disk.d" disk_format: "qcow2" @@ -18,7 +16,7 @@ vmm: iface: "vether0" ip: "192.168.10.1" mask: "255.255.255.0" - + switch: iface: "bridge0" name: "uplink" |