
    Vh8                        d dl mZ dZdZd dlZd dlmZmZ d dlm	Z	 d dl
mZmZ d dlmZ d d	lmZmZmZ d d
lmZ d dlmZ  G d deee      Zy)    )annotationsa	  
author: Unknown (!UNKNOWN)
name: virtualbox
short_description: virtualbox inventory source
description:
  - Get inventory hosts from the local virtualbox installation.
  - Uses a YAML configuration file that ends with virtualbox.(yml|yaml) or vbox.(yml|yaml).
  - The inventory_hostname is always the 'Name' of the virtualbox instance.
  - Groups can be assigned to the VMs using C(VBoxManage). Multiple groups can be assigned by using V(/) as a delimeter.
  - A separate parameter, O(enable_advanced_group_parsing) is exposed to change grouping behaviour. See the parameter documentation for details.
extends_documentation_fragment:
  - constructed
  - inventory_cache
options:
  plugin:
    description: token that ensures this is a source file for the 'virtualbox' plugin
    type: string
    required: true
    choices: ['virtualbox', 'community.general.virtualbox']
  running_only:
    description: toggles showing all vms vs only those currently running
    type: boolean
    default: false
  settings_password_file:
    description: provide a file containing the settings password (equivalent to --settingspwfile)
    type: string
  network_info_path:
    description: property path to query for network information (ansible_host)
    type: string
    default: "/VirtualBox/GuestInfo/Net/0/V4/IP"
  query:
    description: create vars from virtualbox properties
    type: dictionary
    default: {}
  enable_advanced_group_parsing:
    description:
      - The default group parsing rule (when this setting is set to V(false)) is to split the VirtualBox VM's group based on the V(/) character and
        assign the resulting list elements as an Ansible Group.
      - Setting O(enable_advanced_group_parsing=true) changes this behaviour to match VirtualBox's interpretation of groups according to
        U(https://www.virtualbox.org/manual/UserManual.html#gui-vmgroups).
        Groups are now split using the V(,) character, and the V(/) character indicates nested groups.
      - When enabled, a VM that's been configured using V(VBoxManage modifyvm "vm01" --groups "/TestGroup/TestGroup2,/TestGroup3") will result in
        the group C(TestGroup2) being a child group of C(TestGroup); and
        the VM being a part of C(TestGroup2) and C(TestGroup3).
    default: false
    type: bool
    version_added: 9.2.0
a  
---
# file must be named vbox.yaml or vbox.yml
plugin: community.general.virtualbox
settings_password_file: /etc/virtualbox/secrets
query:
  logged_in_users: /VirtualBox/GuestInfo/OS/LoggedInUsersList
compose:
  ansible_connection: ('indows' in vbox_Guest_OS)|ternary('winrm', 'ssh')

---
# add hosts (all match with minishift vm) to the group container if any of the vms are in ansible_inventory'
plugin: community.general.virtualbox
groups:
  container: "'minis' in (inventory_hostname)"
N)PopenPIPE)AnsibleParserError)to_bytesto_text)MutableMapping)BaseInventoryPluginConstructable	Cacheable)get_bin_path)make_unsafec                  l     e Zd ZdZdZdZ fdZd Zd Zd Z	ddZ
d	 Zd
 Zd Z fdZd fd	Z xZS )InventoryModulez; Host inventory parser for ansible using local virtualbox. zcommunity.general.virtualbox
VBoxManagec                8    d | _         t        t        |           y )N)
_vbox_pathsuperr   __init__)self	__class__s    r/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/inventory/virtualbox.pyr   zInventoryModule.__init__]   s    ot-/    c                8   d }	 | j                   ddt        |d      t        |d      g}t        |t              }t	        |j
                  j                         d      }d|v r%|j                  dd      \  }}|j                         }|S # t        $ r Y |S w xY w)	Ns   guestpropertys   getsurrogate_or_strict)errorsstdoutValue:   )
