{"id":466,"date":"2022-03-10T22:42:04","date_gmt":"2022-03-10T22:42:04","guid":{"rendered":"https:\/\/certcent.io\/?p=466"},"modified":"2022-03-10T23:00:14","modified_gmt":"2022-03-10T23:00:14","slug":"quickly-create-multiple-clones-of-esxi-vms-without-requiring-vcenter","status":"publish","type":"post","link":"https:\/\/certcent.io\/index.php\/2022\/03\/10\/quickly-create-multiple-clones-of-esxi-vms-without-requiring-vcenter\/","title":{"rendered":"Quickly create multiple clones of ESXI vms without requiring VCenter."},"content":{"rendered":"\n<p>There is absolutely nothing original about this post and the credit goes to the experts that created these .yaml files for cloning VMs without Vcenter.  <\/p>\n\n\n\n<p>I setup many VMs for testing on my old Dell laptop with 24GB of memory.  They are all running from an SD card which is ironic.  The Ansible host (find the article to install Ansible on Centos 7) is running Centos 7 with the prerequisites: <\/p>\n\n\n\n<p>yum install python3-pyvmomi<br>yum install python2-pyvmomi<\/p>\n\n\n\n<p>ansible-galaxy collection install community.vmware<\/p>\n\n\n\n<p>Here are the working yaml files: <\/p>\n\n\n\n<p>inventory.yml: <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>all:<br>hosts:<br>localhost.my.local<br>ansible_host: 10.0.0.110<br>vars:<br>ansible_user: root<br>ansible_password: mypassword<br><br>clone_vm_without_vcenter_sample.yml<\/p>\n\n\n<p>&#8212;<br \/>\n&#8211; name: Sample Playbook for cloning a vm  on ESXi only environment without vCenter<br \/>\n  hosts: all<br \/>\n  gather_facts: false<br \/>\n  vars:<br \/>\n    clone_vms:  # is new vm list for you want<br \/>\n      &#8211; name: clone_vm04<br \/>\n        disk_type: thin<br \/>\n      &#8211; name: clone_vm05<br \/>\n        disk_type: thin<br \/>\n      &#8211; name: clone_vm06<br \/>\n        disk_type: thin<br \/>\n      &#8211; name: clone_vm07<br \/>\n        disk_type: thin<br \/>\n      &#8211; name: clone_vm08<br \/>\n        disk_type: thin<br \/>\n      &#8211; name: clone_vm09<br \/>\n        disk_type: thin<br \/>\n    datastore_name: VM3  # is datastore name to be deployed of VM<br \/>\n    template: test_vm2  # is template vm name from you want to use<br \/>\n  tasks:<br \/>\n    &#8211; name: Enable SSH Service of ESXi host<br \/>\n      community.vmware.vmware_host_service_manager:<br \/>\n        hostname: &#8220;{{ ansible_host }}&#8221;<br \/>\n        username: &#8220;{{ ansible_user }}&#8221;<br \/>\n        password: &#8220;{{ ansible_password }}&#8221;<br \/>\n        validate_certs: false<br \/>\n        esxi_hostname: &#8220;{{ inventory_hostname }}&#8221;<br \/>\n        service_name: TSM-SSH<br \/>\n        state: present<br \/>\n      delegate_to: localhost<\/p>\n<p>    &#8211; name: Gather datastores information<br \/>\n      community.vmware.vmware_datastore_info:<br \/>\n        hostname: &#8220;{{ ansible_host }}&#8221;<br \/>\n        username: &#8220;{{ ansible_user }}&#8221;<br \/>\n        password: &#8220;{{ ansible_password }}&#8221;<br \/>\n        validate_certs: false<br \/>\n        datacenter: ha-datacenter<br \/>\n      delegate_to: localhost<br \/>\n      register: datastore_result<\/p>\n<p>    &#8211; name: Set datastore_path variable<br \/>\n      set_fact:<br \/>\n        datastore_path: &#8220;{{ item.url }}&#8221;<br \/>\n      loop: &#8220;{{ datastore_result.datastores }}&#8221;<br \/>\n      when:<br \/>\n        &#8211; item.name == datastore_name<\/p>\n<p>    &#8211; when:<br \/>\n        &#8211; datastore_path is defined<br \/>\n      block:<br \/>\n        &#8211; name: Create a directory to store virtual machines<br \/>\n          file:<br \/>\n            path: &#8220;{{ datastore_path }}\/{{ item.name }}&#8221;<br \/>\n            mode: 0755<br \/>\n            state: directory<br \/>\n          loop: &#8220;{{ clone_vms }}&#8221;<\/p>\n<p>        &#8211; name: Copy vmdk file(Multiple file support)<br \/>\n          shell: &gt;-<br \/>\n                 for vmdk in $(find {{ datastore_path }}\/{{ template }} -name &#8220;*.vmdk&#8221; | grep -v flat | awk -F \/ &#8216;{print $(NF)}&#8217;) ; do<br \/>\n                     rename_vmdk=`echo $vmdk | sed -e &#8220;s\/{{ template }}\\(.*\\)\/{{ item.name }}\\1\/g&#8221;`<br \/>\n                     vmkfstools -i {{ datastore_path }}\/{{ template }}\/$vmdk -d {{ item.disk_type }} {{ datastore_path }}\/{{ item.name }}\/$rename_vmdk<br \/>\n                 done<br \/>\n          loop: &#8220;{{ clone_vms }}&#8221;<\/p>\n<p>        &#8211; name: Copy vmx file<br \/>\n          copy:<br \/>\n            src: &#8220;{{ datastore_path }}\/{{ template }}\/{{ template }}.vmx&#8221;<br \/>\n            dest: &#8220;{{ datastore_path }}\/{{ item.name }}\/{{ item.name }}.vmx&#8221;<br \/>\n            mode: 0644<br \/>\n            remote_src: true<br \/>\n          loop: &#8220;{{ clone_vms }}&#8221;<\/p>\n<p>        &#8211; name: Replace vmx file parameter<br \/>\n          replace:<br \/>\n            path: &#8220;{{ datastore_path }}\/{{ item.name }}\/{{ item.name }}.vmx&#8221;<br \/>\n            regexp: &#8220;{{ template }}(\\\\.vmdk|\\\\.nvram|\\&#8221;$)&#8221;<br \/>\n            replace: &#8220;{{ item.name }}\\\\1&#8221;<br \/>\n          loop: &#8220;{{ clone_vms }}&#8221;<\/p>\n<p>        &#8211; name: Register VM to inventory<br \/>\n          community.vmware.vmware_guest_register_operation:<br \/>\n            hostname: &#8220;{{ ansible_host }}&#8221;<br \/>\n            username: &#8220;{{ ansible_user }}&#8221;<br \/>\n            password: &#8220;{{ ansible_password }}&#8221;<br \/>\n            validate_certs: false<br \/>\n            folder: &#8216;\/vm&#8217;<br \/>\n            esxi_hostname: &#8220;{{ inventory_hostname }}&#8221;<br \/>\n            name: &#8220;{{ item.name }}&#8221;<br \/>\n            path: &#8220;[{{ datastore_name }}] {{ item.name }}\/{{ item.name }}.vmx&#8221;<br \/>\n            state: present<br \/>\n          delegate_to: localhost<br \/>\n          loop: &#8220;{{ clone_vms }}&#8221;<\/p>\n\n\n\n<p>Once these are updated with your config, you can image the VM called: test_vm2 <br>My datastore is called VM3.<br>Also, remember to ssh into the ESXI to create the SSH signature finger print on the Cent OS.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is absolutely nothing original about this post and the credit goes to the experts that created these .yaml files for cloning VMs without Vcenter. I setup many VMs for testing on my old Dell laptop with 24GB of memory. They are all running from an SD card which is ironic. The Ansible host (find&hellip; <a class=\"more-link\" href=\"https:\/\/certcent.io\/index.php\/2022\/03\/10\/quickly-create-multiple-clones-of-esxi-vms-without-requiring-vcenter\/\">Continue reading <span class=\"screen-reader-text\">Quickly create multiple clones of ESXI vms without requiring VCenter.<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/certcent.io\/index.php\/wp-json\/wp\/v2\/posts\/466"}],"collection":[{"href":"https:\/\/certcent.io\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/certcent.io\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/certcent.io\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/certcent.io\/index.php\/wp-json\/wp\/v2\/comments?post=466"}],"version-history":[{"count":2,"href":"https:\/\/certcent.io\/index.php\/wp-json\/wp\/v2\/posts\/466\/revisions"}],"predecessor-version":[{"id":468,"href":"https:\/\/certcent.io\/index.php\/wp-json\/wp\/v2\/posts\/466\/revisions\/468"}],"wp:attachment":[{"href":"https:\/\/certcent.io\/index.php\/wp-json\/wp\/v2\/media?parent=466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/certcent.io\/index.php\/wp-json\/wp\/v2\/categories?post=466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/certcent.io\/index.php\/wp-json\/wp\/v2\/tags?post=466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}