o nnhZ)@sddlmZddlZddlmZddlmZddlm Z ddl m Z ddl m Z ddlmZdd lmZdd lmZeZGd d d eZdS) ) annotationsN) constants) AnsibleError)Group)Host) string_types)Display) combine_vars)basedirc@szeZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ dddZ ddZ ddZddZddZdS) InventoryDataz Holds inventory data (host and group objects). Using it's methods should guarantee expected relationships and data. cCsHi|_i|_i|_d|_d|_g|_dD]}||q|dddS)N)all ungroupedr r )groupshosts_groups_dict_cache localhostcurrent_sourceprocessed_sources add_group add_child)selfgrouprA/usr/local/lib/python3.10/dist-packages/ansible/inventory/data.py__init__)s zInventoryData.__init__cCs$d|_|j|j|j|j|jd}|S)N)rrlocalsourcer)rrrrrrrdatarrr serialize<szInventoryData.serializecCsFi|_|d|_|d|_|d|_|d|_|d|_dS)Nrrrrr)rgetrrrrrrrrr deserializeGs     zInventoryData.deserializecCs^|jr|j}|St|}d|_d|_tj}|sd}td|d||dd||_|S)Nz 127.0.0.1Tz/usr/bin/pythonzUnable to determine python interpreter from sys.executable. Using /usr/bin/python default. You can correct this by setting ansible_python_interpreter for localhostansible_python_interpreteransible_connectionr) rraddressimplicitsys executabledisplaywarning set_variable)rpatternnew_host py_interprrr_create_implicit_localhostOs   z(InventoryData._create_implicit_localhostc CsDtdd|_t}|jD]}|j|}||j|jdkr+|s+|d|jqt}|j D]Y}||j| }|jd|vrat| t|jd|jdgr`|jd |n|js~t|}|dksw|dkr~|jd|vr~|d|j|jrt|jd|j|_q4||D] }td|qi|_dS)z1 Ensure inventory basic rules, run after updates z(Reconcile groups and hosts in inventory.Nr r rz,Found both group and host with same name: %s)r(debugrsetraddname get_ancestorsrrvalues get_groups difference remove_hostr%lenr get_varsvars intersectionr)r) r group_namesgr host_nameshostmygroupslengthconflictrrrreconcile_inventorygs6     " z!InventoryData.reconcile_inventorycCs.|j|d}|dur|tjvr||}|S)z; fetch host object using name deal with implicit localhost N)rr C LOCALHOSTr.)rhostname matching_hostrrrget_hosts zInventoryData.get_hostcCs|r>t|tstdt||f||jvr5t|}|j|jvr0||j|j<i|_t d||j}|St d||Std|)zM adds a group to inventory if not there already, returns named actually used z@Invalid group name supplied, expected a string but got %s for %szAdded group %s to inventoryzgroup %s already in inventoryz+Invalid empty/false group name provided: %s) isinstancerrtyperrr3rr(r0)rrr>rrrrs     zInventoryData.add_groupcCsJ||jvr|j|=td|i|_|jD] }|j|}||qdS)NzRemoved group %s from inventory)rr(r0rr remove_group)rrr@hrrrrLs    zInventoryData.remove_groupNcCsH|rt|tstdt||fd}|r&||jvr |j|}ntd|||jvrt||}||j|<|jrK||d|j||dt |jn||dd||ddt d||t j vr|jdurx|j||_t d|nt d||jjfn|j|}|r||i|_t d ||f|Std |) zD adds a host to inventory and possibly a group if not there already z?Invalid host name supplied, expected a string but got %s for %sNz$Could not find group %s in inventoryinventory_file inventory_dirzAdded host %s to inventoryzSet default localhost to %szMA duplicate localhost-like entry was found (%s). First found localhost was %szAdded host %s to group %sz$Invalid empty host name provided: %s)rJrrrKrrrrr*r r(r0rErFrvvvvr)r3add_hostr)rr@rportr>rMrrrrQs<             zInventoryData.add_hostcCs:|j|jvr |j|j=|jD] }|j|}||qdS)N)r3rrr8)rr@rr>rrrr8s     zInventoryData.remove_hostcCsZ||jvr |j|}n||jvr|j|}ntd||||td||fdS)z) sets a variable for an inventory object z)Could not identify group or host named %sz set %s for %sN)rrrr*r(r0)rentityvarnamevalue inv_objectrrrr*s      zInventoryData.set_variablecCsd}||jvr<|j|}||jvr||j|}n||jvr(||j|}ntd|i|_td||f|Std|)z Add host or group to group Fz %s is not a known host nor groupzGroup %s now contains %sz%s is not a known group)radd_child_grouprrQrrr(r0)rrchildaddedr>rrrrs      zInventoryData.add_childcCs8|js|jD]\}}dd|D|j|<q|jS)z We merge a 'magic' var 'groups' with group name keys and hostname list values into every host variable set. Cache for speed. cSsg|]}|jqSr)r3).0rMrrr sz1InventoryData.get_groups_dict..)rritems get_hosts)r group_namerrrrget_groups_dictszInventoryData.get_groups_dict)NN)__name__ __module__ __qualname____doc__rrr!r.rDrIrrLrQr8r*rr_rrrrr #s ,  -  r ) __future__rr&ansiblerrEansible.errorsransible.inventory.groupransible.inventory.hostransible.module_utils.sixransible.utils.displayransible.utils.varsr ansible.utils.pathr r(objectr rrrrs