
    Vh
g                         d dl mZmZmZ eZdZdZdZ	 d dl	m
Z
mZ d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZ d d	lmZ  G d
 de      Zd Ze dk(  r e        yy# e$ r Y Mw xY w)    )absolute_importdivisionprint_functionay  
---
module: vmware_vmotion
short_description: Move a virtual machine using vMotion, and/or its vmdks using storage vMotion.
description:
    - Using VMware vCenter, move a virtual machine using vMotion to a different
      host, and/or its vmdks to another datastore using storage vMotion.
author:
- Bede Carroll (@bedecarroll)
- Olivier Boukili (@oboukili)
options:
    vm_name:
      description:
      - Name of the VM to perform a vMotion on.
      - This is required parameter, if O(vm_uuid) is not set.
      aliases: ['vm']
      type: str
    vm_uuid:
      description:
      - UUID of the virtual machine to perform a vMotion operation on.
      - This is a required parameter, if O(vm_name) or O(moid) is not set.
      aliases: ['uuid']
      type: str
    moid:
      description:
      - Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance.
      - This is required if O(vm_name) or O(vm_uuid) is not supplied.
      type: str
    use_instance_uuid:
      description:
      - Whether to use the VMware instance UUID rather than the BIOS UUID.
      default: false
      type: bool
    destination_host:
      description:
      - Name of the destination host the virtual machine should be running on.
      - Version 2.6 onwards, this parameter is not a required parameter, unlike the previous versions.
      aliases: ['destination']
      type: str
    destination_cluster:
      description:
      - Name of the destination cluster the virtual machine should be running on.
      - Only works if drs is enabled for this cluster.
      type: str
    destination_datastore_cluster:
      description:
      - Name of the destination datastore cluster (storage pod) the virtual machine's vmdk should be moved on.
      - Only works if drs is enabled for the cluster the vm is running / should run.
      type: str
    destination_datastore:
      description:
      - Name of the destination datastore the virtual machine's vmdk should be moved on.
      aliases: ['datastore']
      type: str
    destination_datacenter:
      description:
      - Name of the destination datacenter the datastore is located on.
      - Optional, required only when datastores are shared across datacenters.
      type: str
    destination_resourcepool:
      description:
      - Name of the destination resource pool where the virtual machine should be running.
      - Resource pool is required if vmotion is done between hosts which are part of different clusters or datacenters.
      - if not passed, resource_pool object will be retrived from host_obj parent.
      aliases: ['resource_pool']
      type: str
    timeout:
      description:
      - The timeout in seconds. When the timeout is reached, the module will fail.
      type: int
      default: 3600
      version_added: '3.4.0'
extends_documentation_fragment:
- community.vmware.vmware.documentation

a  
- name: Perform vMotion of virtual machine
  community.vmware.vmware_vmotion:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    vm_name: 'vm_name_as_per_vcenter'
    destination_host: 'destination_host_as_per_vcenter'
  delegate_to: localhost

- name: Perform vMotion of virtual machine
  community.vmware.vmware_vmotion:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    moid: vm-42
    destination_host: 'destination_host_as_per_vcenter'
  delegate_to: localhost

- name: Perform vMotion of virtual machine to resource_pool
  community.vmware.vmware_vmotion:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    moid: vm-42
    destination_host: 'destination_host_as_per_vcenter'
    destination_resourcepool: 'destination_resourcepool_as_per_vcenter'
  delegate_to: localhost

- name: Perform storage vMotion of virtual machine
  community.vmware.vmware_vmotion:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    vm_name: 'vm_name_as_per_vcenter'
    destination_datastore: 'destination_datastore_as_per_vcenter'
  delegate_to: localhost

- name: Perform storage vMotion and host vMotion of virtual machine
  community.vmware.vmware_vmotion:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    vm_name: 'vm_name_as_per_vcenter'
    destination_host: 'destination_host_as_per_vcenter'
    destination_datastore: 'destination_datastore_as_per_vcenter'
  delegate_to: localhost

- name: Perform storage vMotion to a Storage Cluster and vMotion to a Cluster of virtual machine
  community.vmware.vmware_vmotion:
    hostname: '{{ vcenter_hostname }}'
    username: '{{ vcenter_username }}'
    password: '{{ vcenter_password }}'
    vm_name: 'vm_name_as_per_vcenter'
    destination_cluster: 'destination_cluster_as_per_vcenter'
    destination_datastore_cluster: 'destination_datastore_cluster_as_per_vcenter'
  delegate_to: localhost
a  
running_host:
    description:
    - List the host the virtual machine is registered to.
    - Only returned if there is asked for a vMotion (Cluster or Host).
    returned: changed or success
    type: str
    sample: 'host1.example.com'
datastore:
    description:
    - List the datastore the virtual machine is on.
    - Only returned if there is asked for a Storage vMotion (Datastore or Datastore Cluster).
    returned: changed or success
    type: str
    sample: 'datastore1'
)vimVmomiSupport)	to_native)AnsibleModule)
PyVmomifind_hostsystem_by_namefind_vm_by_idfind_datastore_by_namefind_resource_pool_by_namefind_datacenter_by_namefind_cluster_by_nameget_all_objswait_for_task	TaskError)base_argument_specc                   0     e Zd Z fdZd Zd Zd Z xZS )VmotionManagerc                    t         t        |   |       d | _        | j                  j                  dd       | _        | j                  j                  dd      | _        | j                  j                  dd       | _        | j                  j                  d      xs d | _	        | j                  j                  dd       | _
        | j                  j                  d      | _        t               }| j                          | j                  G| j                  xs | j                  xs | j                  }| j                  j                  d|z  	       | j                  }d }|t!        | j"                  |
      }|r|}| j                  j                  dd       }| j                  j                  dd       }|r|r| j                  j                  d	       d | _        d | _        d | _        |Gt+        | j"                  |      | _        | j$                  | j                  j                  d|z  	       |t-        | j"                  ||      | _        | j&                  r>g | _        | j&                  j.                  D ]  }| j(                  j1                  |        n| j                  j                  d|z  	       | j                  j                  dd       }	| j                  j                  dd       }
