X-Git-Url: https://git.realraum.at/?p=noc.git;a=blobdiff_plain;f=ansible%2Froles%2Fvm%2Finstall%2Ftasks%2Fmain.yml;h=a883758c2b58ac3571ed15d9ea22a81e7f62a72a;hp=aaa881e3af879ff3a0a3464b4d23c0da83158100;hb=47ef375e1d90888299fef2a60c5cd12d3c388a72;hpb=120b01b2cf3ad57babbf7cf329d4ed587405ccc6 diff --git a/ansible/roles/vm/install/tasks/main.yml b/ansible/roles/vm/install/tasks/main.yml index aaa881e..a883758 100644 --- a/ansible/roles/vm/install/tasks/main.yml +++ b/ansible/roles/vm/install/tasks/main.yml @@ -1,131 +1,58 @@ --- -- block: - - name: Make a temporary directory - command: mktemp -d - register: tmpdir - - - set_fact: - tmpdir: "{{ tmpdir.stdout }}" - initramfs: "{{ vm_host.installer.preseed_path }}/vm-{{ vmname }}-{{ vmdistro }}-{{ vmdistcodename }}.initrd.gz" - - - name: Copy initramfs into position - copy: - remote_src: yes - src: "{{ vm_host.installer.path }}/{{ vmdistro }}-{{ vmdistcodename }}/{{ hostvars[vmname].vm_install_cooked.arch | default('amd64') }}/initrd.gz" - dest: "{{ initramfs }}" - - - name: generate preseed file - template: - src: "preseed_{{ vmdistro }}-{{ vmdistcodename }}.cfg.j2" - dest: "{{ tmpdir }}/preseed.cfg" - - - name: generate authorized_keys file - authorized_key: - user: root - path: "{{ tmpdir }}/authorized_keys" - key: "{{ hostvars[vmname].ssh_keys | default(noc_ssh_keys) | join('\n') }}" - - - name: Inject files into initramfs - shell: cpio -H newc -o | gzip -9 >> {{ initramfs }} - args: - chdir: "{{ tmpdir }}" - stdin: | - preseed.cfg - authorized_keys - - always: - - name: Delete temporary directory - file: - path: "{{ tmpdir }}" - state: absent - - name: create disks for vm - with_dict: "{{ hostvars[vmname].vm_install_cooked.disks.virtio | default({}) | combine(hostvars[vmname].vm_install_cooked.disks.scsi | default({})) }}" + with_dict: "{{ hostvars[hostname].install_cooked.disks.virtio | default({}) | combine(hostvars[hostname].install_cooked.disks.scsi | default({})) }}" lvol: vg: "{{ item.value.vg }}" lv: "{{ item.value.lv }}" size: "{{ item.value.size }}" -- name: check if vm already exists - virt: - name: "{{ vmname }}" - command: info - register: vmhost_info - block: - - name: destroy exisiting vm - virt: - name: "{{ vmname }}" - state: destroyed + - name: create a temporary workdir + tempfile: + state: directory + register: tmpdir - - name: wait for vm to be destroyed + - import_role: + name: preseed + vars: + ssh_users_root: "{{ hostvars[hostname].ssh_users_root }}" + preseed_tmpdir: "{{ tmpdir.path }}" + preseed_force_net_ifnames_policy: path + install_interface: enp1s1 + + - name: Make preseed workdir readable by qemu + acl: + path: "{{ tmpdir.path }}" + state: present + entity: libvirt-qemu + etype: user + permissions: rx + + - import_role: + name: vm/define + vars: + vm_define_installer: yes + preseed_tmpdir: "{{ tmpdir.path }}" + + - debug: + msg: "you can check on the status of the installer running this command 'virsh console {{ hostname }}' on host {{ inventory_hostname }}." + + - name: wait for installer to finish or crash wait_for_virt: - name: "{{ vmname }}" + name: "{{ hostname }}" states: shutdown,crashed - timeout: 5 - - - name: undefining exisiting vm - virt: - name: "{{ vmname }}" - command: undefine + timeout: 900 + register: installer_result + failed_when: installer_result.failed or installer_result.state == "crashed" - when: vmname in vmhost_info - -- name: enable installer in VM config - set_fact: - run_installer: True - -- name: define new installer vm - virt: - name: "{{ vmname }}" - command: define - xml: "{{ lookup('template', 'libvirt-domain.xml.j2') }}" - -- name: start vm - virt: - name: "{{ vmname }}" - state: running - -- name: wait for installer to start - wait_for_virt: - name: "{{ vmname }}" - states: running - timeout: 10 - -- debug: - msg: "you can check on the status of the installer running this command 'virsh console {{ vmname }}' on host {{ inventory_hostname }}." - -- name: wait for installer to finish or crash - wait_for_virt: - name: "{{ vmname }}" - states: shutdown,crashed - timeout: 900 - register: installer_result - failed_when: installer_result.failed or installer_result.state == "crashed" - -- name: undefining installer vm - virt: - name: "{{ vmname }}" - command: undefine - -- name: disable installer in VM config - set_fact: - run_installer: False - -- name: define new production vm - virt: - name: "{{ vmname }}" - command: define - xml: "{{ lookup('template', 'libvirt-domain.xml.j2') }}" - -- name: start vm - virt: - name: "{{ vmname }}" - state: running + always: + - name: cleanup temporary workdir + file: + path: "{{ tmpdir.path }}" + state: absent -- name: mark vm as autostarted - virt: - name: "{{ vmname }}" - autostart: "{{ hostvars[vmname].vm_install_cooked.autostart }}" - command: info ## virt module needs either command or state - when: hostvars[vmname].vm_install_cooked.autostart is defined +- import_role: + name: vm/define + vars: + vm_define_installer: no