o nnhU@s&ddlmZddlmZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddlmZddlmZddlmZmZdd lmZdd lmZdd lmZdd lmZdd lmZm Z ddl!m"Z"ddl#m$Z$ddl%m&Z&e&Z'Gdddeej(Z)dddZ*e+dkre*dSdS)) annotations)CLIN) constants)context)option_helpers)TaskQueueManager) to_nativeto_text)boolean)parse_kv)Play) list_plugins) module_loaderfragment_loader) plugin_docs)stringc)DisplaycseZdZdZdZgZddiZejpej Z fddZ fddZ fd d Z d d Zd dZddZddZdfddZddZddZddZddZddZd d!ZeZeZd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Z d.d/Z!d0d1Z"d2d3Z#d4d5Z$d6d7Z%d8d9Z&d:d;Z'dd?Z)d@dAZ*dBdCZ+dDdEZ,dFdGZ-dHdIZ.dJdKZ/dLdMZ0dNdOZ1dPdQZ2dRdSZ3dTdUZ4dVdWZ5e4Z6e5Z7dXdYZ8dZd[Z9d\d]Z:d^d_Z;d`daZ<fdbdcZ=dddeZ>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-patternzA name of a group in the inventory, a shell-like glob selecting hosts in inventory or any combination of the two separated by commas.cstt||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__introgroupshostspatternvariable_managerloaderdict passwordscwd remote_userbecome become_user become_method check_modediffforks task_timeout collectionscmdCmdselfargs __class__>/usr/local/lib/python3.10/dist-packages/ansible/cli/console.pyrKs&zConsoleCLI.__init__cstt|jdddt|jt|jt|jt|jt |jt |jt |jt |jt |jt|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.)descepilogrz host patternall?)helpmetavardefaultnargsz--stepstep store_truez4one-step-at-a-time: confirm each task before running)destactionr6)rr init_parseropt_helpadd_runas_optionsparseradd_inventory_optionsadd_connect_optionsadd_check_optionsadd_vault_optionsadd_fork_optionsadd_module_optionsadd_basedir_optionsadd_runtask_optionsadd_tasknoplay_options add_argumentr,r.r0r1r>gs$             zConsoleCLI.init_parsercs,tt||}|jt_|j|ddd|S)NT) runas_opts fork_opts)rrpost_process_args verbositydisplayvalidate_conflicts)r,optionsr.r0r1rO|szConsoleCLI.post_process_argscCst|SN)dirrLr0r0r1 get_namesszConsoleCLI.get_namescCsVz tj|WdSty|YdSty*|d||YdSw)Nz[Ansible-console was exited])r)r*cmdloopKeyboardInterruptEOFErrorrQdo_exitrLr0r0r1rWs   zConsoleCLI.cmdloopcCsz|jpt}|j|j|_d||jt|j|jf}|j r,|j dvr,|d7}t j }n|d7}|j }t||dd|_dS)Nz%s@%s (%d)[f:%s])Nrootz# z$ T)wrap_nonvisible_chars)r getpassgetuser inventory list_hostsrselectedlenr&r!r"C COLOR_ERROR NORMAL_PROMPTrprompt)r, login_userrfcolorr0r0r1 set_promptszConsoleCLI.set_promptcCs td|jS)Nmodule)r r(rLr0r0r1 list_moduless zConsoleCLI.list_modulesFc Cs\|drdS|jstddSd}|}|dur0|^}}t|r0|}|r.d|}nd}|jr7|j}nt j rCt j dkrCt j }nd }d }z9|t j v} t t |t|| d d |jd } t d|jd| g|j|j|j|j|j|j|jd } tj| |j|jd} Wnty} ztdt| WYd } ~ dSd } ~ wwzUd |_z0t|j|j|j|j |t j d|j!d|_|j"| }t#|W|jr|j$|jr|j%n|jr|j$|jr|j%ww|d urtdWdSWd St&ytdYdSty-} z|j'dkrdd l(}t)|*tt| WYd } ~ dSd } ~ ww)z actually runs modules #Fz No host foundshellT r8minimalN) check_raw)rjr-)r=timeoutz Ansible Shellno) namer gather_factstasksr r!r"r#r$r%r()rrzUnable to build command: %s)r_rrrstdout_callbackrun_additional_callbacksrun_treer&zNo hosts foundzUser interrupted executionr)+ startswithrrQerrorsplitr find_pluginjoincallbackrcDEFAULT_LOAD_CALLBACK_PLUGINSDEFAULT_STDOUT_CALLBACK_ACTION_ALLOWS_RAW_ARGSrr r'r r!r"r#r$r%r(r loadrr Exceptionr _tqmrr_rr&rundebugcleanupcleanup_all_tmp_filesrXrP tracebackv format_exc)r,line forceshellrj module_argspossible_module possible_argscbresultrqtaskplay_dsplayerr0r0r1r8s               zConsoleCLI.defaultcCsdSrTr0rLr0r0r1 emptylineszConsoleCLI.emptylinecCs||ddS)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 TNr8r,argr0r0r1do_shells zConsoleCLI.do_shellcCtddS)Nz4You can run shell commands through the shell module.rQrLr0r0r1 help_shellzConsoleCLI.help_shellcCsj|r/zt|}Wntytd|Ynw|dkr(||_|dStddS|dS)zSet the number of forkszInvalid argument for "forks"rz(forks must be greater than or equal to 1N)int TypeErrorrQr| usage_forksr&ri)r,rr&r0r0r1do_forks s      zConsoleCLI.do_forkscCtd|dS)Nz'Set the number of forks to use per task)rQrrLr0r0r1 help_forks  zConsoleCLI.help_forkscCr)NzUsage: forks rrLr0r0r1r!rzConsoleCLI.usage_forkscCs|dvrd|_n!|s|n|d}|D]}|jdur g|_|j|q|jr9tdd|jdStddS)z.Set list of collections for 'short name' usage)rononeN,z%Collections name search is set to: %sz, z)Collections name search is using defaults)r(usage_collectionsr}appendstriprQrr)r,rr( collectionr0r0r1do_collections's   zConsoleCLI.do_collectionscCr)NzFSet the collection name search path when using short names for plugins)rQrrLr0r0r1help_collections9rzConsoleCLI.help_collectionscCr)NzgUsage: collections [, ...] Use empty quotes or "none" to reset to default. rrLr0r0r1r=rzConsoleCLI.usage_collectionsc Csn|s tddSzt|t_td|WdSttfy6}ztdt|WYd}~dSd}~ww)zSet verbosity levelzUsage: verbosity zverbosity level set to %sz)The verbosity must be a valid integer: %sN)rQrrPrr ValueErrorr|r )r,rrr0r0r1 do_verbosity@s  zConsoleCLI.do_verbositycCr)Nz@Set the verbosity level, equivalent to -v for 1 and -vvvv for 4.rrLr0r0r1help_verbosityKrzConsoleCLI.help_verbositycCsF|sd|_n|dvrd|_n|j|r||_ntd|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)rr_ get_hostsrQrirr0r0r1do_cdNs    zConsoleCLI.do_cdcCr)NzChange active host/group. )rQusage_cdrLr0r0r1help_cdbrzConsoleCLI.help_cdcCr)Nz'Usage: cd ||rrLr0r0r1rfrzConsoleCLI.usage_cdcCsX|s|jD]}t|jqdS|dkr!|jD]}t|qdStd|dS)z#List the hosts in the current grouprzInvalid option passed to "list"N)rarQrtrr| help_list)r,rhostgroupr0r0r1do_listis     zConsoleCLI.do_listcCr)NzLList the hosts in the current group or a list of groups if you add 'groups'.rrLr0r0r1rurzConsoleCLI.help_listcCs<|rt|dd|_td|j|dStddS)z$Toggle whether plays run with becomeFstrictzbecome changed to %sz.Please specify become value, e.g. `become yes`N)r r!rQrrirr0r0r1 do_becomexs  zConsoleCLI.do_becomecCr)Nz,Toggle whether the tasks are run with becomerrLr0r0r1 help_becomerzConsoleCLI.help_becomecCs$|r ||_|dStddS)z6Given a username, set the remote user plays are run byz5Please specify a remote user, e.g. `remote_user root`N)r rirQrr0r0r1do_remote_users zConsoleCLI.do_remote_usercCr)Nz2Set the user for use as login to the remote targetrrLr0r0r1help_remote_userrzConsoleCLI.help_remote_usercCs2|r||_n tdtd|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"rQrrirr0r0r1do_become_users   zConsoleCLI.do_become_usercCr)NznSet the user for use with privilege escalation (which remote user attempts to 'become' when become is enabled)rrLr0r0r1help_become_userrzConsoleCLI.help_become_usercCs<|r||_td|jdStdtd|jdS)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#rQrrr0r0r1do_become_methods  zConsoleCLI.do_become_methodcCr)NzASet the privilege escalation plugin to use when become is enabledrrLr0r0r1help_become_methodrzConsoleCLI.help_become_methodcCD|rt|dd|_td|jdStdtd|jdS)z(Toggle whether plays run with check modeFrzcheck mode changed to %sz1Please specify check mode value, e.g. `check yes`zcheck mode is currently %s.N)r r$rQrrr0r0r1do_check  zConsoleCLI.do_checkcCr)NzToggle check_mode for the tasksrrLr0r0r1 help_checkrzConsoleCLI.help_checkcCr)z"Toggle whether plays run with diffFrzdiff mode changed to %sz-Please specify a diff value , e.g. `diff yes`zdiff mode is currently %sN)r r%rQrrr0r0r1do_diffrzConsoleCLI.do_diffcCr)Nz Toggle diff output for the tasksrrLr0r0r1 help_diffrzConsoleCLI.help_diffc Cs||r8zt|}|dkrtdn||_WdSWdSttfy7}ztdt|WYd}~dSd}~ww|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)rrQr|r'rrr usage_timeout)r,rrrrr0r0r1 do_timeouts    zConsoleCLI.do_timeoutcCr)NzSet task timeout in seconds)rQrrLr0r0r1 help_timeoutrzConsoleCLI.help_timeoutcCr)NzUsage: timeout rrLr0r0r1rrzConsoleCLI.usage_timeoutcCstjddS)zExits from the consolez Ansible-console was exited. )sysstdoutwriter+r0r0r1rZs zConsoleCLI.do_exitcCr)NzLEAVE!rrLr0r0r1 help_exitrzConsoleCLI.help_exitcCs|rUt|}|rLt|t\}}}}|rCt|dtd|dD]}tdt||jd|d|ddq'dSt d|dSt d |dSdS) Nshort_descriptionz Parameters:rSz rn descriptionrzNo documentation found for %s.z<%s is not a valid command, use ? to list all valid commands.) rr~r get_docstringrrQkeysrrer|)r, module_namein_pathoca_dummy1_dummy2optr0r0r1 helpdefaults  0zConsoleCLI.helpdefaultcCstddS)NzDon't be redundant!)rQwarningrLr0r0r1 help_helprzConsoleCLI.help_helpcsb|ddtt||jdvr|j|j}n dd|j|jD}fdd|DS)Nrn)r4r\cSg|]}|jqSr0rt.0xr0r0r1 z*ConsoleCLI.complete_cd..cs.g|]}t|trt|dqSrT)rr{rsmlineoffsr0r1rs.) partitionrbrrrr_r`r,textrbegidxendidx completionsr0rr1 complete_cds  zConsoleCLI.complete_cdcs\|d|vr,|ddtt|||d}fdd|DSdS)Nrrnrcs&g|]}|r|ddqS)N=)r{rrr0r1rs&z.ConsoleCLI.completedefault..)r}rkrbrrr0rr1completedefaults zConsoleCLI.completedefaultcCs2t|}tj|tdd\}}}}t|dS)NT) is_modulerS)rr~rrrlistr)r,rrrrrrr0r0r1rs zConsoleCLI.module_argscsttd}d}tjd_j_tjd_tjd_tjd_ tjd_ tjd_ tjd_ tjd_ tjd _tjd rYtjd D] }|rXt|qO_jD]}td ||ffd d td||ffdd qa\}}||d_\___jtjdj}j_dd|D_dtjvrt dnt dt!j"#t!j"$dd}zt%|Wn t&yYnwt'(tj)|*+dS)Nrr r!r"r#checkr%r&r' module_pathdo_c|d|SNrnrrrjrLr0r1'rz ConsoleCLI.run..help_c |SrTrrjrLr0r1r( ) conn_pass become_passsubsetcSrr0rrr0r0r1r2rz"ConsoleCLI.run..libeditzbind ^I rl_completez tab: complete~z.ansible-console_history),rrrrCLIARGSrrr r!r"r#r$r%r&r'r add_directoryrkmodulessetattr ask_passwordsr _play_prereqsrr_r get_host_list list_groupsrrreadline__doc__parse_and_bindospathr expanduserread_history_fileIOErroratexitregisterwrite_history_filerirW)r,sshpass becomepassrrjrhistfiler.rLr1r sP                     zConsoleCLI.runcsd}|dr%|dd}t|r$t||ffdd t|}n"|drG|dd}t|rGt||ffdd t|}|durVtjd|d |S) z[ handle not found to populate dynamically a module function if module matching name exists NrrocrrrrrLr0r1rLrz(ConsoleCLI.__getattr__..rcrrTrrrLr0r1rQrz does not have a z attribute) r{replacerr~robject __getattr__AttributeErrorr/)r,rtattrrjr0rLr1r$Es        zConsoleCLI.__getattr__)F)@__name__ __module__ __qualname__rrtr ARGUMENTSrcCOLOR_CONSOLE_PROMPTCOLOR_HIGHLIGHTrerr>rOrVrWrirkr8rrrrrr do_serial help_serialrrrrrrrrrrrrrrrrrrrrrrrrrrZrdo_EOFhelp_EOFrrrrrrr$ __classcell__r0r0r.r1r%st      X         s>              9