a )g)@sddlmZmZmZeZddlZddlmZ ddl m Z ddl m Z ddlmZddlmZddlmZdd lmZdd lmZeZGd d d eZdS) )absolute_importdivisionprint_functionN) 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 ungroupedrr)groupshosts_groups_dict_cache localhostcurrent_sourceprocessed_sources add_group add_child)selfgroupr:/usr/lib/python3.9/site-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 deserializeHs     zInventoryData.deserializecCs\|jr|j}nJt|}d|_d|_tj}|s:d}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 localhostZansible_python_interpreterZansible_connectionr) rraddressimplicitsys executabledisplaywarning set_variable)rpatternZnew_hostZ py_interprrr_create_implicit_localhostPs   z(InventoryData._create_implicit_localhostc CsFtdd|_t}|jD]:}|j|}||j|jdkr|s|d|jqt}|j D]}||j| }|jd|vrt| t|jd|jdgr|jd |n:|jst|}|dks|dkr|jd|vr|d|j|jrht|jd|j|_qh||D]}td|q&i|_dS)z1 Ensure inventory basic rules, run after updates z(Reconcile groups and hosts in inventory.Nrrrz,Found both group and host with same name: %s)r(debugrsetraddnameZ get_ancestorsrrvaluesZ get_groups difference remove_hostr%lenr Zget_varsvars intersectionr)r) rZ group_namesgrZ host_nameshostZmygroupslengthZconflictrrrreconcile_inventoryhs0     "z!InventoryData.reconcile_inventorycCs.|j|d}|dur*|tjvr*||}|S)z; fetch host object using name deal with implicit localhost N)rr"C LOCALHOSTr,)rhostnameZ matching_hostrrrget_hosts zInventoryData.get_hostcCs|rxt|ts"tdt||f||jvrht|}|j|jvr`||j|j<i|_t d||j}qt d|n td||S)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) isinstancer rtyperrr1rr(r.)rrr8rrrrs     zInventoryData.add_groupcCsJ||jvr&|j|=td|i|_|jD]}|j|}||q,dS)NzRemoved group %s from inventory)rr(r.rr remove_group)rrr9hrrrrBs   zInventoryData.remove_groupNcCsR|rBt|ts$tdt||fd}|rN||jvrB|j|}n td|||jvrt||}||j|<|jr||d|j||dt |jn||dd||ddt d||t j vr|jdur|j||_t d|nt d||jjfn |j|}|rN||i|_t d ||fn td ||S) 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 inventoryZinventory_fileZ 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)r@r rrArrrrr*r r(r.r<r=rZvvvvr)r1add_hostr)rr9rportr8rCrrrrDs:             zInventoryData.add_hostcCs:|j|jvr|j|j=|jD]}|j|}||qdS)N)r1rrr4)rr9rr8rrrr4s     zInventoryData.remove_hostcCsZ||jvr|j|}n"||jvr,|j|}n td||||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(r.)rZentityZvarnamevalueZ inv_objectrrrr*s      zInventoryData.set_variablecCsd}||jvrv|j|}||jvr4||j|}n(||jvrP||j|}n td|i|_td||fn td||S)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)rZadd_child_grouprrDrrr(r.)rrchildZaddedr8rrrrs      zInventoryData.add_childcCs8|js2|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)r1).0rCrrr z1InventoryData.get_groups_dict..)rritemsZ get_hosts)rZ group_namerrrrget_groups_dictszInventoryData.get_groups_dict)NN)__name__ __module__ __qualname____doc__rr!r#r,r;r?rrBrDr4r*rrLrrrrr $s ,  -  r )Z __future__rrrrAZ __metaclass__r&Zansiblerr<Zansible.errorsrZansible.inventory.grouprZansible.inventory.hostrZansible.module_utils.sixr Zansible.utils.displayr Zansible.utils.varsr Zansible.utils.pathr r(objectr rrrrs