From 239ababf1d19e83b8d67f4448569cbdef6d643ed Mon Sep 17 00:00:00 2001 From: michael Date: Sun, 30 Oct 2022 21:11:14 +1300 Subject: [PATCH] finalise vm_deploy role --- .../inventory/host_vars/kube01/kube01.yaml | 3 +- .../inventory/host_vars/kube02/kube02.yaml | 4 +- .../inventory/host_vars/kube03/kube03.yaml | 3 +- ansible/roles/vm_deploy/tasks/main.yml | 138 +++++++++--------- 4 files changed, 80 insertions(+), 68 deletions(-) diff --git a/ansible/inventory/host_vars/kube01/kube01.yaml b/ansible/inventory/host_vars/kube01/kube01.yaml index 9ddf8a1..4f6ec83 100644 --- a/ansible/inventory/host_vars/kube01/kube01.yaml +++ b/ansible/inventory/host_vars/kube01/kube01.yaml @@ -1,2 +1,3 @@ --- -ansible_host: kube01.balsillie.net \ No newline at end of file +ansible_host: kube01.balsillie.net +ssh_public_key_string: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGtk+mk1+J3sZ3CA/yS7XV2wH913IdJj0gznmb/nI2nV ladmin@kube01.balsillie.net diff --git a/ansible/inventory/host_vars/kube02/kube02.yaml b/ansible/inventory/host_vars/kube02/kube02.yaml index e3672bc..5203b25 100644 --- a/ansible/inventory/host_vars/kube02/kube02.yaml +++ b/ansible/inventory/host_vars/kube02/kube02.yaml @@ -1,2 +1,4 @@ --- -ansible_host: kube02.balsillie.net \ No newline at end of file +ansible_host: kube02.balsillie.net +ssh_public_key_string: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGOfsOJJJ34VT9mHv9moHQAQNTAok8sOr49rVTkIfDn9 ladmin@kube02.balsillie.net + diff --git a/ansible/inventory/host_vars/kube03/kube03.yaml b/ansible/inventory/host_vars/kube03/kube03.yaml index 02a636e..1331366 100644 --- a/ansible/inventory/host_vars/kube03/kube03.yaml +++ b/ansible/inventory/host_vars/kube03/kube03.yaml @@ -1,2 +1,3 @@ --- -ansible_host: kube03.balsillie.net \ No newline at end of file +ansible_host: kube03.balsillie.net +ssh_public_key_string: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINFqYq3CcxziLbWfp/0VpO5uD/HyjiKTXD8t/dAS01Oq ladmin@kube00.balsillie.net diff --git a/ansible/roles/vm_deploy/tasks/main.yml b/ansible/roles/vm_deploy/tasks/main.yml index dcdceb5..35acfba 100644 --- a/ansible/roles/vm_deploy/tasks/main.yml +++ b/ansible/roles/vm_deploy/tasks/main.yml @@ -9,73 +9,81 @@ ansible.builtin.set_fact: vm_name: "{{ vm_name_prefix }}{{ vm_number }}" - - 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 - creates: "{{ root_vhd_pool_dir }}/{{ vm_name }}_vda.qcow2" - register: root_vhd_created - -# TODO check this template copy - - 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: -# TODO check virt customize command -# TODO select host vars for vm_number for password and ssh string - cmd: | - virt-customize -a {{ root_vhd_pool_dir }}/{{ vm_name }}_vda.qcow2 \ - --format qcow2 \ - --hostname {{ vm_name }}.{{ vm_domain }} \ - --copy-in /tmp/eno1_{{ vm_name }}.network:/etc/systemd/network/10-eno1.network \ - --append-line "/etc/hosts:127.0.1.1 {{ vm_name }}.{{ vm_domain }} {{ vm_name }}" - --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_data'] }}" - - - name: create container storage vhd - ansible.builtin.shell: - cmd: | - qemu-img create -f qcow2 {{ containers_vhd_pool_dir }}/{{ vm_name }}_vdb.qcow2 64G - creates: "{{ containers_vhd_pool_dir }}/{{ vm_name }}_vdb.qcow2" - - - name: create nvme data storage vhd - ansible.builtin.shell: - cmd: | - qemu-img create -f qcow2 {{ data_nvme_vhd_pool_dir }}/{{ vm_name }}_vdc.qcow2 64G - creates: "{{ data_nvme_vhd_pool_dir }}/{{ vm_name }}_vdc.qcow2" - - - name: create hdd data storage vhd - ansible.builtin.shell: - cmd: | - qemu-img create -f qcow2 {{ data_hdd_vhd_pool_dir }}/{{ vm_name }}_vdd.qcow2 4T - creates: "{{ data_hdd_vhd_pool_dir }}/{{ vm_name }}_vdd.qcow2" - - name: list vms community.libvirt.virt: - command: list + command: list_vms register: vm_list + + - name: debug vm list + ansible.builtin.debug: + var: vm_list.list_vms - - name: define vm - community.libvirt.virt: - command: define - autostart: true - name: "{{ vm_name }}" - xml: "{{ lookup('template', 'vm_template.xml.j2') }}" - # TODO check in list logic - when: not (vm_list contains {{ vm_name }}) + - name: proceed if vm does not exist + when: vm_name not in vm_list.list_vms + block: - - name: start vm - community.libvirt.virt: - state: started - name: "{{ vm_name }}" \ No newline at end of file + - 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 + creates: "{{ root_vhd_pool_dir }}/{{ vm_name }}_vda.qcow2" + 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: | + virt-customize -a {{ root_vhd_pool_dir }}/{{ vm_name }}_vda.qcow2 \ + --format qcow2 \ + --hostname {{ vm_name }}.{{ vm_domain }} \ + --copy-in /tmp/eno1_{{ vm_name }}.network:/etc/systemd/network/10-eno1.network \ + --append-line "/etc/hosts:127.0.1.1 {{ vm_name }}.{{ vm_domain }} {{ vm_name }}" + --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: | + qemu-img create -f qcow2 {{ containers_vhd_pool_dir }}/{{ vm_name }}_vdb.qcow2 64G + creates: "{{ containers_vhd_pool_dir }}/{{ vm_name }}_vdb.qcow2" + + - name: create nvme data storage vhd + ansible.builtin.shell: + cmd: | + qemu-img create -f qcow2 {{ data_nvme_vhd_pool_dir }}/{{ vm_name }}_vdc.qcow2 64G + creates: "{{ data_nvme_vhd_pool_dir }}/{{ vm_name }}_vdc.qcow2" + + - name: create hdd data storage vhd + ansible.builtin.shell: + cmd: | + qemu-img create -f qcow2 {{ data_hdd_vhd_pool_dir }}/{{ vm_name }}_vdd.qcow2 4T + creates: "{{ data_hdd_vhd_pool_dir }}/{{ vm_name }}_vdd.qcow2" + + - name: define vm + community.libvirt.virt: + command: define + autostart: false + name: "{{ vm_name }}" + xml: "{{ lookup('template', 'vm_template.xml.j2') }}" + + - name: start vm + community.libvirt.virt: + state: started + name: "{{ vm_name }}" \ No newline at end of file