Score:1

count.index for dictionary in Ansible

cn flag

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 }}"
in flag
note: `with_dict` is deprecated. [Use `loop` instead](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-dict)
Alien Life Form avatar
ru flag
@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_<lookup> - that syntax will still be valid for the foreseeable future.")
Score:1
in flag

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
ranji avatar
cn flag
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)
in flag
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.
ranji avatar
cn flag
Yes they have same config and same qcow2 image being used for all 100+ vm's
in flag
In your example the configuration varies (different memory)
ranji avatar
cn flag
Sorry that was a mistake from my end. I mentioned diff mem. But all the vms are going to be same config
in flag
I modified my answer with a different solution that seems more fitting.
ranji avatar
cn flag
I have edited the ansible task in the question section. Will inventory help me on this case ?
in flag
Yes, you can use it for that. Delegate the tasks to the target machine.
ranji avatar
cn flag
Thanks alot Gerald, with inventory it works !
ranji avatar
cn flag
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/128455/discussion-between-shiva-ranjini-and-gerald-schneider).
ranji avatar
cn flag
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
in flag
Please ask a new question for that. Include verbose output. Check on the target host for hanging processes.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.