r   r   r   r   r   r   readsplitstrip	Exception)	r   hostproperty_pathretcmdxipinfoaips	            r   _query_vbox_dataz InventoryModule._query_vbox_dataa   s    
	??$4fD)>?M2GHJC c$'AQXX]]_5JKF& S!,2hhj 
  	
	s   BB 	BBc                   |D ]  }| j                  d      }|r2t        |t              r"|D ]  }| j                  |||         ||   |<    | j                  d      }| j	                  | j                  d      ||   ||       ||   D ]%  }| j
                  j                  ||||   |          ' | j                  | j                  d      ||   ||       | j                  | j                  d      ||   ||        y )Nquerystrictcompose)r1   groupskeyed_groups)	
get_option
isinstancer	   r.   _set_composite_vars	inventoryset_variable_add_host_to_composed_groups_add_host_to_keyed_groups)r   hostvarsr&   r0   varnamer1   keys          r   _set_variableszInventoryModule._set_variablesp   s      	qDOOG,EE>:$ ZG.2.C.CD%PW..YHTN7+Z __X.F $$T__Y%?$QU^d$e  ~ L++D#x~c7JKL --dooh.GRVY]fl-m **4??>+JHUYN\`io*p+	qr   c           	        t        |      }|j                  di       j                  di       }|D ]  }|dk(  r	| j                  j	                  |      }||   j                  dg       }|D ]&  }| j                  |g|j                  |i       |       ( | j                  j                  d|        |sF|D ]@  }| j                  j                  |       | j                  |g|j                  |i              B y y )N_metar<   allhosts)r   popgetr8   	add_group_populate_host_vars	add_childadd_host)r   source_datar<   grouprC   r&   s         r   _populate_from_cachez$InventoryModule._populate_from_cache   s    !+.??7B/33JC  	7E~007#E*..w;! TD,,dVX\\$5KUST((6	7   I''-(($dB1GHI r   c                0   |r| j                  |       |S ddi ii}i }dx}}d }| j                  d      }|D ]  }	t        |	      }	d|	vr	 |	j                  dd      \  }
}|
j                         dk(  r>|j                         }|
j                  d      rod|vrkt        |      }||vr i ||<   | j                  j                  |       | j                  ||      }|s| j                  j                  |d	t        |             |
d
k(  r:| j                  d      r| j                  |||       n| j                  |||       t        d|
j                         j                  dd             }t        |
      t        |
j!                  d            z
  }d|cxk  rdk  r=n n:|||   vst#        ||   |   t$              si ||   |<   t        |      ||   |   |<   n|dkD  r|dk7  rt        |      ||   |<   | j'                  ||      r"d|vrdg i|d<   |d   d   j)                  |       |} | j+                  |       |D ]<  }| j                  j-                  |      }|j.                  |d   d   |j0                  <   > |S # t        $ r Y ]w xY w)NrA   r<    network_info_pathr    r!   Name,ansible_hostGroupsenable_advanced_group_parsingvbox_ _r      	ungroupedrC   )rL   r5   r   r#   r%   r$   
