ansible-test/roles/virt_vm/tasks/create_vm.yml

120 lines
2.8 KiB
YAML

---
# VM PREPARATION
- name: create vm storage
delegate_to: "{{ virt.host }}"
copy:
src: "{{ libvirt.images }}/{{ virt.image.filename }}"
dest: "{{ libvirt.images }}/{{ inventory_hostname }}.qcow2"
force: no
- name: set vm storage size
delegate_to: "{{ virt.host }}"
shell:
cmd: qemu-img resize "{{ libvirt.images }}/{{ inventory_hostname }}.qcow2" "{{ virt.hdd }}"
- name: define vm
delegate_to: "{{ virt.host }}"
virt:
name: "{{ inventory_hostname }}"
xml: '{{ lookup("template", "fedora-34.xml.j2") }}'
command: define
- name: make temp dir for cloud-init (CI)
delegate_to: "{{ virt.host }}"
tempfile:
state: directory
register: cidata
# CLOUD-INIT PREPARATION
- name: create CI-metadata
delegate_to: "{{ virt.host }}"
template:
src: ci-metadata.j2
dest: "{{ cidata.path }}/meta-data"
- name: create CI-userdata
delegate_to: "{{ virt.host }}"
template:
src: ci-userdata.j2
dest: "{{ cidata.path }}/user-data"
- name: create CI-network-config
delegate_to: "{{ virt.host }}"
template:
src: ci-networkconfig.j2
dest: "{{ cidata.path }}/network-config"
- name: create CI ISO
delegate_to: "{{ virt.host }}"
shell:
cmd: genisoimage -output "{{ libvirt.images }}/{{ inventory_hostname }}-cidata.iso" -volid cidata -joliet -rock user-data meta-data network-config
chdir: "{{ cidata.path }}"
# VM INIT
- name: attach CI to vm
delegate_to: "{{ virt.host }}"
shell:
cmd: virsh change-media "{{ inventory_hostname }}" sda --source "{{ libvirt.images }}/{{ inventory_hostname }}-cidata.iso" --insert --config
- name: define vm
delegate_to: "{{ virt.host }}"
virt:
name: "{{ inventory_hostname }}"
state: running
- name: ping VM
delegate_to: "{{ virt.host }}"
shell:
cmd: "ping -c 2 {{ ansible_host }}"
register: pingcmd
retries: 11
delay: 10
until: pingcmd.rc == 0
# CLOUD-INIT CLEANUP
# FEDORA-34: after rebooting the ssh login via key does not work anymore. weird.
# name: shutdown VM
# delegate_to: "{{ virt.host }}"
# virt:
# name: "{{ inventory_hostname }}"
# state: shutdown
# name: wait for shutdown
# delegate_to: "{{ virt.host }}"
# virt:
# name: "{{ inventory_hostname }}"
# command: status
# register: vmstate
# retries: 11
# delay: 10
# until: vmstate.status == "shutdown"
- name: detach CI from vm (live)
delegate_to: "{{ virt.host }}"
shell:
cmd: virsh change-media "{{ inventory_hostname }}" sda --eject
- name: detach CI from vm (config)
delegate_to: "{{ virt.host }}"
shell:
cmd: virsh change-media "{{ inventory_hostname }}" sda --eject --config
- name: delete CI temp dir
delegate_to: "{{ virt.host }}"
file:
path: "{{ cidata.path }}"
state: absent
force: true
- name: delete CI ISO
delegate_to: "{{ virt.host }}"
file:
path: "{{ libvirt.images }}/{{ inventory_hostname }}-cidata.iso"
state: absent