a ì)g€Bã@sddlmZmZmZeZddlmZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlmZddlmZddlmZddlmZddlmZmZdd lmZdd lm Z dd l!m"Z"e"ƒZ#Gd d „d eƒZ$ddd„Z%e&dkre%ƒdS)é)Úabsolute_importÚdivisionÚprint_function)ÚCLIN)Ú constants)Úcontext)Úoption_helpers)ÚAnsibleOptionsError)Ú to_nativeÚto_text)Ú module_loader)Úrun_cmd)ÚDisplaycs‚eZdZdZdZdZdZdZdddœZd d iZ d Z e d d „ƒZ ‡fdd„Z ‡fdd„Z‡fdd„Ze dd„ƒZe dd„ƒZ‡ZS)ÚPullCLIaO Used to pull a remote copy of ansible on each managed node, each set to run via cron and update playbook source via a source repository. This inverts the default *push* architecture of ansible into a *pull* architecture, which has near-limitless scaling potential. None of the CLI tools are designed to run concurrently with themselves, you should use an external scheduler and/or locking to ensure there are no clashing operations. The setup playbook can be tuned to change the cron frequency, logging locations, and parameters to ansible-pull. This is useful both for extreme scale-out as well as periodic remediation. Usage of the 'fetch' module to retrieve logs from ansible-pull runs would be an excellent way to gather and analyze remote logs from ansible-pull. z ansible-pullÚgitz local.yml)rÚ subversionÚhgÚbzrzFile does not existzFile is not readable)ééú playbook.ymlaThe name of one the YAML format files to run as an Ansible playbook.This can be a relative path within the checkout. By default, Ansible willlook for a playbook based on the host's fully-qualified domain name,on the host hostname and finally a playbook named *local.yml*.TcCsdd}tj dd¡r`tjdD]B}t|tƒr>|dd |¡7}qd|vsRtj |¡r|d|7}q|S)NÚZ inventoryFz -i '%s' ú,z -i %s ) rÚCLIARGSÚgetÚ isinstanceÚlistÚjoinÚosÚpathÚexists)Úinv_optsÚinv©r#ú4/usr/lib/python3.9/site-packages/ansible/cli/pull.pyÚ _get_inv_cliCs zPullCLI._get_inv_clicsÞtt|ƒjdddt |j¡t |j¡t |j¡t |j¡t  |j¡t  |j¡t  |j¡|jj ddddd|jj d d d d d |jj dddd d dd|jj dddddd|jj dddd d dd|jj ddddt  ¡d d!|jj d"d#d$dd%d|jj d&d'd d(d)|jj d*d+d,d-d.|jj d/d d0d d1d2|jj d3d4d5|jd6|j|jfd|jj d7d8d d d9d|jj d:d;d d dd d d?d|jj d@d dAd dBd2|jj dCtjdDd dEd2dS)Fz* create an options parser for bin/ansible z0%prog -U [options] []zDpulls playbooks from a VCS repo and executes them for the local host)ÚusageZdescÚargsz Playbook(s)rÚ*)ÚhelpÚmetavarÚnargsz--purgeFÚ store_truez!purge checkout after playbook run)ÚdefaultÚactionr)z-oz--only-if-changedÚ ifchangedz8only run the playbook if the repository has been updated)Údestr-r.r)z-sz--sleepÚsleepNz|sleep for random interval (between 0 and n number of seconds) before starting. This is a useful way to disperse git requests)r0r-r)z-fz--forceÚforcezrÚpost_process_argsr0ÚsocketÚgetfqdnrrrrEZ ANSIBLE_HOMEr Úisdirr r1ÚrandomZrandintÚintÚ ValueErrorr4r8rDÚ verbosityÚdisplayZvalidate_conflicts)rFÚoptionsÚhostnameZsecsrGr#r$rIƒs&    zPullCLI.post_process_argsc stt|ƒ ¡tj ¡}t | d¡¡t d tj ¡¡t   ¡}t   ¡}d t||| d¡d| d¡dgƒ¡}|r‚d|}nd}d}tjd dkrÀ|d d  d d „tdtjd ƒDƒ¡7}| ¡}|sèd}|dt dtj¡7}tjddkr dtjdtjdf}tjdr.|dtjd7}tjdrB|d7}tjdr`|dtjd7}tjdrt|d7}tjdrˆ|d7}tjd sª|d!7}n tjdd"krüd#tjdtjdf}tjdræ|d$tjd7}tjd sª|d%7}n®tjdd&krDd#tjdtjdf}tjdrª|d$tjd7}nftjdd'krŒdtjdtjdf}tjdrª|dtjd7}ntd(tjdd |j¡fƒ‚tjd)r¾|d*7}t tjd¡} | d+urêtd,tjdƒ‚tj tj tj d¡¡} d-| ||tjd||f} tjd.D]} | d/t | ¡7} q(tjd0rrt d1tjd0¡t  tjd0¡t !d2¡t "d3| ¡t#| d4d5\} }}| dkrÄtjd6r¾t $d7¡n| Sn$tjd8rèd9|vrèt d:¡dS| %tjd¡}|d+ur td;ƒ‚d<| ||f} tjd=rBtjd=D]}| d>|7} q.tjd?rltjd?D]}| d@|7} qXtjd.D]} | d/t | ¡7} qvtjdAr¤| dB7} tjdCrÌ| dDt&d tjdC¡ƒ7} tjdErô| dFt&d tjdE¡ƒ7} tjdGr| dHtjdG7} n | dH|7} tjdIr4| dJ7} tjdKrH| dL7} t 'tjd¡| ¡}|rn| |7} t !dM¡t !d3| ¡t#| d4d5\} }}tjdNrt 'dO¡z(t !dPtjd¡t( )tjd¡WnBt*y}z(t +dQtjdt,|ƒf¡WYd+}~n d+}~00| S)Rz! use Runner lib to do SSH things zStarting Ansible Pull at %F %Tú rÚ.rzlocalhost,%s,127.0.0.1zlocalhost,127.0.0.1z -c local rPz -%srcSsg|]}d‘qS)Úvr#)Ú.0Úxr#r#r$Ú µózPullCLI.run..z -i localhost, z-e %s zansible_python_interpreter=%sr8rzname=%s dest=%sr4r0r6z version=%sr7z accept_hostkey=yesZprivate_key_filez key_file=%sr9z verify_commit=yesr;z track_submodules=yesr5z depth=1rzrepo=%s dest=%sz revision=%sz export=yesrrz5Unsupported (%s) SCM module for pull, choices are: %sr:z force=yesNzmodule '%s' not found. z*%s/ansible %s %s -m %s -a "%s" all -l "%s"Z extra_varsz -e %sr1zSleeping for %d seconds...z0running ansible with VCS module to checkout repozEXEC: %sT)Zliver2zFUnable to update repository. Continuing with (forced) run of playbook.r/s"changed": truez%Repository has not changed, quitting.z!Could not find a playbook to run.z%s/ansible-playbook %s %sZvault_password_filesz --vault-password-file=%sZ vault_idsz --vault-id=%sZbecome_ask_passz --ask-become-passZ skip_tagsz --skip-tags "%s"Útagsz -t "%s"Zsubsetz -l "%s"r<z -Cr=z -Dz*running ansible-playbook to do actual workÚpurgeú/z removing: %szFailed to remove %s: %s)-r>rÚrunÚdatetimeÚnowrQÚstrftimerÚsysÚargvÚplatformÚnoderJrKÚsetÚsplitrrÚranger%ÚshlexÚquoteÚ executabler rDr Z find_pluginrrÚdirnameÚabspathÚtimer1ÚdebugZvvvvr ÚwarningÚselect_playbookr ÚchdirÚshutilÚrmtreeÚ ExceptionÚerrorr )rFr`reÚhostZ hostnamesZ limit_optsZ base_optsr!Z repo_optsrZbin_pathÚcmdZevÚrcZb_outZb_errÚplaybookZvault_password_fileZvault_idÚerGr#r$r^ sæ * (             ÿÿ  þ                   2z PullCLI.runcCs&tj |¡sdSt |tj¡s"dSdS)Nrrr)rrr ÚaccessÚR_OK)rr#r#r$Ú try_playbook=s  zPullCLI.try_playbookc Cspd}g}tjdr¾tjdddur¾g}tjdD]H}tj ||¡}t |¡}|dkrp| d|tj|f¡q2| |¡q2dt |ƒkršt   d |¡¡n t |ƒt tjdƒkrºd |¡}|St   ¡}tj ||d¡}tj || d¡dd¡} tj |tj¡} || | fD]>} t | ¡}|dkr2| }qNn| d| tj|f¡q|durht   d |¡¡|SdS)Nr'rz%s: %sÚ rTz.ymlrU)rrrrrrr~ÚappendÚPLAYBOOK_ERRORSÚlenrQrprJrKrgÚDEFAULT_PLAYBOOK) rrzÚerrorsZ playbooksZbookZ book_pathryZfqdnZhostpbZ shorthostpbZlocalpbZpbr#r#r$rqEs:       zPullCLI.select_playbook)Ú__name__Ú __module__Ú __qualname__Ú__doc__ÚnamerCrƒrDrZ ARGUMENTSZSKIP_INVENTORY_DEFAULTSÚ staticmethodr%r?rIr^r~rqÚ __classcell__r#r#rGr$r$s(þ 4   rcCst |¡dS)N)rZ cli_executor)r'r#r#r$ÚmainhsrŒÚ__main__)N)'Z __future__rrrr3Z __metaclass__Z ansible.clirr_rrdrMrirsrJrbrnZansiblerrErZansible.cli.argumentsrr@Zansible.errorsr Zansible.module_utils._textr r Zansible.plugins.loaderr Zansible.utils.cmd_functionsr Zansible.utils.displayrrQrrŒr…r#r#r#r$Ús4        F