a )g@@sddlmZmZmZeZddlmZddlZddl Z ddl m Z ddl m Z ddlmZddlmZmZddlmZmZmZdd lmZdd lmZdd lmZmZeZe gd Z!Gd ddeZ"dddZ#e$dkre#dS))absolute_importdivisionprint_function)CLIN) constants)context)option_helpers) AnsibleErrorAnsibleOptionsError)to_bytes to_nativeto_text) combine_vars)Display)get_vars_from_inventory_sourcesget_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_shortgroupsZ group_namesZomitZ playbook_dircseZdZdZdZdddZfddZfdd Zfd d Zfd d Z e ddZ ddZ ddZ ddZe ddZe ddZe ddZe d)ddZd*dd Zd!d"Zd#d$Zd%d&Zd'd(ZZS)+ 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)hostgroupcs&tt||d|_d|_d|_dSN)superr__init__vmloader inventory)selfargs __class__9/usr/lib/python3.9/site-packages/ansible/cli/inventory.pyr9szInventoryCLI.__init__csPtt|jdddt|jt|jt|jt|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?)helpactionnargsz --list-hosts)r$r%rz host|group)metavarr&ZActionsz6One of following must be used on invocation, ONLY ONE!z--list store_trueFlistz0Output all hosts info, works as inventory script)r%defaultdestr$z--hoststorerz4Output specific host info, works as inventory scriptz--graphgraphzJcreate inventory graph, if supplying pattern it must be a valid group namez-yz--yamlyamlzis    zInventoryCLI.post_process_argsc stt||\|_|_|_d}tjdrr|j tjd}t |dkrVt d|j |dd}| |}nhtjdr|}nTtjdr|d}tjd r||}n tjd r||}n ||}| |}|rtjd }|durt|nzBtt|d }|t|Wdn1s40YWnBttfy}z$td t|t|fWYd}~n d}~00tdtddS)Nrr<z5You must pass a single valid host to --host parameterrrr-r)r=r.r/r2wbz,Unable to write to destination file (%s): %s)rrrunZ _play_prereqsrrrrCLIARGS get_hostslenr _get_host_variablesdumpinventory_graph _get_groupyaml_inventorytoml_inventoryjson_inventoryr@openr writeOSErrorIOErrorr r sysexit)rresultshostsmyvarstopZoutfileferr r!rGs<               2. zInventoryCLI.runc CsDtjdr8ddl}ddlm}t|j||ddd}ntjdrddlm}z ||}Wndt y}zt d |WYd}~n>d}~0t y}zt d |j dWYd}~n d}~00n~ddl }dd lm}z|j||dd ddd }WnNt y>}z4|j||dd ddd }tdt|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_keysindentZpreprocess_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 KeyErrorrjsonZansible.parsing.ajsonr`dumpsr@Zwarningr )stuffr.r^rXr_r]rfr`r r r!rLs4      (zInventoryCLI.dumpcCsj|}t|t|j|jj|gd}tjdrLt|t|jtjd|gd}|j dkr`|j |d<| |S)Nr=basedirr<Zansible_group_priority) get_varsrrrr_sourcesrrHrpriority_remove_internal)rrresr r r!_get_group_variabless   z!InventoryCLI._get_group_variablescCsttjdrX|}t|t|j|jj|gd}tjdrjt|t|jtjd|gd}n|j j|ddd}| |S)Nr1r=riF)rZinclude_hostvarsZstage) rrHrjrrrrrkrrrm)rrhostvarsr r r!rKs   z InventoryCLI._get_host_variablescCs|jj|}|Sr)rrget)rgnamerr r r!rNszInventoryCLI._get_groupcCstD]}||vr||=q|Sr) INTERNAL_VARS)rLZinternalr r r!rmszInventoryCLI._remove_internalcCs$dD]}||vr||s||=qdS)N)rYvarschildrenr )rLxr r r! _remove_emptyszInventoryCLI._remove_emptycCs8g}t|D]"\}}|td||f|q|S)Nz {%s = %s})sorteditemsappendr _graph_name)rLdepthresultnamevalr 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|| | ||dqJtjdr|| | |||S)Nz@%s:r<r=r0) r{r~ child_groupsextend _graph_grouprYrzrrHrrKro)rrr|r}Zkidrr r r!rs     zInventoryCLI._graph_groupcCs0|tjd}|r$d||StddS)NrA z3Pattern must be valid group name when using --graph)rNrrHjoinrr )rZstart_atr r r!rM!szInventoryCLI.inventory_graphcsbtfdd|}dii|d<j}|D]$}|}|r8||dd|j<q8|S)Ncsi}i||j<|jdkr2dd|jD||jd<g||jd<|jD]>}||jd|j|jvrF|||jqFtjdr|||jd< ||j||js||j=|S)Nr=cSsg|] }|jqSr )r~).0hr r r! 1zEInventoryCLI.json_inventory..format_group..rYrur1rt) r~rYrrzupdateaddrrHrorw)rrXsubgroup format_groupseenrr r!r-s       z1InventoryCLI.json_inventory..format_grouprpZ_meta)setrrIrKr~)rr[rXrYrZhvarsr rr!rQ)s   zInventoryCLI.json_inventorycsgfdd|S)Ncsi}i||j<i||jd<|jD]&}|jdkr"||jd|q"i||jd<|jdkr|jD]>}i}|jvr|jj|d}|||jd|j<qhtjdrΈ|}|r|||jd< ||j|S)Nrur=rYrEr1rt) r~rrrYrzrKrrHrorw)rrXrrrZgvarsrr r!rQs(          z1InventoryCLI.yaml_inventory..format_groupr )rr[r rr!rOMszInventoryCLI.yaml_inventorycs<tttdd|jDfdd|}|S)Ncss|]}|jdkr|jVqdS) ungroupedN)r~rY)rgr r r! trz.InventoryCLI.toml_inventory..c s,i}i||j<g||jd<|jD]B}|jdkr6s6q"|jdkrV||jd|j||q"|jdkr|jD]l}|jvr|jj|d}ni}z|||jd|j<Wqvty|j|i||jd<Yqv0qvtj dr |||jd< ||j||js(||j=|S)Nrurr=rErYr1rt) r~rrzrrYrrKrerrHrorw)rrXrrZ host_varsrZ has_ungroupedrrr r!rvs2          z1InventoryCLI.toml_inventory..format_group)rboolnextr)rr[rXr rr!rPrs !zInventoryCLI.toml_inventory)r)r)__name__ __module__ __qualname____doc__r~Z ARGUMENTSrr3r>rG staticmethodrLrorKrNrmrwrr{rrMrQrOrP __classcell__r r rr!r1s6  )  -      $%rcCst|dSr)rZ cli_executor)rr r r!mainsr__main__)N)%Z __future__rrrtypeZ __metaclass__Z ansible.clirrVr7Zansiblerr:rZansible.cli.argumentsrr4Zansible.errorsr r Zansible.module_utils._textr r r Zansible.utils.varsrZansible.utils.displayrZansible.vars.pluginsrrr@ frozensetrsrrrr r r r!s(       m