a ì)gµUã@s0ddlmZmZmZeZddlmZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlmZddlmZddlmZddlmZddlmZmZdd lmZdd lmZdd lmZdd l m!Z!dd l"m#Z#m$Z$ddl%m&Z&ddl'm(Z(ddl)m*Z*e*ƒZ+Gdd„dee j,ƒZ-ddd„Z.e/dkr,e.ƒdS)é)Úabsolute_importÚdivisionÚprint_function)ÚCLIN)Ú constants)Úcontext)Úoption_helpers)ÚTaskQueueManager)Ú to_nativeÚto_text)Úboolean)Úparse_kv)ÚPlay)Ú list_plugins)Ú module_loaderÚfragment_loader)Ú plugin_docs)Ústringc)ÚDisplaycsÒeZdZdZdZgZddiZejp&ej Z ‡fdd„Z ‡fdd„Z ‡fd d „Z d d „Zd d„Zdd„Zdd„Zdfdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zd d!„ZeZeZd"d#„Zd$d%„Zd&d'„Zd(d)„Zd*d+„Zd,d-„Z d.d/„Z!d0d1„Z"d2d3„Z#d4d5„Z$d6d7„Z%d8d9„Z&d:d;„Z'dd?„Z)d@dA„Z*dBdC„Z+dDdE„Z,dFdG„Z-dHdI„Z.dJdK„Z/dLdM„Z0dNdO„Z1dPdQ„Z2dRdS„Z3dTdU„Z4dVdW„Z5e4Z6e5Z7dXdY„Z8dZd[„Z9d\d]„Z:d^d_„Z;d`da„Z<‡fdbdc„Z=ddde„Z>‡Z?S)gÚ ConsoleCLIa“ A REPL that allows for running ad-hoc tasks against a chosen inventory from a nice shell with built-in tab completion (based on dominis' ansible-shell). It supports several commands, and you can modify its configuration at runtime: - `cd [pattern]`: change host/group (you can use host patterns eg.: app*.dc*:!app01*) - `list`: list available hosts in the current path - `list groups`: list groups included in the current path - `become`: toggle the become flag - `!`: forces shell module instead of the ansible module (!yum update -y) - `verbosity [num]`: set the verbosity level - `forks [num]`: set the number of forks - `become_user [user]`: set the become_user - `remote_user [user]`: set the remote_user - `become_method [method]`: set the privilege escalation method - `check [bool]`: toggle check mode - `diff [bool]`: toggle diff mode - `timeout [integer]`: set the timeout of tasks in seconds (0 to disable) - `help [command/module]`: display documentation for the command or module - `exit`: exit ansible-console zansible-consolez host-patternz…A name of a group in the inventory, a shell-like glob selecting hosts in inventory or any combination of the two separated by commas.csˆtt|ƒ |¡d|_g|_g|_d|_d|_d|_t ƒ|_ d|_ d|_ d|_ d|_d|_d|_d|_d|_d|_d|_tj |¡dS)NzAWelcome to the ansible console. Type help or ? to list commands. Ú*)ÚsuperrÚ__init__ZintroÚgroupsÚhostsÚpatternÚvariable_managerÚloaderÚdictÚ passwordsÚcwdÚ remote_userÚbecomeÚ become_userÚ become_methodÚ check_modeÚdiffÚforksÚ task_timeoutÚ collectionsÚcmdÚCmd©ÚselfÚargs©Ú __class__©ú7/usr/lib/python3.9/site-packages/ansible/cli/console.pyrHs&zConsoleCLI.__init__csºtt|ƒjdddt |j¡t |j¡t |j¡t |j¡t  |j¡t  |j¡t  |j¡t  |j¡t  |j¡t |j¡|jjdddddd|jjd d d d d dS)Nz)REPL console for executing Ansible tasks.zgThis is not a live session/connection: each task is executed in the background and returns its results.)ZdescÚepilogrz host patternÚallú?)ÚhelpÚmetavarÚdefaultÚnargsz--stepÚstepÚ store_truez4one-step-at-a-time: confirm each task before running)ÚdestÚactionr6)rrÚ init_parserÚopt_helpZadd_runas_optionsÚparserZadd_inventory_optionsZadd_connect_optionsZadd_check_optionsZadd_vault_optionsZadd_fork_optionsZadd_module_optionsZadd_basedir_optionsZadd_runtask_optionsZadd_tasknoplay_optionsÚ add_argument©r-r/r1r2r>ds$ þ           ÿzConsoleCLI.init_parsercs,tt|ƒ |¡}|jt_|j|ddd|S)NT)Z runas_optsZ fork_opts)rrÚpost_process_argsÚ verbosityÚdisplayZvalidate_conflicts)r-Úoptionsr/r1r2rCyszConsoleCLI.post_process_argscCst|ƒS©N)ÚdirrBr1r1r2Ú get_namesszConsoleCLI.get_namescCsTztj |¡Wn>ty*| ¡Yn&tyN| d¡| |¡Yn0dS)Nz[Ansible-console was exited])r*r+ÚcmdloopÚKeyboardInterruptÚEOFErrorrEÚdo_exitrBr1r1r2rJ‚s    zConsoleCLI.cmdloopcCsz|jp t ¡}|j |j¡|_d||jt|jƒ|jf}|j rX|j dvrX|d7}t j }n|d7}|j }t||dd|_dS)Nz%s@%s (%d)[f:%s])NÚrootz# z$ T)Zwrap_nonvisible_chars)r!ÚgetpassZgetuserÚ inventoryÚ list_hostsr ÚselectedÚlenr'r"r#ÚCZ COLOR_ERRORÚ NORMAL_PROMPTrÚprompt)r-Z login_userrVZcolorr1r1r2Ú set_promptszConsoleCLI.set_promptcCs td|jƒS)NÚmodule)rr)rBr1r1r2Ú list_modules™szConsoleCLI.list_modulesFc Csh| d¡rdS|js"t d¡dSd}|}|dur`| ¡^}}t |¡r`|}|r\d |¡}nd}|jrn|j}nt j r†t j dkr†t j }nd }d }zr|t j v} t t |t|| d d |jd } t d|jd| g|j|j|j|j|j|j|jd } tƒj| |j|jd} Wn:ty:} z t dt| ƒ¡WYd } ~ dSd } ~ 00z°d |_zdt|j|j|j|j |t j d|j!d|_|j "| ¡}t #|¡W|jr–|j $¡|jrÐ|j %¡n&|jr¼|j $¡|jrÎ|j %¡0|d urêt d¡WdSWnvt&y t d¡YdStyb} z>|j'dkr>dd l(}t )| *¡¡t t| ƒ¡WYd } ~ dSd } ~ 00d S)z actually runs modules ú#Fz No host foundÚshellTú Úr8ZminimalN)Ú check_raw)rXr.)r=Útimeoutz Ansible ShellÚno) ÚnamerZ gather_factsZtasksr!r"r#r$r%r&r))rrzUnable to build command: %s)rPrrrZstdout_callbackZrun_additional_callbacksZrun_treer'zNo hosts foundzUser interrupted executionér)+Ú startswithr rEÚerrorÚsplitrÚ find_pluginÚjoinÚcallbackrTZDEFAULT_LOAD_CALLBACK_PLUGINSZDEFAULT_STDOUT_CALLBACKZ_ACTION_ALLOWS_RAW_ARGSrr r(r!r"r#r$r%r&r)rÚloadrrÚ Exceptionr Z_tqmr rPrr'ÚrunÚdebugZcleanupZcleanup_all_tmp_filesrKrDÚ tracebackÚvÚ format_exc)r-ÚlineZ forceshellrXÚ module_argsZpossible_moduleZ possible_argsÚcbÚresultr^ZtaskZplay_dsZplayÚermr1r1r2r8œs–      õ ø    ý       zConsoleCLI.defaultcCsdSrGr1rBr1r1r2Ú emptylineôszConsoleCLI.emptylinecCs| |d¡dS)a You can run shell commands through the shell module. eg.: shell ps uax | grep java | wc -l shell killall python shell halt -n You can use the ! to force the shell module. eg.: !ps aux | grep java | wc -l TN©r8©r-Úargr1r1r2Údo_shell÷s zConsoleCLI.do_shellcCst d¡dS)Nz4You can run shell commands through the shell module.©rErBr1r1r2Ú help_shellszConsoleCLI.help_shellcCsf|rZz t|ƒ}Wn$ty4t d¡| ¡Yn0|dkrN||_| ¡qbt d¡n| ¡dS)zSet the number of forkszInvalid argument for "forks"rz(forks must be greater than or equal to 1N)ÚintÚ TypeErrorrErdÚ usage_forksr'rW)r-rxr'r1r1r2Údo_forkss     zConsoleCLI.do_forkscCst d¡| ¡dS)Nz'Set the number of forks to use per task)rEr~rBr1r1r2Ú help_forkss zConsoleCLI.help_forkscCst d¡dS)NzUsage: forks rzrBr1r1r2r~szConsoleCLI.usage_forkscCs~|dvrd|_nB|s| ¡n4| d¡}|D]$}|jdur@g|_|j | ¡¡q,|jrpt dd |j¡¡n t d¡dS)z.Set list of collections for 'short name' usage)r]ZnoneNú,z%Collections name search is set to: %sz, z)Collections name search is using defaults)r)Úusage_collectionsreÚappendÚstriprErnrg)r-rxr)Z collectionr1r1r2Údo_collections$s   zConsoleCLI.do_collectionscCst d¡| ¡dS)NzFSet the collection name search path when using short names for plugins)rEr‚rBr1r1r2Úhelp_collections6s zConsoleCLI.help_collectionscCst d¡dS)NzgUsage: collections [, ...] Use empty quotes or "none" to reset to default. rzrBr1r1r2r‚:szConsoleCLI.usage_collectionsc Csl|st d¡nXzt|ƒt_t d|¡Wn:ttfyf}zt dt|ƒ¡WYd}~n d}~00dS)zSet verbosity levelzUsage: verbosity zverbosity level set to %sz)The verbosity must be a valid integer: %sN)rEr|rDrnr}Ú ValueErrorrdr )r-rxrtr1r1r2Ú do_verbosity=s  zConsoleCLI.do_verbositycCst d¡dS)Nz@Set the verbosity level, equivalent to -v for 1 and -vvvv for 4.rzrBr1r1r2Úhelp_verbosityHszConsoleCLI.help_verbositycCsF|s d|_n.|dvrd|_n|j |¡r0||_n t d¡| ¡dS)a Change active host/group. You can use hosts patterns as well eg.: cd webservers cd webservers:dbservers cd webservers:!phoenix cd webservers:&staging cd webservers:dbservers:&staging:!phoenix rz/*r4zno host matchedN)r rPZ get_hostsrErWrwr1r1r2Údo_cdKs   zConsoleCLI.do_cdcCst d¡| ¡dS)NzChange active host/group. )rEÚusage_cdrBr1r1r2Úhelp_cd_s zConsoleCLI.help_cdcCst d¡dS)Nz'Usage: cd ||rzrBr1r1r2r‹cszConsoleCLI.usage_cdcCsT|s|jD]}t |j¡q n2|dkr>|jD]}t |¡q,nt d¡| ¡dS)z#List the hosts in the current grouprzInvalid option passed to "list"N)rRrErarrdÚ help_list)r-rxÚhostÚgroupr1r1r2Údo_listfs   zConsoleCLI.do_listcCst d¡dS)NzLList the hosts in the current group or a list of groups if you add 'groups'.rzrBr1r1r2rrszConsoleCLI.help_listcCs:|r,t|dd|_t d|j¡| ¡n t d¡dS)z$Toggle whether plays run with becomeF©Ústrictzbecome changed to %sz.Please specify become value, e.g. `become yes`N)r r"rErnrWrwr1r1r2Ú do_becomeus  zConsoleCLI.do_becomecCst d¡dS)Nz,Toggle whether the tasks are run with becomerzrBr1r1r2Ú help_become~szConsoleCLI.help_becomecCs"|r||_| ¡n t d¡dS)z6Given a username, set the remote user plays are run byz5Please specify a remote user, e.g. `remote_user root`N)r!rWrErwr1r1r2Údo_remote_users zConsoleCLI.do_remote_usercCst d¡dS)Nz2Set the user for use as login to the remote targetrzrBr1r1r2Úhelp_remote_user‰szConsoleCLI.help_remote_usercCs2|r ||_nt d¡t d|j¡| ¡dS)zFGiven a username, set the user that plays are run by when using becomez1Please specify a user, e.g. `become_user jenkins`zCurrent user is %sN)r#rErnrWrwr1r1r2Údo_become_userŒs  zConsoleCLI.do_become_usercCst d¡dS)NznSet the user for use with privilege escalation (which remote user attempts to 'become' when become is enabled)rzrBr1r1r2Úhelp_become_user•szConsoleCLI.help_become_usercCs:|r||_t d|j¡nt d¡t d|j¡dS)zLGiven a become_method, set the privilege escalation method when using becomezbecome_method changed to %sz7Please specify a become_method, e.g. `become_method su`zCurrent become_method is %sN)r$rErnrwr1r1r2Údo_become_method˜s  zConsoleCLI.do_become_methodcCst d¡dS)NzASet the privilege escalation plugin to use when become is enabledrzrBr1r1r2Úhelp_become_method¡szConsoleCLI.help_become_methodcCsB|r$t|dd|_t d|j¡nt d¡t d|j¡dS)z(Toggle whether plays run with check modeFr‘zcheck mode changed to %sz1Please specify check mode value, e.g. `check yes`zcheck mode is currently %s.N)r r%rErnrwr1r1r2Údo_check¤s  zConsoleCLI.do_checkcCst d¡dS)NzToggle check_mode for the tasksrzrBr1r1r2Ú help_check­szConsoleCLI.help_checkcCsB|r$t|dd|_t d|j¡nt d¡t d|j¡dS)z"Toggle whether plays run with diffFr‘zdiff mode changed to %sz-Please specify a diff value , e.g. `diff yes`zdiff mode is currently %sN)r r&rErnrwr1r1r2Údo_diff°s  zConsoleCLI.do_diffcCst d¡dS)Nz Toggle diff output for the tasksrzrBr1r1r2Ú help_diff¹szConsoleCLI.help_diffc Cst|rhz&t|ƒ}|dkr"t d¡n||_Wqpttfyd}zt dt|ƒ¡WYd}~qpd}~00n| ¡dS)zSet the timeoutrz@The timeout must be greater than or equal to 1, use 0 to disablezAThe timeout must be a valid positive integer, or 0 to disable: %sN)r|rErdr(r}r‡r Ú usage_timeout)r-rxr_rtr1r1r2Ú do_timeout¼s  *zConsoleCLI.do_timeoutcCst d¡| ¡dS)NzSet task timeout in seconds)rErŸrBr1r1r2Ú help_timeoutÊs zConsoleCLI.help_timeoutcCst d¡dS)NzUsage: timeout rzrBr1r1r2rŸÎszConsoleCLI.usage_timeoutcCstj d¡dS)zExits from the consolez Ansible-console was exited. éÿÿÿÿ)ÚsysÚstdoutÚwriter,r1r1r2rMÑs zConsoleCLI.do_exitcCst d¡dS)NzLEAVE!rzrBr1r1r2Ú help_exitÖszConsoleCLI.help_exitcCs¦|r¢t |¡}|r”t |t¡\}}}}|r„t |d¡t d¡|d ¡D]2}t dt||jƒd|d|dd¡qNq¢t  d|¡nt  d |¡dS) NZshort_descriptionz Parameters:rFz r\Ú descriptionrzNo documentation found for %s.z<%s is not a valid command, use ? to list all valid commands.) rrfrÚ get_docstringrrEÚkeysrrUrd)r-Ú module_nameÚin_pathÚocÚaÚ_dummy1Ú_dummy2Úoptr1r1r2Ú helpdefaultÜs  2zConsoleCLI.helpdefaultcCst d¡dS)NzDon't be redundant!)rEZwarningrBr1r1r2Ú help_helpëszConsoleCLI.help_helpcsb| d¡d‰tˆƒt|ƒ‰|jdvr6|j|j}ndd„|j |j¡Dƒ}‡‡fdd„|DƒS)Nr\é)r4rú\cSsg|] }|j‘qSr1©ra©Ú.0Úxr1r1r2Ú õóz*ConsoleCLI.complete_cd..cs.g|]&}t|ƒ tˆƒ¡rt|ƒˆd…‘qSrG)r rc©r·Ús©ZmlineZoffsr1r2r¹÷rº)Ú partitionrSr rrrPrQ©r-ÚtextrpZbegidxZendidxZ completionsr1r½r2Ú complete_cdîs  zConsoleCLI.complete_cdcs\| ¡d| ¡vrX| d¡d‰tˆƒt|ƒ‰| | ¡d¡}‡‡fdd„|DƒSdS)Nrr\r¢cs&g|]}| ˆ¡r|ˆd…d‘qS)Nú=)rcr»r½r1r2r¹ÿrºz.ConsoleCLI.completedefault..)rerYrSrqr¿r1r½r2Úcompletedefaultùs zConsoleCLI.completedefaultcCs2t |¡}tj|tdd\}}}}t|d ¡ƒS)NT)Z is_modulerF)rrfrr¨rÚlistr©)r-rªr«r¬r­r®r¯r1r1r2rqs zConsoleCLI.module_argscsÞttˆƒ ¡d}d}tjdˆ_ˆjˆ_tjdˆ_tjdˆ_tjdˆ_ tjdˆ_ tjdˆ_ tjdˆ_ tjdˆ_ tjd ˆ_tjd r²tjd D]}|ržt |¡qžˆ ¡ˆ_ˆjD]<}tˆd ||f‡fd d „ ƒtˆd||f‡fdd „ ƒqˆ ¡\}}||dœˆ_ˆ ¡\ˆ_ˆ_ˆ_ˆ ˆjtjdˆj¡}ˆj ¡ˆ_dd„|Dƒˆ_dtjvrxt  d¡n t  d¡t!j" #t!j" $d¡d¡}zt %|¡Wnt&yºYn0t' (tj)|¡ˆ *¡ˆ +¡dS)Nrr!r"r#r$Úcheckr&r'r(Z module_pathÚdo_csˆ |d|¡S©Nr\rv©rxrXrBr1r2Ú$rºz ConsoleCLI.run..Úhelp_cs ˆ |¡SrG©r±©rXrBr1r2rÉ%rº)Z conn_passZ become_passZsubsetcSsg|] }|j‘qSr1rµr¶r1r1r2r¹/rºz"ConsoleCLI.run..Úlibeditzbind ^I rl_completez tab: completeú~z.ansible-console_history),rrrkrZCLIARGSrr r!r"r#r$r%r&r'r(rZ add_directoryrYÚmodulesÚsetattrZ ask_passwordsrZ _play_prereqsrrPrZ get_host_listZ list_groupsrrÚreadlineÚ__doc__Úparse_and_bindÚosÚpathrgÚ expanduserÚread_history_fileÚIOErrorÚatexitÚregisterÚwrite_history_filerWrJ)r-ZsshpassZ becomepassrÕrXrZhistfiler/rBr2rksL                   zConsoleCLI.runcs°d}| d¡rJ| dd¡}t |¡rŽtˆ||f‡fdd„ ƒt ˆ|¡}nD| d¡rŽ| dd¡}t |¡rŽtˆ||f‡fdd„ ƒt ˆ|¡}|dur¬tˆj›d|›d ƒ‚|S) z[ handle not found to populate dynamically a module function if module matching name exists NrÆr]csˆ |d|¡SrÇrvrÈrBr1r2rÉIrºz(ConsoleCLI.__getattr__..rÊcs ˆ |¡SrGrËrÌrBr1r2rÉNrºz does not have a z attribute) rcÚreplacerrfrÐÚobjectÚ __getattr__ÚAttributeErrorr0)r-raÚattrrXr1rBr2rÞBs       zConsoleCLI.__getattr__)F)@Ú__name__Ú __module__Ú __qualname__rÒrarÏZ ARGUMENTSrTZCOLOR_CONSOLE_PROMPTZCOLOR_HIGHLIGHTrUrr>rCrIrJrWrYr8ruryr{rr€r~Z do_serialZ help_serialr…r†r‚rˆr‰rŠrŒr‹rrr“r”r•r–r—r˜r™ršr›rœrržr r¡rŸrMr¦Zdo_EOFZhelp_EOFr±r²rÁrÃrqrkrÞÚ __classcell__r1r1r/r2r&sr       X         s<            5