a ì)g@ã@sâddlmZmZmZeZddlmZddlZddl Z ddl m Z ddl m Z ddlmZddlmZmZddlmZmZmZdd lmZdd lmZdd lmZmZeƒZe gd ¢ƒZ!Gd d„deƒZ"ddd„Z#e$dkrÞe#ƒdS)é)Úabsolute_importÚdivisionÚprint_function)ÚCLIN)Ú constants)Úcontext)Úoption_helpers)Ú AnsibleErrorÚAnsibleOptionsError)Úto_bytesÚ to_nativeÚto_text)Ú combine_vars)ÚDisplay)Úget_vars_from_inventory_sourcesÚget_vars_from_path)Zansible_diff_modeZansible_config_fileZ ansible_factsZ ansible_forksZansible_inventory_sourcesZ ansible_limitZansible_playbook_pythonZansible_run_tagsZansible_skip_tagsZansible_verbosityZansible_versionZ inventory_dirZinventory_fileZinventory_hostnameZinventory_hostname_shortÚgroupsZ group_namesZomitZ playbook_dircsÒeZdZdZdZdddœZ‡fdd„Z‡fdd „Z‡fd d „Z‡fd d „Z e dd„ƒZ dd„Z dd„Z dd„Ze dd„ƒZe dd„ƒZe dd„ƒZe d)dd„ƒZd*dd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Z‡ZS)+Ú InventoryCLIzE used to display or dump the configured inventory as Ansible sees it zansible-inventoryzHThe name of a host to match in the inventory, relevant when using --listzAThe name of a group in the inventory, relevant when using --graph)ÚhostÚgroupcs&tt|ƒ |¡d|_d|_d|_dS©N)ÚsuperrÚ__init__ÚvmÚloaderÚ inventory)ÚselfÚargs©Ú __class__©ú9/usr/lib/python3.9/site-packages/ansible/cli/inventory.pyr9szInventoryCLI.__init__csPtt|ƒjddd�t |j¡t |j¡t |j¡t |j¡|jj ddt j tj dd�|jj dt j tj d �|jj d d dd �|j  d d¡}|j dddddd�|j dddddd�|j dddddd�|j  |¡|jj ddddddd�|jj d ddd!d"d�|jj d#ddd$d%d�|jj d&dtjd'd(d�|jj d)dd*d+d,�dS)-Nz#usage: %prog [options] [host|group]zWShow Ansible inventory information, by default it uses the inventory script JSON format)ÚusageZdescz-lz--limitú?)ÚhelpÚactionÚnargsz --list-hosts)r$r%rz host|group)Úmetavarr&ZActionsz6One of following must be used on invocation, ONLY ONE!z--listÚ store_trueFÚlistz0Output all hosts info, works as inventory script)r%ÚdefaultÚdestr$z--hostÚstorerz4Output specific host info, works as inventory scriptz--graphÚgraphzJcreate inventory graph, if supplying pattern it must be a valid group namez-yz--yamlÚyamlzis    zInventoryCLI.post_process_argsc sœtt|ƒ ¡| ¡\|_|_|_d}tjdrr|j  tjd¡}t |ƒdkrVt dƒ‚|j |dd�}|  |¡}nhtjdr†| ¡}nTtjdrÚ| d¡}tjd r°| |¡}n tjd rÆ| |¡}n | |¡}|  |¡}|�rŽtjd }|durþt |¡n†zBtt|ƒd ƒ�}| t|ƒ¡Wdƒn1�s40YWnBttf�y‚}z$td t|ƒt|ƒfƒ‚WYd}~n d}~00t d¡t d¡dS)Nrr<z5You must pass a single valid host to --host parameterr©rr-r)r=r.r/r2Úwbz,Unable to write to destination file (%s): %s)rrÚrunZ _play_prereqsrrrrÚCLIARGSÚ get_hostsÚlenr Ú_get_host_variablesÚdumpÚinventory_graphÚ _get_groupÚyaml_inventoryÚtoml_inventoryÚjson_inventoryr@Úopenr ÚwriteÚOSErrorÚIOErrorr r ÚsysÚexit)rÚresultsÚhostsÚmyvarsÚtopZoutfileÚfÚerr r!rG�s<               2. zInventoryCLI.runc CsDtjdr8ddl}ddlm}t|j||ddd�ƒ}�ntjdrÂddlm}z ||ƒ}Wndt yŠ}zt d |ƒ‚WYd}~n>d}~0t y¾}zt d |j dƒ‚WYd}~n d}~00n~ddl }dd lm}z|j||dd ddd �}WnNt �y>}z4|j||dd ddd �}t dt|ƒ¡WYd}~n d}~00|S)Nr.r)Ú AnsibleDumperFT)ZDumperZdefault_flow_styleZ allow_unicoder/)Ú toml_dumpszLThe source inventory contains a value that cannot be represented in TOML: %saThe source inventory contains a non-string key (%s) which cannot be represented in TOML. The specified key will need to be converted to a string. Be aware that if your playbooks expect this key to be non-string, your playbooks will need to be modified to support this change.)ÚAnsibleJSONEncoderé)ÚclsZ sort_keysÚindentZpreprocess_unsafeZ ensure_asciiz?Could not sort JSON output due to issues while sorting keys: %s)rrHr.Zansible.parsing.yaml.dumperr^r rLZansible.plugins.inventory.tomlr_Ú TypeErrorr ÚKeyErrorrÚjsonZansible.parsing.ajsonr`Údumpsr@Zwarningr )Ústuffr.r^rXr_r]rfr`r r r!rL®s4     ÿýÿ (zInventoryCLI.dumpcCsj| ¡}t|t|j|jj|gdƒƒ}tjdrLt|t|jtjd|gdƒƒ}|j dkr`|j |d<|  |¡S)Nr=Úbasedirr<Zansible_group_priority) Úget_varsrrrrÚ_sourcesrrHrÚpriorityÚ_remove_internal)rrÚresr r r!Ú_get_group_variablesÏs   z!InventoryCLI._get_group_variablescCsttjdrX| ¡}t|t|j|jj|gdƒƒ}tjdrjt|t|jtjd|gdƒƒ}n|j j|ddd�}|  |¡S)Nr1r=riF)rZinclude_hostvarsZstage) rrHrjrrrrrkrrrm)rrÚhostvarsr r r!rKÞs   z InventoryCLI._get_host_variablescCs|jj |¡}|Sr)rrÚget)rÚgnamerr r r!rNîszInventoryCLI._get_groupcCstD]}||vr||=q|Sr)Ú INTERNAL_VARS)rLZinternalr r r!rmòszInventoryCLI._remove_internalcCs$dD]}||vr||s||=qdS)N)rYÚvarsÚchildrenr )rLÚxr r r!Ú _remove_emptyûszInventoryCLI._remove_emptycCs8g}t| ¡ƒD]"\}}| t d||f|¡¡q|S)Nz {%s = %s})ÚsortedÚitemsÚappendrÚ _graph_name)rLÚdepthÚresultÚnameÚvalr r r!Ú _show_varsszInventoryCLI._show_varsrcCs|rd|d|}|S)Nz |z--%sr )r~r|r r r!r{ szInventoryCLI._graph_namecCs°| d|j|¡g}|d}|jD]}| | ||¡¡q"|jdkrŠ|jD]>}| | |j|¡¡tjdrJ| |  |  |¡|d¡¡qJtjdr¬| |  |  |¡|¡¡|S)Nz@%s:r<r=r0) r{r~Ú child_groupsÚextendÚ _graph_grouprYrzrrHr€rKro)rrr|r}Zkidrr r r!rƒs     zInventoryCLI._graph_groupcCs0| tjd¡}|r$d | |¡¡Stdƒ‚dS)NrAÚ z3Pattern must be valid group name when using --graph)rNrrHÚjoinrƒr )rZstart_atr r r!rM!szInventoryCLI.inventory_graphcsbtƒ‰‡‡‡fdd„‰ˆ|ƒ}dii|d<ˆj ¡}|D]$}ˆ |¡}|r8||dd|j<q8|S)NcsÊi}i||j<|jdkr2dd„|jDƒ||jd<g||jd<|jD]>}||jd |j¡|jˆvrF| ˆ|ƒ¡ˆ |j¡qFtjdr¤ˆ |¡||jd<ˆ  ||j¡||jsÆ||j=|S)Nr=cSsg|] }|j‘qSr )r~)Ú.0Úhr r r!Ú 1ózEInventoryCLI.json_inventory..format_group..rYrur1rt) r~rYr�rzÚupdateÚaddrrHrorw)rrXÚsubgroup©Ú format_groupÚseenrr r!rŽ-s       z1InventoryCLI.json_inventory..format_grouprpZ_meta)ÚsetrrIrKr~)rr[rXrYrZhvarsr r�r!rQ)s   zInventoryCLI.json_inventorycsg‰‡‡‡fdd„‰ˆ|ƒS)Ncsâi}i||j<i||jd<|jD]&}|jdkr"||jd ˆ|ƒ¡q"i||jd<|jdkr¨|jD]>}i}|jˆvr’ˆ |j¡ˆj|d�}|||jd|j<qhtjdrΈ |¡}|rÎ|||jd<ˆ  ||j¡|S)Nrur=rYrEr1rt) r~r�rŠrYrzrKrrHrorw)rrXrŒr‡rZÚgvarsr�r r!rŽQs(          z1InventoryCLI.yaml_inventory..format_groupr )rr[r r�r!rOMszInventoryCLI.yaml_inventorycs<tƒ‰ttdd„|jDƒƒƒ‰‡‡‡‡fdd„‰ˆ|ƒ}|S)Ncss|]}|jdkr|jVqdS)Ú ungroupedN)r~rY)r†Úgr r r!Ú tr‰z.InventoryCLI.toml_inventory..c s,i}i||j<g||jd<|jD]B}|jdkr6ˆs6q"|jdkrV||jd |j¡| ˆ|ƒ¡q"|jdkrä|jD]l}|jˆvržˆ |j¡ˆj|d�}ni}z|||jd|j<Wqvtyà|j|i||jd<Yqv0qvtj d�rˆ  |¡||jd<ˆ  ||j¡||j�s(||j=|S)Nrur’r=rErYr1rt) r~r�rzrŠrYr‹rKrerrHrorw)rrXrŒrZ host_vars©rŽZ has_ungroupedr�rr r!rŽvs2          z1InventoryCLI.toml_inventory..format_group)r�ÚboolÚnextr�)rr[rXr r•r!rPrs !zInventoryCLI.toml_inventory)r)r)Ú__name__Ú __module__Ú __qualname__Ú__doc__r~Z ARGUMENTSrr3r>rGÚ staticmethodrLrorKrNrmrwr€r{rƒrMrQrOrPÚ __classcell__r r rr!r1s6ÿ  )  -      $%rcCst |¡dSr)rZ cli_executor)rr r r!ÚmainœsržÚ__main__)N)%Z __future__rrrÚtypeZ __metaclass__Z ansible.clirrVr7Zansiblerr:rZansible.cli.argumentsrr4Zansible.errorsr r Zansible.module_utils._textr r r Zansible.utils.varsrZansible.utils.displayrZansible.vars.pluginsrrr@Ú frozensetrsrržr˜r r r r!Ús(       m