- name: prepare nested environment hosts: "{{target}}" gather_facts: no become: "{{ become_var | default(false) }}" # we need /dev/nbd0 to be used by one guest only, therefore reducing batch size to a single host # https://docs.ansible.com/projects/ansible/latest/playbook_guide/playbooks_strategies.html#setting-the-number-of-forks serial: 1 tasks: - debug: var=type # assuming user has perms to write in /data/guests/ - name: kvm guest configs delegate_to: "{{kvm_host}}" run_once: false # every single guest needs to be configured template: src: "{{type}}.{{inventory_hostname_short}}.xml" dest: "/data/guests/{{inventory_hostname_short}}/{{inventory_hostname_short}}.xml" - name: un-mounting guests just in case delegate_to: "{{kvm_host}}" run_once: false # every single guest needs to be configured shell: executable: /bin/bash cmd: | set -e tmp=`virsh list --all | grep -E "[[:space:]]+{{inventory_hostname_short}}[[:space:]]+"` || true [[ -n $tmp ]] && echo guest already running && exit 0 unset tmp [[ {{ansible_check_mode}} = True ]] && echo will do during play && exit 0 if [[ {{vdisk_type}} = qcow2 ]]; then img=/data/guests/{{inventory_hostname_short}}/{{inventory_hostname_short}}.qcow2 elif [[ {{vdisk_type}} = zfs ]]; then img=/dev/zvol/smith/{{inventory_hostname_short}}_boot else echo error: unknown vdisk_type exit 1 fi mnt=/data/guests/{{inventory_hostname_short}}/lala #echo checking whether /dev/nbd0p1 already exists #ls -lF /dev/nbd0p1 && exit 1 || true # deal with previously mounted for possibly other guests echo checking for mounted guests just in case mounted_guests=`mount | grep /lala | awk '{print $3}'` for mounted_guest in $mounted_guests; do echo -n UN-MOUNTING $mounted_guest/ ... umount $mounted_guest/ && echo DONE done; unset mounted_guest unset mounted_guests echo disconnecting nbd0 just in case qemu-nbd -d /dev/nbd0 || true check_mode: false register: output - debug: var=output.stdout - debug: var=output.stderr - name: mounting guests delegate_to: "{{kvm_host}}" run_once: false # every single guest needs to be configured shell: executable: /bin/bash cmd: | set -e tmp=`virsh list --all | grep -E "[[:space:]]+{{inventory_hostname_short}}[[:space:]]+"` || true [[ -n $tmp ]] && echo guest already running && exit 0 unset tmp [[ {{ansible_check_mode}} = True ]] && echo will do during play && exit 0 if [[ {{vdisk_type}} = qcow2 ]]; then img=/data/guests/{{inventory_hostname_short}}/{{inventory_hostname_short}}.qcow2 elif [[ {{vdisk_type}} = zfs ]]; then img=/dev/zvol/smith/{{inventory_hostname_short}}_boot else echo error: unknown vdisk_type exit 1 fi mnt=/data/guests/{{inventory_hostname_short}}/lala echo -n preparing /dev/nbd0 ... modprobe nbd qemu-nbd --connect=/dev/nbd0 --format={{vdisk_format}} $img && echo done sleep 1 echo -n /dev/nbd0p1 now should exist ... ls -lF /dev/nbd0p1 >/dev/null && echo done echo -n mounting nbd0p1 ... mkdir -p $mnt/ mount /dev/nbd0p1 $mnt/ && echo done check_mode: false register: output - debug: var=output.stdout - debug: var=output.stderr - name: guest interfaces on debian systems delegate_to: "{{kvm_host}}" run_once: false # every single guest needs to be configured template: src: "{{type}}.interfaces.{{inventory_hostname_short}}" dest: "/data/guests/{{inventory_hostname_short}}/lala/etc/network/interfaces" when: - system == 'debian' - not ansible_check_mode - "'already running' not in output.stdout" - name: inet1 on slackware systems delegate_to: "{{kvm_host}}" run_once: false # every single guest needs to be configured template: src: "{{type}}.inet1.{{inventory_hostname_short}}" dest: "/data/guests/{{inventory_hostname_short}}/lala/etc/rc.d/rc.inet1" when: - system == 'slackware' - not ansible_check_mode - "'already running' not in output.stdout" # assuming user has perms to run virsh - name: un-mount and guests are up delegate_to: "{{kvm_host}}" run_once: false # every single guest needs to be configured shell: executable: /bin/bash cmd: | set -e tmp=`virsh list --all | grep -E "[[:space:]]+{{inventory_hostname_short}}[[:space:]]+"` || true [[ -n $tmp ]] && echo guest already running && exit 0 unset tmp [[ {{ansible_check_mode}} = True ]] && echo will do during play && exit 0 img=/data/guests/{{inventory_hostname_short}}/{{inventory_hostname_short}}.qcow2 mnt=/data/guests/{{inventory_hostname_short}}/lala echo -n un-mounting $mnt/ ... umount $mnt/ && echo done # self-verbose echo disconnecting nbd0 qemu-nbd -d /dev/nbd0 check_mode: false register: output - debug: var=output.stdout - debug: var=output.stderr - name: start guests at boot time delegate_to: "{{kvm_host}}" run_once: true template: src: rc.local.slackware dest: /etc/rc.d/rc.local mode: 0755 when: - start_guests is defined - system == 'slackware' - debug: msg: | ready do proceed: virsh create /data/guests/{{inventory_hostname_short}}/{{inventory_hostname_short}}.xml --console