|	r|
r| j                  j                  d	       d | _        d | _        |	t7        | j"                  |	|      | _        |
Yt9        | j"                  t:        j<                  g| j"                  j>                        }|D ]  }|j@                  |
k(  s|| _         | j2                  @| j4                  4| j$                  (| j&                  | j                  j                  d	       g }| j                  jB                  D ]~  }| j$                  r)|| j$                  jB                  vr|j1                  d       | j&                  r*|| j&                  jB                  vr|j1                  d       n|j1                  d        tE        |      r|	|
ddjG                  | j$                  jB                  xs | j&                  jB                  D cg c]  }|j@                   c}      ddjG                  | j                  jB                  D cg c]  }|j@                   c}      d}| j                  j                  |	       d}d}| j                  jH                  jJ                  jL                  jO                  dd      d   jQ                  d d!      jQ                  d"d!      }| j$                  r2| j2                  r%| j2                  jR                  jT                  s| j                  j                  d#|	z  	       | j2                  | j$                  jB                  vra| j                  j                  d$|	d%|d&djG                  | j$                  jB                  D cg c]  }|j@                   c}      d'	       | j                  jV                  j.                  j@                  |k(  r|	| j                  jB                  D cg c]  }|j@                   c}v rd}n| j$                  r| j4                  rtY        | j4                  jZ                        tY        | j$                  jB                        k  sa| j                  j                  d(|
d%|d&djG                  | j$                  jB                  D cg c]  }|j@                   c}      d'	       | j                  jV                  j.                  j@                  |k(  r|| j4                  jZ                  D cg c]  }|j@                   c}v r~d}nz| j&                  rS| j2                  rF| j2                  jR                  jT                  s| j                  j                  d#|	z  	       | j2                  | j&                  jB                  vra| j                  j                  d$|	d)|d&djG                  | j&                  jB                  D cg c]  }|j@                   c}      d'	       | j                  jV                  j.                  j@                  | j(                  D cg c]  }|j@                   c}v rO|	| j                  jB                  D cg c]  }|j@                   c}v rd}n| j&                  r1| j4                  r$tY        | j4                  jZ                        tY        | j&                  jB                        k  sa| j                  j                  d(|
d)|d&djG                  | j&                  jB                  D cg c]  }|j@                   c}      d'	       | j                  jV                  j.                  j@                  | j(                  D cg c]  }|j@                   c}v r|| j4                  jZ                  D cg c]  }|j@                   c}v rd}n| j$                  r| j2                  | j$                  r?| j4                  3| j                  jV                  j.                  j@                  |k(  rd}d}ny| j&                  r| j2                  | j&                  r_| j4                  S| j                  jV                  j.                  j@                  | j(                  D cg c]  }|j@                   c}v rd}d}n| j2                  r| j$                  | j2                  rp| j&                  d| j2                  | j                  jB                  v rd}| j2                  jR                  jT                  s| j                  j                  d#|	z  	       nb| j4                  r| j$                  | j4                  r>| j&                  2|| j4                  jZ                  D cg c]  }|j@                   c}v rd}| j&                  s| j4                  r| j]                          | j                  j                  d*d       }d | _/        |rHta        | j"                  |+      | _/        | j^                  S| j                  j                  d,|z  	       n3|s1| j$                  r%| j$                  jb                  jd                  | _/        |jf                  ra| j$                  r| j$                  j@                  |d-<   | j2                  r| j2                  j@                  |d.<   ||d/<    |jh                  d4i | |r(| jk                         }	 tm        || j                  0       |jr                  jt                  t:        jv                  jx                  jz                  k(  r~|r| j                  j}                          | j$                  r| j$                  j@                  |d-<   | j2                  r| j2                  j@                  |d.<   d|d/<    |jh                  d4i | y d1}|jr                  j~                  |d2|jr                  j~                  z  z  }|j                  |	       y 	 | j$                  r| j$                  j@                  |d-<   | j2                  r| j2                  j@                  |d.<   d|d/<    |jh                  d4i | y c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w # tn        $ r0}| j                  j                  tq        |      	       Y d }~d }~ww xY w# t:        j                  j                  $ r d3|d-<   d3|d.<   Y w xY w)5Nvm_uuiduse_instance_uuidFvm_namemoiddestination_datacentertimeoutz*Failed to find the virtual machine with %smsg)contentdatacenter_namedestination_hostdestination_clusterz?Please only define one: destination_host or destination_cluster)r    hostnamez"Unable to find destination host %s)r    cluster_name
datacenterz%Unable to find destination cluster %sdestination_datastoredestination_datastore_clusterzNPlease only define one: destination_datastore or destination_datastore_cluster)r    datastore_namer!   )folderztUnable to find destination datastore, destination datastore cluster, destination host system or destination cluster.Tz>Destination host system or cluster does not share datastore ['z', 'z'] with source host system ['z'] on which virtual machine is located.  Please specify destination_datastore or destination_datastore_cluster to rectify this problem.    r   [ ]z+Destination datastore %s is not accessible.zDestination datastore z9 provided is not associated with destination host system z#. Please specify datastore value ['z)'] associated with the given host system.zDestination datastore cluster z5 provided is not associated with destination cluster destination_resourcepool)r    resource_pool_namez6Unable to find destination resource pool object for %srunning_host	datastorechanged)r   zGUnable to migrate virtual machine due to an error, please check vCenterz : %sNA )Bsuperr   __init__vmparamsgetr   r   r   r   r   r   dictget_vmmodule	fail_jsonr   r    host_objectcluster_objectcluster_hostsr   r   hostappenddatastore_objectdatastore_cluster_objectr   r   r   
StoragePod
rootFoldernamer3   anyjoinconfigfiles
vmPathNamesplitreplacesummary
accessibleruntimesetchildEntityset_placementresourcepool_objectr   parentresourcePool
check_mode	exit_json
migrate_vmr   r   r   infostateTaskInfoStatesuccessRefreshStorageInfoerrorfaultNoPermission)selfr>   resultvm_iddest_datacenterdatacenter_objectdest_host_namedest_cluster_namerC   dest_datastoredest_datastore_clusterdata_store_clustersdschost_datastore_requiredvm_datastoredsr   storage_vmotion_neededchange_required
vm_ds_namedest_resourcepooltask_object
task_error	__class__s                          s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/vmware/plugins/modules/vmware_vmotion.pyr8   zVmotionManager.__init__   s   nd,V4{{y$7!%1De!L{{y$7KKOOF+3t	&*kkoo6NPT&U#{{y177?LL=DLL=DIIEKK!!&RUZ&Z![ 55 & 7^m n "3 );TB KKOO,A4H/KK!!&g!h"!%6t||@N PD'%%*NQ_*_%`("6t||DUbs#uD""%'" //44 4D&&--d34 %%*QTe*e%f )@$G!%1PRV!W4KK!!&v!w $(,%%$:4<<JXKZ%\D! "-".t||cnn=MVZVbVbVmVm"n* 8885547D18
   (T-J-J-RW[WgWgWotx  uH  uH  uPKK!! 'Y! Z
 #% GG-- 	6LL8H8H8R8R$R'..t4""|4;N;N;X;X'X'..t4'..u5	6 &'^-CH^Hf 28QUQaQaQkQk RrTXTgTgTqTq>t"bgg >t 2u17PTPWPWPaPa=b"bgg=b1ceC KK!!c!* "&WW^^))44::3B1EMMcSUV^^_bdfg
 5 5((00;;%% +=?M+N% O $$D,<,<,F,FF%% HVGUGM{{fjfvfv  gA  gA  TB`bTVT[T[  TB  HC+D% E ww##((N:~dhdkdkduduQv^`RTRYRYQv?v"'$"?"?t44@@ASIYIYIcIcEdd%% H^GUGM{{TXTdTdTnTnTp`bTVT[T[ Tp Hq+r% s ww##((N:zNRNkNkNwNwNyZ\bgg Ny @y"'  T%:%:((00;;%% +=?M+N% O $$D,?,?,I,II%% HVGXGM{{TXTgTgTqTqTs`bTVT[T[ Ts Ht+u% v ww##((4CUCU,V4TYY,VV"9J9J&K2rww&KK"'  T%B%Bt44@@ASI\I\IfIfEgg%% H^GXGM{{TXTgTgTqTqTs`bTVT[T[ Ts Ht+u% v ww##((4CUCU,V4TYY,VVT5R5R5^5^"_r277"__"'4#8#8#@  T%B%B%Jww##((N:"'%*"!!d&;&;&C##(E(E(Mww##((4CUCU,V4TYY,VV"'%*"##(8(8(@%%$*=*=*E$$(9(99"'((00;;%% +=?M+N% O ++0@0@0H--$2E2E2M0M0M0Y0YZ"bggZZ"'$"?"?  !KKOO,FM#' 'A$,,Uf(hD$''/%%*bev*v%w"t'7'7'+'7'7'>'>'K'KD$)-)9)9)>)>~&$$&*&;&;&@&@{# /F9F&v&//+KAk4<<@ %%););)C)CC)GG..0##-1-=-=-B-BF>*((*.*?*?*D*DF;'$(y!   *6*_##))57[%5%5%;%;;;C  S )+##-1-=-=-B-BF>*((*.*?*?*D*DF;' !&F9F&v&Q>t=b. TBQvTpNy$Ts -W&KTs,V"_ -W& [@  A%%)J*?%@@A0 99)) +)-~&&*{#+s   
|;
} }}
9}"}8}}}#6}(}- }2&}7}<~ A
~= 	~:
%~55~:=''&'c                     t         j                  j                  | j                  | j                  | j
                        }| j                  j                  |      }|S )z>
        Migrate virtual machine and return the task.
        )rC   r3   pool)r   r9   RelocateSpecr@   rE   rW   Relocate)rf   relocate_specrx   s      r{   r\   zVmotionManager.migrate_vm  sT     ++1A1A6:6K6K151I1I , K gg&&}5    c                    | j                   W| j                  r| j                  j                  | _         n/| j                  j                  j
                  j                  | _         | j                   j                  j                  j                  s| j                  j                  d       t        j                  j                  | j                  | j                        }| j                  r| j                  g}nd}t        j                  j!                  || j"                  | j                  |      }| j                   j%                  |      }|j&                  s| j                  j                  d       | j                  +|j&                  d   j(                  d   j*                  | _        | j                  6|j&                  d   j(                  d   j,                  j.                  | _        yy)z\
        Get the host from the cluster and/or the datastore from datastore cluster.
        Nz`destination_cluster or destination_storage_cluster is only allowed for clusters with active drs.r   )rC   r3   )storagePodshostsr9   relocateSpecz)No placement recommendation from vSphere.r   )rA   r@   rX   r9   rS   rC   configuration	drsConfigenabledr>   r?   r   r~   rE   rF   clusterPlacementSpecrB   PlaceVmrecommendationsaction
targetHostr   r3   )rf   r   r   placement_spec	placements        r{   rV   zVmotionManager.set_placement  s    &&*&6&6&=&=#&*ggoo&:&:&A&A#""00::BBKK!!v " x ++1A1A6:6K6K , M((889KK22{9=9K9K6:gg@M 3 O ''//?	((KK!!? " A #(88;BB1EPPD  ($-$=$=a$@$G$G$J$W$W$a$aD! )r   c                 J   g }d}| j                   rg| j                  s&t        | j                  | j                  d   d      }n1| j                  r%t        | j                  | j                  d   d      }|g}n| j
                  r| j                  t        j                  dg      }|D ][  }t        |j                        dk7  r|j                  j                  | j
                  k(  s@|j                  |j                          n[ nY| j                  rM t        j                   d	      | j                  | j"                  j$                        }|r|j                  |       t        |      dkD  r)| j&                  j)                  d
| j
                  z         |r|d   | _        yy)z
        Find unique virtual machine either by UUID or Name.
        Returns: virtual machine object if found, else None.

        Nr   uuid)rh   
vm_id_typeinstance_uuidrI   )vim_type
propertiesr,   VirtualMachinez]Multiple virtual machines with same name %s found. Please specify vm_uuid instead of vm_name.r   r   )r   r   r   r    r:   r   get_managed_objects_propertiesr   r   lenpropSetobjrI   rD   r   r   
templateOfsi_stubr>   r?   r9   )rf   vmsvm_objobjectstemp_vm_objects        r{   r=   zVmotionManager.get_vm  sg    <<))&t||4;;y;Q^de''&t||4;;y;Q^mn(C\\993CUCUcibj9kG") ~--.!3!%%**dll:JJ~112 YY>\,,-=>tyy$''--XF

6"s8a<KK!! 'TVZVbVb'c! d !fDG r   )__name__
__module____qualname__r8   r\   rV   r=   __classcell__)rz   s   @r{   r   r      s    C'J!bF r   r   c                     t               } | j                  t        t        dg      t        dg      t        d      t        dd      t        d	g      t        d
g      t        dg      t        d      t        d      t        d      t        dd                   t        | dg dg dgg dg      }t	        |       y )Nr9   )aliasesr   str)typeboolF)r   defaultdestinationresource_poolr3   inti  )r   r   r   r   r"   r0   r'   r   r#   r(   r   T)r"   r'   r#   r(   )r   r   r   )argument_specsupports_check_moderequired_one_ofmutually_exclusive)r   updater<   r	   r   )r   r>   s     r{   mainr     s    &(M$(&*5!">!=/:%)?2C%D"&}"=#'U#3 $% 0*.E*:eT2	
  # q*

 +

F 6r   __main__N)!
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNpyVmomir   r   ImportErroransible.module_utils._textr   ansible.module_utils.basicr	   @ansible_collections.community.vmware.plugins.module_utils.vmwarer
   r   r   r   r   r   r   r   r   r   Hansible_collections.community.vmware.plugins.module_utils._argument_specr   r   r   r   r6   r   r{   <module>r      s    A @KZ9v
"	) 1 4   hSW Sl
B zF M  		s   A" "A*)A*