2022-10-30 04:22:34 -04:00
|
|
|
---
|
|
|
|
- name: set vm_name
|
|
|
|
ansible.builtin.set_fact:
|
|
|
|
vm_name: "{{ vm_name_prefix }}{{ vm_number }}"
|
|
|
|
|
|
|
|
- name: list vms
|
|
|
|
community.libvirt.virt:
|
|
|
|
command: list_vms
|
|
|
|
register: vm_list
|
|
|
|
|
|
|
|
- name: debug vm list
|
|
|
|
ansible.builtin.debug:
|
|
|
|
var: vm_list.list_vms
|
|
|
|
|
|
|
|
- name: proceed if vm does not exist
|
|
|
|
when: vm_name not in vm_list.list_vms
|
|
|
|
block:
|
|
|
|
|
|
|
|
- name: create root vhd from template
|
|
|
|
ansible.builtin.shell:
|
|
|
|
cmd: |
|
|
|
|
qemu-img create \
|
|
|
|
-b {{ root_vhd_pool_dir }}/{{ vhd_template }} \
|
|
|
|
-F qcow2 \
|
|
|
|
-f qcow2 \
|
|
|
|
{{ root_vhd_pool_dir }}/{{ vm_name }}_vda.qcow2
|
2022-10-30 04:26:10 -04:00
|
|
|
creates: "{{ root_vhd_pool_dir }}/{{ vm_name }}_vda.qcow2"
|
2022-10-30 04:22:34 -04:00
|
|
|
register: root_vhd_created
|
|
|
|
|
|
|
|
- name: copy network files to hypervisor host
|
|
|
|
when: root_vhd_created is changed
|
|
|
|
ansible.builtin.template:
|
|
|
|
src: eno1.network.j2
|
|
|
|
dest: /tmp/eno1_{{ vm_name }}.network
|
|
|
|
|
|
|
|
- name: customize root vhd
|
|
|
|
when: root_vhd_created is changed
|
|
|
|
ansible.builtin.shell:
|
|
|
|
cmd: |
|
2022-10-30 04:51:27 -04:00
|
|
|
virt-customize --format qcow2 \
|
|
|
|
-a {{ root_vhd_pool_dir }}/{{ vm_name }}_vda.qcow2 \
|
2022-10-30 04:22:34 -04:00
|
|
|
--hostname {{ vm_name }}.{{ vm_domain }} \
|
2022-10-30 06:48:43 -04:00
|
|
|
--write /etc/hostname:{{ vm_name }} \
|
2022-10-30 05:09:51 -04:00
|
|
|
--upload /tmp/eno1_{{ vm_name }}.network:/etc/systemd/network/10-eno1.network \
|
2022-10-30 05:13:23 -04:00
|
|
|
--append-line "/etc/hosts:127.0.1.1 {{ vm_name }}.{{ vm_domain }} {{ vm_name }}" \
|
2022-10-30 04:22:34 -04:00
|
|
|
--password ladmin:password:{{ hostvars[vm_name]['ansible_become_pass'] }} \
|
|
|
|
--root-password password:{{ hostvars[vm_name]['ansible_root_pass'] }} \
|
|
|
|
--password-crypto sha512 \
|
|
|
|
--ssh-inject "ladmin:string:{{ hostvars[vm_name]['ssh_public_key_string'] }}"
|
|
|
|
|
|
|
|
- name: cleanup network files on hypervisor host
|
|
|
|
ansible.builtin.file:
|
|
|
|
state: absent
|
|
|
|
path: /tmp/eno1_{{ vm_name }}.network
|
|
|
|
|
|
|
|
- name: create container storage vhd
|
|
|
|
ansible.builtin.shell:
|
|
|
|
cmd: |
|
2022-10-30 04:51:27 -04:00
|
|
|
qemu-img create -f qcow2 {{ containers_vhd_pool_dir }}/{{ vm_name }}_vdb.qcow2 {{ containers_vhd_size }}
|
2022-10-30 04:26:10 -04:00
|
|
|
creates: "{{ containers_vhd_pool_dir }}/{{ vm_name }}_vdb.qcow2"
|
2022-10-30 04:22:34 -04:00
|
|
|
|
|
|
|
- name: create nvme data storage vhd
|
|
|
|
ansible.builtin.shell:
|
|
|
|
cmd: |
|
2022-10-30 04:51:27 -04:00
|
|
|
qemu-img create -f qcow2 {{ data_nvme_vhd_pool_dir }}/{{ vm_name }}_vdc.qcow2 {{ data_nvme_vhd_size }}
|
2022-10-30 04:26:10 -04:00
|
|
|
creates: "{{ data_nvme_vhd_pool_dir }}/{{ vm_name }}_vdc.qcow2"
|
2022-10-30 04:22:34 -04:00
|
|
|
|
|
|
|
- name: create hdd data storage vhd
|
|
|
|
ansible.builtin.shell:
|
|
|
|
cmd: |
|
2022-10-30 04:51:27 -04:00
|
|
|
qemu-img create -f qcow2 {{ data_hdd_vhd_pool_dir }}/{{ vm_name }}_vdd.qcow2 {{ data_hdd_vhd_size }}
|
2022-10-30 04:26:10 -04:00
|
|
|
creates: "{{ data_hdd_vhd_pool_dir }}/{{ vm_name }}_vdd.qcow2"
|
2022-10-30 04:22:34 -04:00
|
|
|
|
|
|
|
- name: define vm
|
|
|
|
community.libvirt.virt:
|
|
|
|
command: define
|
|
|
|
autostart: false
|
|
|
|
xml: "{{ lookup('template', 'vm_template.xml.j2') }}"
|
|
|
|
|
|
|
|
- name: start vm
|
|
|
|
community.libvirt.virt:
|
2022-10-30 05:40:13 -04:00
|
|
|
state: running
|
2022-10-30 04:22:34 -04:00
|
|
|
name: "{{ vm_name }}"
|