├── README.md ├── delete-vm.yml ├── get-vm.yml └── create-vm.yml /README.md: -------------------------------------------------------------------------------- 1 | # scripts 2 | Scripts and Ansible playbooks for various automations 3 | -------------------------------------------------------------------------------- /delete-vm.yml: -------------------------------------------------------------------------------- 1 | - name: Delete a VM 2 | hosts: '{{ host }}' 3 | vars_prompt: 4 | - name: host 5 | prompt: VM Host 6 | private: no 7 | default: localhost 8 | - name: name 9 | prompt: "Name of the VM to be deleted" 10 | private: no 11 | 12 | tasks: 13 | - name: Destroy domain 14 | command: virsh destroy {{ name }} 15 | ignore_errors: yes 16 | 17 | - name: Undefine domain 18 | command: virsh undefine {{ name }} 19 | ignore_errors: yes 20 | -------------------------------------------------------------------------------- /get-vm.yml: -------------------------------------------------------------------------------- 1 | - name: Get VM details 2 | hosts: '{{ host }}' 3 | vars_prompt: 4 | - name: host 5 | prompt: VM Host 6 | private: no 7 | default: localhost 8 | - name: name 9 | prompt: Name of the VM 10 | private: no 11 | 12 | tasks: 13 | - name: Get MAC address 14 | shell: virsh domiflist {{ name }} | awk '/bridge/{print $5}' 15 | register: mac 16 | 17 | - name: Get IP address 18 | shell: virsh net-dhcp-leases default | awk '/{{ mac.stdout }}/{print $5}' | awk -F/ '{print $1}' 19 | register: ip 20 | 21 | - debug: 22 | var: ip.stdout 23 | -------------------------------------------------------------------------------- /create-vm.yml: -------------------------------------------------------------------------------- 1 | - name: Create a VM using a cloud image 2 | hosts: '{{ host }}' 3 | vars: 4 | base_image: /home/sumit/images/base/focal-server-cloudimg-amd64.img 5 | images_dir: /home/sumit/images/{{ name }} 6 | os: ubuntu20.04 7 | vars_prompt: 8 | - name: host 9 | prompt: VM Host 10 | private: no 11 | default: localhost 12 | - name: name 13 | prompt: Name of the VM 14 | private: no 15 | - name: image_capacity 16 | prompt: Disk capacity 17 | private: no 18 | default: 10G 19 | - name: ram 20 | prompt: "RAM" 21 | private: no 22 | default: 2048 23 | - name: vcpus 24 | prompt: Num. of CPUs 25 | private: no 26 | default: 1 27 | 28 | tasks: 29 | - name: Create images directory 30 | file: 31 | path: '{{ images_dir }}' 32 | state: directory 33 | 34 | - name: Create VM image from base image 35 | command: qemu-img create -b {{ base_image }} -f qcow2 -F qcow2 {{ images_dir }}/{{ name }}.img {{ image_capacity }} 36 | 37 | - name: Create user-data 38 | template: 39 | src: user-data.j2 40 | dest: '{{ images_dir }}/user-data' 41 | 42 | - name: Create meta-data 43 | template: 44 | src: meta-data.j2 45 | dest: '{{ images_dir }}/meta-data' 46 | 47 | - name: Create cloud-init configuration image 48 | command: genisoimage -output {{ images_dir }}/{{ name }}-cidata.iso -V cidata -r -J {{ images_dir }}/user-data {{ images_dir }}/meta-data 49 | become: yes 50 | 51 | - name: Create the VM 52 | command: virt-install --name={{ name }} --ram={{ ram }} --vcpus={{ vcpus }} --import --disk path={{ images_dir }}/{{ name }}.img,format=qcow2 --disk path={{ images_dir }}/{{ name }}-cidata.iso,device=cdrom --os-variant {{ os }} --network bridge=virbr0,model=virtio --graphics vnc,listen=0.0.0.0 --noautoconsole 53 | --------------------------------------------------------------------------------