startswithr   r8   rI   r.   r9   _handle_vboxmanage_group_string_handle_group_stringreplacelenlstripr6   dict_ungrouped_hostappendr?   get_hostvarsname)r   rJ   using_current_cachecacheable_resultsr<   prevkeypref_kcurrent_hostnetinfolinekvnetdataleading_spacesr&   hs                   r   _populate_from_sourcez%InventoryModule._populate_from_source   s   %%k2$z2&67& //"56 5	!D4=D$zz#q)1
 wwyB	A||F#1*1~x/-/H\*NN++L9 //gFNN//nkZaNbc h??#BC88LJ[\--a?PQ %uQWWY->->sC-H,I%JK!$Q#ahhsm*<!<~**h|&<<JxXdOefmOnptDu:<.w7>I!nH\*73F;#a'Bw9DQ.v6''6GH"*;;:A2)+6%k27;BB<P k5	!n 	H% 	DD''-A=>VVg&z2166:	D ! m  s   J	JJc                2      fd}t         |||            S )Nc              3     K   |j                         D ]G  \  }}|dk(  rt        |t              rj                  | |       1t        |t              sB| |v I d y w)NrA   T)itemsr6   r`   ra   list)r&   r8   rm   rn   r   s       r   	find_hostz2InventoryModule._ungrouped_host.<locals>.find_host   sc     !) (1<a&..tQ774(a-'( Js   AA#A#)rB   )r   r&   r8   rw   s   `   r   ra   zInventoryModule._ungrouped_host   s    	 9T9-..r   c                    |j                  d      D ]i  }|st        |      }| j                  j                  |      }| j                  j	                  ||       ||vrdg i||<   ||   d   j                  |       k y)zoHandles parsing the VM's Group assignment from VBoxManage according to this inventory's initial implementation./rC   Nr#   r   r8   rF   rH   rb   )r   vboxmanage_grouprj   rg   rK   s        r   r\   z$InventoryModule._handle_group_string   s     &++C0 	GE#E*007((= 1107}%e,!%(188F	Gr   c                   |j                  d      D ]  }|s|dk(  rd}|j                  d      D ]  }|s|dk(  rt        |      }| j                  j                  |      }|| j                  j	                  ||       | j                  j	                  ||       ||vrdg i||<   ||   d   j                  |       |}  y)z`Handles parsing the VM's Group assignment from VBoxManage according to VirtualBox documentation.rQ   ry   NrC   rz   )r   r{   rj   rg   rK   parent_groupsubgroups          r   r[   z/InventoryModule._handle_vboxmanage_group_string  s     &++C0 !	(E | L!KK, ( s? &x0>>33H=+NN,,\8D((<@#443:B-%h/!(+G4;;LI')(!	(r   c                X    d}t         t        |   |      r|j                  d      rd}|S )NF)zvirtualbox.yamlzvirtualbox.ymlz	vbox.yamlzvbox.ymlT)r   r   verify_fileendswith)r   pathvalidr   s      r   r   zInventoryModule.verify_file.  s.    $3D9}}[\r   c                   	 t        | j                        | _        t
        t        |   |||       | j                  |      }| j                  |      }| j                  |       d }|r| j                  d      }d}	|r	 | j                  |   }|st        | j                  d      dd      }
| j                  d      }| j                  d	d
g}|r|j                  d       n|j                  d       |
rAt         j"                  j%                  |
      r"|j                  d       |j                  |
       	 t'        |t(              }|j.                  j1                         j3                         }|xr |	 }| j5                  ||      }|	r|| j                  |<   y y # t        $ r}t	        |      d }~ww xY w# t        $ r d}	Y 7w xY w# t*        $ r}t	        t-        |            d }~ww xY w)NcacheFTsettings_password_filer   passthru)r   	nonstringrunning_onlys   lists   -ls
   runningvmss   vmss   --settingspwfiler   )r   VBOXr   
ValueErrorr   r   r   parseget_cache_key_read_config_data_consume_optionsr5   _cacheKeyErrorr   rb   osr   existsr   r   r%   strr   r"   
splitlinesrr   )r   r8   loaderr   r   e	cache_keyconfig_datarJ   update_cacheb_pwfilerunningr)   prf   rg   r   s                   r   r   zInventoryModule.parse6  s   	(*4995DO 	ot*9fdC&&t,	,,T2 	k*OOG,E$"kk)4 0H IRgs}~Hoon5G ??GU3C

=)

6"BGGNN84

./

8$1#d+ ((--/446K#8L(8 66{DWX%6DKK	" a  	($Q''	((  $#$(  1(Q001sA   F ?F7 6G	 	F4$F//F47GG		G+G&&G+)F)T)__name__
__module____qualname____doc__NAMEr   r   r.   r?   rL   rr   ra   r\   r[   r   r   __classcell__)r   s   @r   r   r   W   sM    E)DD0q4I"J!X/G+(Z57 57r   r   )
__future__r   DOCUMENTATIONEXAMPLESr   
subprocessr   r   ansible.errorsr   +ansible.module_utils.common.text.convertersr   r   /ansible.module_utils.common._collections_compatr	   ansible.plugins.inventoryr
   r   r   #ansible.module_utils.common.processr   Aansible_collections.community.general.plugins.plugin_utils.unsafer   r    r   r   <module>r      sI    #/b" 
 " - I J S S < YT7)=) T7r   