o nnh2E@sddlmZdZdZdZddlZddlZddlZddlm Z ddl m Z ddl m Z Gd d d eZGd d d eZGd ddeZGdddeZGdddeZddZedkr_edSdS)) annotationsal --- module: service_facts short_description: Return service state information as fact data description: - Return service state information as fact data for various service management utilities. version_added: "2.5" requirements: ["Any of the following supported init systems: systemd, sysv, upstart, openrc, AIX SRC"] extends_documentation_fragment: - action_common_attributes - action_common_attributes.facts attributes: check_mode: support: full diff_mode: support: none facts: support: full platform: platforms: posix notes: - When accessing the RV(ansible_facts.services) facts collected by this module, it is recommended to not use "dot notation" because services can have a C(-) character in their name which would result in invalid "dot notation", such as C(ansible_facts.services.zuul-gateway). It is instead recommended to using the string value of the service name as the key in order to obtain the fact data value like C(ansible_facts.services['zuul-gateway']) - AIX SRC was added in version 2.11. author: - Adam Miller (@maxamillion) z - name: Populate service facts ansible.builtin.service_facts: - name: Print service facts ansible.builtin.debug: var: ansible_facts.services a ansible_facts: description: Facts to add to ansible_facts about the services on the system returned: always type: complex contains: services: description: States of the services with service name as key. returned: always type: list elements: dict contains: source: description: - Init system of the service. - One of V(rcctl), V(systemd), V(sysv), V(upstart), V(src). returned: always type: str sample: sysv state: description: - State of the service. - 'This commonly includes (but is not limited to) the following: V(failed), V(running), V(stopped) or V(unknown).' - Depending on the used init system additional states might be returned. returned: always type: str sample: running status: description: - State of the service. - Either V(enabled), V(disabled), V(static), V(indirect) or V(unknown). returned: systemd systems or RedHat/SUSE flavored sysvinit/upstart or OpenBSD type: str sample: enabled name: description: Name of the service. returned: always type: str sample: arp-ethers.service N) AnsibleModule)get_best_parsable_locale)is_systemd_managedc@eZdZddZdS) BaseServicecCs ||_dSN)module)selfr r H/usr/local/lib/python3.10/dist-packages/ansible/modules/service_facts.py__init__f zBaseService.__init__N)__name__ __module__ __qualname__r r r r r rd rc@s4eZdZddZddZddZddZd d Zd S) ServiceScanServicec Cs|jd|j\}}}|dkrtjdsdS|dkr&|jd||ftjdtj d}| |D]}| d}| d d krDd }nd }||d d||<q3dS)Nz%s --status-allz /etc/init.drz'Unable to query 'service' tool (%s): %sz*^\s*\[ (?P\+|\-) \]\s+(?P.+)$)flagsnamestate+runningstoppedsysvrrsource) r run_command service_pathospathexistswarnrecompileMfinditergroup) r servicesrcstdoutstderrpmatch service_name service_stater r r _list_sysvinitls z!ServiceScanService._list_sysvinitcCstd}|jd|j\}}}|dkr|jd|dS|dd}|dD]1}||}|s4q*| d} | d } | d } | d rN| d } nd} | | | d d } | || <q*dS)NzR^\s?(?P.*)\s(?P\w+)\/(?P\w+)(\,\sprocess\s(?P[0-9]+))?\s*$z%s listrz,Unable to query upstart for service data: %s  rgoalrpidupstart)rrr5r) r$r%r r initctl_pathr#replacesplitr.r()r r)r-r*r+r, real_stdoutlinemr/ service_goalr0r6payloadr r r _list_upstarts$         z ServiceScanService._list_upstartcCs~td}|jjd|jdd\}}}d}|dD] }||r#d}q|s_td}d}|dD] }||r;d}q2|rM|jjd|jdd\}}}nd |vr_|jjd |jdd\}}}|dD]X}||} | r| d } d } d } | ddkrd} |jjd|j| fdd\}}}|} |dvrd} n| } dD]}|| vr|j d| nqd } | | | dd}||| <qddS)Nz(?P.*?)\s+[0-9]:(?Pon|off)\s+[0-9]:(?Pon|off)\s+[0-9]:(?Pon|off)\s+[0-9]:(?Pon|off)\s+[0-9]:(?Pon|off)\s+[0-9]:(?Pon|off)\s+[0-9]:(?Pon|off)z%sTuse_unsafe_shellFr4z"(?P.*?)\s+(?Pon|off)z%s -l --allservicesz--listz %s --listservicerdisabledrl3onenabledz %s %s status)rr)root permissionznot in sudoerszDInsufficient permissions to query sysV service "%s" and their statesrrrstatusr) r$r%r rchkconfig_pathr:r.r(rlowerr#)r r)r-r*r+r, match_anyr<p_simpler=r/r0service_statusoutputx service_datar r r _list_rhsV      zServiceScanService._list_rhcCsi}|jjd|jdd\}}}|jjd|jdd\}}}|dD]'} | d} t| dkr1q#| d} | d } | sDd|| <q#| || <q#|dD]$} | } t| dkr]qP| d} | d } || }| || d d }||| <qPdS) Nz)%s -a -s -m 2>&1 | grep '^ ' | tr -d '[]'TrAz%s show -v 2>&1 | grep '|'r4|ropenrc)r runlevelsrr)r rrc_status_pathrc_update_pathr:lenstrip)r r)all_services_runlevelsr*r+r,rc_ustdout_ustderr_ur< line_datar/rYr0service_runlevelsrSr r r _list_openrcs,       zServiceScanService._list_openrccCsi}|jd|_|jd|_|jd|_|jd|_|jd|_|jr7|jdur7|jdur7|||jrF|jdurF|||S|jrP| ||S|jdur_|jdur_| ||S)NrC chkconfiginitctlz rc-statusz rc-update) r get_bin_pathrrLr8rZr[r1r@rTrd)r r)r r r gather_servicess"    z"ServiceScanService.gather_servicesN)rrrr1r@rTrdrhr r r r rjs 1 rc@s8eZdZegdZddZddZddZdd Zd S) SystemctlScanService)z not-foundmaskedfailedcCs t|jSr)rr )r r r r systemd_enabledrz$SystemctlScanService.systemd_enabledc Cs|jjd|dd\}}}|dkr|jd|dSdd|dDD]7}d }d }|} |jD]} | | vrB| }| d d} nq2| d }| d} | d dkrSd}| ||dd|| <q%dS)Nz-%s list-units --no-pager --type service --allTrArz%Could not list units from systemd: %scSg|]}d|vr|qSz.servicer .0svc_liner r r z9SystemctlScanService._list_from_units..r4runknownrWrVrsystemdrJ)r rr#r: BAD_STATES) r systemctl_pathr)r*r+r,r< state_val status_valfieldsbadr/r r r _list_from_unitss&   z%SystemctlScanService._list_from_unitsc Cs |jjd|dd\}}}|dkr|jd|dSdd|dDD]^}z |dd \}}WntyF|jjd |d Ynw||vrt|jjd ||fdd\}}}d } |sj|dkrj|dd} || |dd||<q%||d|j vr|||d<q%dS)Nz2%s list-unit-files --no-pager --type service --allTrArz.Could not get unit files data from systemd: %scSrmrnr ror r r rrrsz>SystemctlScanService._list_from_unit_files..r4rVz=Malformed output discovered from systemd list-unit-files: {0})msgz!%s show %s --property=ActiveStatertr3z ActiveState=rvrJrK) r rr#r: IndexError fail_jsonformatr9rstriprw) r rxr)r*r+r,r<r/rzrr r r _list_from_unit_filess&   z*SystemctlScanService._list_from_unit_filescCs@i}|r|jjdddgd}|r|||||||S)N systemctlz/usr/binz/usr/local/bin)opt_dirs)rlr rgr}r)r r)rxr r r rh,s  z$SystemctlScanService.gather_servicesN) rrr frozensetrwrlr}rrhr r r r ris   ric@r)AIXScanServicec Csi}tdkre|jd}|re|jd|\}}}|dkr+|jd||f|S|dD]4}|}t|dkr=q0|ddkrDq0|d}|d d krQd } n |d d krZd } nd} || dd||<q0|S)NAIXlssrcz%s -arz.lssrc could not retrieve service data (%s): %sr4rV Subsystemactiver inoperativerrtsrcr)platformsystemr rgrr#r:r\) r r) lssrc_pathr*r+r,r<rbr/r0r r r rh:s,      zAIXScanService.gather_servicesN)rrrrhr r r r r8rrc@s$eZdZddZddZddZdS)OpenBSDScanServicecCsdg}|jd|j|f\}}}d|vr|jd|S|dD] }|dkr*q#||q#|S)Nz%s ls %sneeds root privilegesrcctl requires root privilegesr4r3)r r rcctl_pathrMr#r:append)r cmdsvcsr*r+r,svcr r r query_rcctlWs   zOpenBSDScanService.query_rcctlc Csi}|jd|j|f\}}}d|vr|jd|Sd|}|dD]}|dks1d|vr2q'||ddd\}} | ||<q'|S) Nz %s get %srrz%s_r4r3=rW)r rrrMr#r:r9) r rinfor*r+r,undyvariablekvr r r get_infods    zOpenBSDScanService.get_infocCs(i}|jd|_|jr|dD]}|ddd||<||||q|dD] }||ddiq+|dD] }||d d iq<|d D] }||d d iqM|D]&}||ddurq||dd i||d dur||d d iq]|dD]}d||d<q|S)NrcctlallF)rrroguerFrKrGstartedrrrkrDrrT)r rgrrupdaterkeysget)r r)rr r r rhss* z"OpenBSDScanService.gather_servicesN)rrrrrrhr r r r rUs rcCsttdd}t|}t||d|_ttttf}i}|D]}||}|}|r-| |qt |dkr;tddd}ntt|dd}|j d i|dS) NT) argument_specsupports_check_mode)LANGLC_ALLrz]Failed to find any services. This can be due to privileges or some other configuration issue.)skippedr~)r)) ansible_factsr ) rdictrrun_command_environ_updaterrirrrhrr\ exit_json)r localeservice_modules all_services svc_modulesvcmodrresultsr r r mains   r__main__) __future__r DOCUMENTATIONEXAMPLESRETURNr rr$ansible.module_utils.basicr"ansible.module_utils.common.localeransible.module_utils.servicerobjectrrrirrrrr r r r s(   *    EB