1

Is it possible to auto generate the vm10,vm11,vm12 in the below script (as count.index used in terraform) ? I would like to pass/define name "vm" and should be able to deploy 3 vm's with the different names vm10, vm11 and vm12. Please suggest a way, Thanks

---
- hosts: Target                         
  vars:
    machines:                  
      v10:
        mem: 1024
        vcpu: 1
      v11:
        mem: 1024
        vcpu: 1
  tasks:
  - name: img cpy
    copy:
      src: /root/pri.qcow2
      dest: /test/{{ item.key }}.qcow2
      remote_src: yes
    with_dict: "{{ machines }}"
  - name: Import/Load VM
    command: >
             virt-install --name {{ item.key }} --memory {{ item.value.mem }} --vcpus {{ item.value.vcpu }} --disk /test/{{ item.key }}.qcow2,bus=sata --import --os-variant generic --network default --noreboot
    with_dict: "{{ machines }}"
ranji
  • 21
  • 4
  • note: `with_dict` is deprecated. [Use `loop` instead](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-dict) – Gerald Schneider Aug 11 '21 at 12:42
  • @GeraldSchneider: according to https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html with_* is not deprecated ("We have not deprecated the use of with_ - that syntax will still be valid for the foreseeable future.") – Alien Life Form Aug 11 '21 at 13:33

1 Answers1

1

Use an inventory instead of a dict. You want 100 vms?

vms:
  hosts:
    vm[001:100]:
      mem: 1024
      vcpu: 1

This will be interpreted as vm001,vm002,...,vm099,vm100. Delegate the task to create them to localhost, since they don't exist when the task is run. Afterwards you can run the setup module and run tasks directly on the newly created VMs.

The corresponding playbook would look like this:

---
- hosts: vms
  gather_facts: no
  tasks:
  - name: copy qcow image to target path
    copy:
      src: /root/ovms/pri.qcow2
      dest: /root/ovms/test/{{ inventory_hostname }}.qcow2
      remote_src: yes
    delegate_to: target
  - name: Import/Load VM
    command: >
            virt-install --name {{ inventory_hostname }} --memory {{ mem }} --vcpus {{ vcpu }} --disk /root/ovms/test/{{ inventory_hostname }}.qcow2,bus=sata --import --os-variant generic --network default --noreboot
    delegate_to: target
Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79
  • Hi @GeraldSchneider, Thanks for the update ! But my query is that, Now its just vm10, vm11 and vm12 (3 vm's) mentioning them in vars with dict is easier. But incase if I wanted to deploy 100vm's, should I still mention 100 vmname's ? or can we do something for that ? (As in terraform, [link](https://emilwypych.com/2017/10/15/deploying-multiple-vsphere-vms-terraform/?cn-reloaded=1) there is something like count.index+1) – ranji Aug 11 '21 at 10:35
  • If you need to specify a different configuration for every VM there is not much you can do to automate this. Of course it would be easy to just number them when they all have the same configuration. – Gerald Schneider Aug 11 '21 at 10:38
  • Yes they have same config and same qcow2 image being used for all 100+ vm's – ranji Aug 11 '21 at 10:39
  • In your example the configuration varies (different memory) – Gerald Schneider Aug 11 '21 at 10:40
  • Sorry that was a mistake from my end. I mentioned diff mem. But all the vms are going to be same config – ranji Aug 11 '21 at 10:46
  • I modified my answer with a different solution that seems more fitting. – Gerald Schneider Aug 11 '21 at 10:54
  • I have edited the ansible task in the question section. Will inventory help me on this case ? – ranji Aug 11 '21 at 11:58
  • Yes, you can use it for that. Delegate the tasks to the target machine. – Gerald Schneider Aug 11 '21 at 12:40
  • Thanks alot Gerald, with inventory it works ! – ranji Aug 11 '21 at 13:35
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/128455/discussion-between-shiva-ranjini-and-gerald-schneider). – ranji Aug 11 '21 at 13:35
  • Hi Gerald, Ansible playbook does not show any progress after executing the task. I have to manually press ctrl + C to exit. This happens only when I load vm's in running state. If vm's loaded in off state, it shows the progress. TASK [copy qcow image to target path] *************************************************************** changed: [vm-1 -> x.x.x.x] changed: [vm-2 -> x.x.x.x] TASK [Import/Load VM] ********************************************************************************************************* [ERROR]: User interrupted execution – ranji Aug 17 '21 at 06:31
  • Please ask a new question for that. Include verbose output. Check on the target host for hanging processes. – Gerald Schneider Aug 17 '21 at 08:10