Skip to content

Instantly share code, notes, and snippets.

@sky-joker
Last active Oct 24, 2021
Embed
What would you like to do?
This playbook is an example of cloning a new VM with vmwre.vmware_res
---
- name: This playbook is an example of cloning a new VM with vmwre.vmware_res
hosts: localhost
gather_facts: false
vars:
vcenter_hostname: change me to vcenter host or ip
vcenter_username: administrator@vsphere.local
vcenter_password: change me to user password
folder: F0
template_vm: test_vm1
new_vm_name: test_vm3
tasks:
- name: Check a new VM isn't existing in vCenter
vmware.vmware_rest.vcenter_vm_info:
vcenter_hostname: "{{ vcenter_hostname }}"
vcenter_username: "{{ vcenter_username }}"
vcenter_password: "{{ vcenter_password }}"
vcenter_validate_certs: false
filter_names:
- "{{ new_vm_name }}"
register: check_vm_result
- name: Occur a failing if the condition isn't match
fail:
msg: "{{ new_vm_name }} is existing in vCenter"
when: check_vm_result.value | length >= 1
- name: Gather a template vm info
vmware.vmware_rest.vcenter_vm_info:
vcenter_hostname: "{{ vcenter_hostname }}"
vcenter_username: "{{ vcenter_username }}"
vcenter_password: "{{ vcenter_password }}"
vcenter_validate_certs: false
filter_names:
- "{{ template_vm }}"
register: template_info_result
- name: Gather a folder info
vmware.vmware_rest.vcenter_folder_info:
vcenter_hostname: "{{ vcenter_hostname }}"
vcenter_username: "{{ vcenter_username }}"
vcenter_password: "{{ vcenter_password }}"
vcenter_validate_certs: false
filter_names:
- "{{ folder }}"
register: folder_info_result
- name: Start to clone a new VM
vmware.vmware_rest.vcenter_vm:
vcenter_hostname: "{{ vcenter_hostname }}"
vcenter_username: "{{ vcenter_username }}"
vcenter_password: "{{ vcenter_password }}"
vcenter_validate_certs: false
placement:
folder: "{{ folder_info_result.value.0.folder }}"
name: "{{ new_vm_name }}"
source: "{{ template_info_result.value.0.vm }}"
state: clone
register: clone_vm_result
changed_when: clone_vm_result.failed is sameas false
- name: Set clone_task_id variable
set_fact:
clone_task_id: "{{ clone_vm_result.value }}"
- name: Get a vmware api session id for getting a status state
uri:
url: "https://{{ vcenter_hostname }}/rest/com/vmware/cis/session"
user: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: false
force_basic_auth: true
method: POST
headers:
Accept: application/json
register: get_vmware_api_session_id_result
- name: Set vmware_api_session_id variable
set_fact:
vmware_api_session_id: "{{ get_vmware_api_session_id_result.json.value }}"
- name: Wait until the status isn't RUNNING
uri:
url: "https://{{ vcenter_hostname }}/rest/cis/tasks/{{ clone_task_id }}"
validate_certs: false
method: GET
headers:
vmware-api-session-id: "{{ vmware_api_session_id }}"
retries: 60
delay: 5
until: status_result.json.value.status != "RUNNING"
register: status_result
- name: Delete the vmware api session id
uri:
url: "https://{{ vcenter_hostname }}/rest/com/vmware/cis/session"
validate_certs: false
method: DELETE
headers:
vmware-api-session-id: "{{ vmware_api_session_id }}"
register: delete_vmware_api_session_id_result
- name: Occur a failing if the condition isn't match
fail:
msg: "{{ new_vm_name }} clone failed"
when: status_result.json.value.status != "SUCCEEDED"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment