
    Vh                       d dl mZ dZdZdZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZ ej                         dk7  rd dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZmZ d dlmZm Z   G d de!      Z" G d de"      Z# G d de"      Z$ G d de$      Z% G d de"      Z& G d de"      Z' G d de"      Z( G d de"      Z)d Z*e+dk(  r e*        yy)     )annotationsaG  
---
module: service
version_added: "0.1"
short_description:  Manage services
description:
    - Controls services on remote hosts. Supported init systems include BSD init,
      OpenRC, SysV, Solaris SMF, systemd, upstart.
    - This module acts as a proxy to the underlying service manager module. While all arguments will be passed to the
      underlying module, not all modules support the same arguments. This documentation only covers the minimum intersection
      of module arguments that all service manager modules support.
    - This module is a proxy for multiple more specific service manager modules
      (such as M(ansible.builtin.systemd) and M(ansible.builtin.sysvinit)).
      This allows management of a heterogeneous environment of machines without creating a specific task for
      each service manager. The module to be executed is determined by the O(use) option, which defaults to the
      service manager discovered by M(ansible.builtin.setup).  If M(ansible.builtin.setup) was not yet run, this module may run it.
    - For Windows targets, use the M(ansible.windows.win_service) module instead.
options:
    name:
        description:
        - Name of the service.
        type: str
        required: true
    state:
        description:
          - V(started)/V(stopped) are idempotent actions that will not run
            commands unless necessary.
          - V(restarted) will always bounce the service.
          - V(reloaded) will always reload.
          - At least one of O(state) and O(enabled) are required.
          - Note that V(reloaded) will start the service if it is not already started,
            even if your chosen init system wouldn't normally.
        type: str
        choices: [ reloaded, restarted, started, stopped ]
    sleep:
        description:
        - If the service is being V(restarted) then sleep this many seconds
          between the stop and start command.
        - This helps to work around badly-behaving init scripts that exit immediately
          after signaling a process to stop.
        - Not all service managers support sleep, i.e when using systemd this setting will be ignored.
        type: int
        version_added: "1.3"
    pattern:
        description:
        - If the service does not respond to the status command, name a
          substring to look for as would be found in the output of the C(ps)
          command as a stand-in for a status result.
        - If the string is found, the service will be assumed to be started.
        - While using remote hosts with systemd this setting will be ignored.
        type: str
        version_added: "0.7"
    enabled:
        description:
        - Whether the service should start on boot.
        - At least one of O(state) and O(enabled) are required.
        type: bool
    runlevel:
        description:
        - For OpenRC init scripts (e.g. Gentoo) only.
        - The runlevel that this service belongs to.
        - While using remote hosts with systemd this setting will be ignored.
        type: str
        default: default
    arguments:
        description:
        - Additional arguments provided on the command line.
        - While using remote hosts with systemd this setting will be ignored.
        type: str
        default: ''
        aliases: [ args ]
    use:
        description:
        - The service module actually uses system specific modules, normally through auto detection, this setting can force a specific module.
        - Normally it uses the value of the C(ansible_service_mgr) fact and falls back to the C(ansible.legacy.service) module when none matching is found.
        - The 'old service module' still uses autodetection and in no way does it correspond to the C(service) command.
        type: str
        default: auto
        version_added: 2.2
extends_documentation_fragment:
  -  action_common_attributes
  -  action_common_attributes.flow
attributes:
    action:
        support: full
    async:
        support: full
    bypass_host_loop:
        support: none
    check_mode:
        details: support depends on the underlying plugin invoked
        support: N/A
    diff_mode:
        details: support depends on the underlying plugin invoked
        support: N/A
    platform:
        details: The support depends on the availability for the specific plugin for each platform and if fact gathering is able to detect it
        platforms: all
notes:
    - For AIX, group subsystem names can be used.
    - The C(service) command line utility is not part of any service manager system but a convenience.
      It does not have a standard implementation across systems, and this action cannot use it directly.
      Though it might be used if found in certain circumstances, the detected system service manager is normally preferred.
seealso:
    - module: ansible.windows.win_service
author:
    - Ansible Core Team
    - Michael DeHaan
a9  
- name: Start service httpd, if not started
  ansible.builtin.service:
    name: httpd
    state: started

- name: Stop service httpd, if started
  ansible.builtin.service:
    name: httpd
    state: stopped

- name: Restart service httpd, in all cases
  ansible.builtin.service:
    name: httpd
    state: restarted

- name: Reload service httpd, in all cases
  ansible.builtin.service:
    name: httpd
    state: reloaded

- name: Enable service httpd, and not touch the state
  ansible.builtin.service:
    name: httpd
    enabled: yes

- name: Start service foo, based on running process /usr/bin/foo
  ansible.builtin.service:
    name: foo
    pattern: /usr/bin/foo
    state: started

- name: Restart network service for interface eth0
  ansible.builtin.service:
    name: network
    state: restarted
    args: eth0
#NSunOS)LooseVersion)to_bytesto_text)AnsibleModule)get_best_parsable_locale)get_platform_subclass)fail_if_missingis_systemd_managed)PY2bc                  h     e Zd ZdZdZdZ fdZd Zd Zd Z	d Z
d	 Zdd
Zd Zd Zd Zd Z xZS )ServiceaT  
    This is the generic Service manipulation class that is subclassed
    based on platform.

    A subclass should override the following action methods:-
      - get_service_tools
      - service_enable
      - get_service_status
      - service_control

    All subclasses MUST define platform and distribution (which may be None).
    GenericNc                @    t        t              }t        | |  |      S )N)r   r   super__new__)clsargskwargsnew_cls	__class__s       G/home/dcms/DCMS/lib/python3.12/site-packages/ansible/modules/service.pyr   zService.__new__   s    '0S'*733    c                   || _         |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        d| _        d | _	        d | _
        d | _        d | _        d | _        d | _        d | _        |j                  j!                  dd	      | _        d | _        d | _        d | _        d| _        y )
NnamestatesleeppatternenabledrunlevelF	arguments )moduleparamsr   r   r    r!   enabler#   changedrunningcrashedactionsvc_cmdsvc_initscriptsvc_initctl
enable_cmdgetr$   rcconf_file
rcconf_keyrcconf_value
svc_change)selfr&   s     r   __init__zService.__init__   s    MM&)	]]7+
]]7+
}}Y/mmI.j1"**;; r   c                <    | j                   j                  d       y )Nz4get_service_tools not implemented on target platformmsgr&   	fail_jsonr6   s    r   get_service_toolszService.get_service_tools   s    "XYr   c                <    | j                   j                  d       y )Nz1service_enable not implemented on target platformr9   r;   r=   s    r   service_enablezService.service_enable   s    "UVr   c                <    | j                   j                  d       y )Nz5get_service_status not implemented on target platformr9   r;   r=   s    r   get_service_statuszService.get_service_status   s    "YZr   c                <    | j                   j                  d       y )Nz2service_control not implemented on target platformr9   r;   r=   s    r   service_controlzService.service_control   s    "VWr   c           	     	   t        | j                        }t        |||      }|s| j                  j                  ||      S t	        j
                         t	        j                         }|dk(  rt	        j                  d          t	        j                  t        j                  t        j                        }|dk7  rt	        j                  |d       |dk7  rt	        j                  |d       |dk7  rt	        j                  |d       |dvrt	        j                  |       t	        j                         }|dkD  rt	        j                  d       t	        j                          t	        j                  d       t	        j                         }|dkD  rt	        j                  d       t        r#t!        |d	      }t#        j$                  |      }n:t'        |d	      }t#        j$                  |      D cg c]  }t!        |d	       }}t)        j*                  |d
t(        j,                  t(        j,                  |fd      }t/        d      }	t/        d      }
|j0                  |j2                  g}|rt5        j4                  |g |d      \  }}}||z   |z   s|j7                         n|j0                  |v rPt	        j8                  |j0                  j;                         d      }|s|j=                  |j0                         |	|z  }	|j2                  |v rPt	        j8                  |j2                  j;                         d      }|s|j=                  |j2                         |
|z  }
|r|j?                          tA        jB                  |jD                  t'        |	      t'        |
      g      }t	        jF                  d   t!        |d	             t	        j                  d          t	        j                  d       y |dk(  r| j                  jI                  d       y t	        j                  d          t	        jJ                  |d       t/        d      }	 t5        j4                  d   gg d   g      \  }}}d   |v r!t	        j8                  d   d      }|sn||z  }LtA        jL                  t'        |d	            S c c}w )N)LANGLC_ALLLC_MESSAGES)environ_updater         )r   rJ   rK   /surrogate_or_strict)errorsFc                 4    t        j                   d         S )NrJ   )osclose)pipes   r   <lambda>z)Service.execute_command.<locals>.<lambda>,  s6      EG  EM  EM  NR  ST  NU  EV r   )shellstdoutstderrenv
preexec_fnr%   i   zunable to forkr9   )'r
   r&   dictrun_commandrP   rR   forkrQ   opendevnullO_RDWRdup2_exitsetsidchdirr   r   shlexsplitr   
subprocessPopenPIPEr   rU   rV   selectpollreadfilenoremovewaitjsondumps
returncodewriter<   waitpidloads)r6   cmd	daemonizelocalelang_envpidfdcprU   rV   fdsrfdwfdefddatblobdatarR   s                     @r   execute_commandzService.execute_command   s   )$++6VFG  ;;**3x*HH wwyggi!8HHT!WRYY/BQwAQwAQwA" '')CQwIIKHHSM'')CQw s+@Akk#& c*?@JO++VYJZ[Qx*?@[[
   E*//R\RaRago  }V  WArUFrUF88QXX&C &c2sA >S#c	CQVVX-A88s?''!((//"3T:C

188,cMF88s?''!((//"3T:C

188,cMF  FFH::q||WV_gfoNODHHT!Wht4IJKHHT!WHHQKBYKK!!&6!7HHT!WJJsAR5D &tAwid1gY GS#7c>''$q'40CCKD  ::gd3HIJJY \s   8S
c                *   t        j                         dk(  rd}nd}| j                  j                  dd      }| j	                  |d|      \  }}}|dk(  r<d| _        |j                  d	      }|D ]  }| j                  |v sd
|vsd| _         y  y y )Nr   z-efauxwwpsT r   F
zpattern=)platformsystemr&   get_bin_pathr   r*   re   r!   )r6   psflagspsbinrcpsoutpserrlineslines           r   check_pszService.check_psU  s    ??'GG ((t4!11UG2LMUE7 DLKK%E <<4'Jd,B#'DL	 r   c                   | j                   r(| j                  | j                  j                  d       | j                  s| j                   dv rd| _        n8| j                  r| j                   dv rd| _        n| j                   dk(  rd| _        | j                  j
                  r+| j                  r| j                  j                  dd       y y y )	Nz@failed determining service state, possible typo of service name?r9   )reloadedstartedT)r   stopped	restartedzservice state changedr)   r:   )r   r*   r&   r<   r5   
check_mode	exit_jsonr=   s    r   check_service_changedzService.check_service_changedj  s    ::$,,.KK!!&h!i||

.E E"DO\\djj,CC"DOZZ;&"DO;;!!dooKK!!$4K!L '6!r   c                   | j                   r| j                  dv rd| _        ng| j                  s| j                  dk(  rd| _        nD| j                  dk(  rd| _        n-| j                  dk(  rd| _        n| j                  dk(  rd| _        | j                  j
                  r| j                  j                  d	d
       | j                         S d}d}d}|||fS )N)r   startr   r   stopreloadr   restartTzchanging service stater   r   r%   )r5   r   r,   r*   r&   r   r   rD   )r6   r   errouts       r   modify_service_statezService.modify_service_statew  s     ??zz[(%\\djjJ&>%y($z)&{*'{{%%%%d8P%Q'')) BCCsC<r   c                   | j                   | j                  | j                  | j                  j	                  d       d | _        | j                  d| j                  d}t        | j                   d      5 }g }|D ]  }t        j                  |d      }t        |      dk\  r]d	|d
   v rV|d
   j                  d	d      \  }}|| j                  k(  r/|j                         | j                  k(  r	d| _         n-|}d| _        |j                  |j                         dz           d d d        | j
                  j                  |       d| _        | j
                  du r	| j                  j                  r| j                  j                  dd       t        j                   j#                  | j                         }t        j                   j%                  | j                         }	t'        j(                  |d|	z        \  }
}D ]&  }t        j*                  |
|j-                                ( t        j.                  |
       | j                  j1                  || j                          y y # 1 sw Y   GxY w)NzIservice_enable_rcconf() requires rcconf_file, rcconf_key and rcconf_valuer9   ="z"
rTcommentsrJ   =r   Fr   changing service enablementr   z%s-)dirprefix)r2   r3   r4   r&   r<   r)   r]   rd   re   lenupperappendstripr   r   rP   pathdirnamebasenametempfilemkstemprr   encoderQ   atomic_move)r6   entryRCFILEnew_rc_confrclinercarraykeyvalue
rcconf_dirrcconf_base
TMP_RCCONFtmp_rcconf_files               r   service_enable_rcconfzService.service_enable_rcconf  s   #t'>$BSBSB[KK!!&q!r#0A0AB$""C( 	:FK ! :++ft<w<1$
):#*1:#3#3C#;LS%doo- ;;=D,=,==+0DL! &+F+/DL ""6<<>D#89!:		:. <<u%DL<<4{{%%%%d8U%V )9)9:J''**4+;+;<K,4,<,<TY\gTg,h)Z & 6V]]_56 HHZ  KK##OT5E5EF'  7	: 	:s   :B.I--I7)F)__name__
__module____qualname____doc__r   distributionr   r7   r>   r@   rB   rD   r   r   r   r   r   __classcell__r   s   @r   r   r      sT     HL4 2ZW[X\K|*M 84Gr   r   c                  B    e Zd ZdZdZdZd Zd Zd Zd Z	d Z
d	 Zd
 Zy)LinuxServicez
    This is the Linux Service manipulation class - it is currently supporting
    a mixture of binaries and init scripts for controlling services started at
    boot, as well as for controlling the current state.
    LinuxNc                    g d}g d}dg}t               |D ]"  } j                  j                  ||      |<   $ |D ]:  }|d j                  }t        j
                  j                  |      s4| _        <  fd} |       r' j                   _        d    _	        d    _
        n`j                  dd	      rt        j
                  j                  d
 j                  z        rd    _
        t        d       _        	 t        j                   d      } j                  j#                  dd   z        \  }	}
}|	dk(  r4|j%                  |
      }|r!t        |j'                         d          _        d    _	        nj                  dd	      rd    _	        d    _
        y  j                  rVj                  dd	      rd    _
        n9j                  dd       rd    _
        nj                  dd	      r
d    _
         j                  #t+         j                  d	 j                  d        j                  j                  dd	      r
d    _	         j                  ( j                  s j                  j-                  d       j                  dd	      rd    _        y y # t(        $ r Y Iw xY w)N)z/sbinz	/usr/sbinz/binz/usr/bin)service	chkconfigupdate-rc.d
rc-service	rc-updateinitctl	systemctlr   r   r   insservz/etc/init.d)opt_dirsrL   c                 T     j                  dd      rt        j                        S y)Nr   F)r1   r   r&   )locationr6   s   r   check_systemdz5LinuxService.get_service_tools.<locals>.check_systemd  s%     ||K/)$++66r   r   r   Fz/etc/init/%s.confz0.0.0z\(upstart (.*)\)z
%s versionr   r   r   r   r   r   hostr9   r   zbcannot find 'service' binary or init script for service,  possible typo in service name?, aborting)rZ   r&   r   r   rP   r   isfiler.   _LinuxService__systemd_unitr-   r0   r1   existsr   upstart_versionrecompiler[   searchgroups	Exceptionr   r<   r/   )r6   pathsbinaries	initpathsbinaryinitdir
initscriptr   
version_rer   rU   rV   resr   s   `            @r   r>   zLinuxService.get_service_tools  s   : U"O	6 	PF#{{777OHV	P ! 	1G$+TYY7Jww~~j)&0#	1
	 ?"&))D#K0DL&{3DO\\)U+?RUYU^U^?^0_&y1DO#/#8D ZZ(;<
%)[[%<%<\HU^L_=_%`"FF7$++F3C/;CJJLO/L, $I.DL\\,.#L1DL&{3DO  ||M51"*="9i."*9"5k51"*;"7??"DKK		vF <<HLLE$B#I.DL <<(;(;KK!!  'M!  N<<	5)'	2D *E  s   A3J; ;	KKc                    d }d }| j                   }| j                  | j                  d|      \  }}}|dk(  ry|j                  d      ry ||      r ||      S y)Nc                T    d| z   }t        j                  |t         j                        S )Nz/etc/init.d/)rP   accessX_OK)r   scripts     r   sysv_existsz=LinuxService.get_systemd_service_enabled.<locals>.sysv_exists#  s!    #d*F99VRWW--r   c                D    t        t        j                  d| z               S )N/etc/rc?.d/S??)boolglob)r   s    r   sysv_is_enabledzALinuxService.get_systemd_service_enabled.<locals>.sysv_is_enabled'  s    		"2T"9:;;r   z is-enabled r   TdisabledF)r   r   r0   
startswith)r6   r   r   service_namer   r   r   s          r   get_systemd_service_enabledz(LinuxService.get_systemd_service_enabled"  sh    	.	< **--DOOUa.cdS#7^^J'&"<00r   c                   | j                  | j                  d| j                  d      \  }}}|dk7  r-| j                  j	                  d|| j                  |fz         n0d|v r,| j                  j	                  d| j                  d|       d }g }i }|j                         D ]  }d	|v r|su|j                  d	d
      \  }}|j                         j                  d      r9|j                         j                  d      r|||<   d }d|j                  |       v|||<   d }~|j                         j                  d      rdj                  |      ||<   d }|j                         |j                          |S )Nz show ''r   z,failure %d running systemctl show for %r: %sr9   zLoadState=not-foundz.systemd could not find the requested service "z": r   rJ   {}r   )r   r0   r   r&   r<   
splitlinesre   lstripr   rstripendswithr   join)	r6   r   r   r   r   value_bufferstatus_dictr   r   s	            r   get_systemd_status_dictz$LinuxService.get_systemd_status_dict5  so    --QUQdQd.fgS#7KK!!&TXZ\`\o\oqtWu&u!v"c)KK!!aeatatvy&z!{NN$ 	+Dd{!%C!3JC
 ||~005 <<>2237/4K,"&C(//6+0C("{{}--c2+/99\+BC("$++E2##E*1	+4 r   c                   | j                         }|j                  d      dk(  rd| _        d| _        | j                  S |j                  d      dk(  rd| _        d| _        | j                  S |j                  d      5| j                  j                  d| j                         | j                  S d| _        d| _        | j                  S )NActiveStateactiveTFfailedz2No ActiveState value in systemctl show output for r9   )r	  r1   r*   r+   r&   r<   r   )r6   ds     r   get_systemd_service_statusz'LinuxService.get_systemd_service_status]  s    ((*558+  DL DL || UU=!X- DLDL || UU=!)KK!!`d`s`s&u!v || !DL DL||r   c                   | j                   r+| j                   j                  d      r| j                         S d| _        | j	                         \  }}}| j
                  r^| j                  R| j                  | j
                  d| j                  d| j                        \  }}}d|v rd| _        nd|v rd| _        | j                   rh| j                   j                  d	      rM| j                  A| j                  | j                   d| j                  d
      \  }}}	d|v | _        d|	v | _
        | j                  |dv rd| _        | j                  |j                  d      dk  r|j                         j                  | j                  j                         d      }
d|
v rd| _        nMd|
v r
d|
v| _        n?d|
v rd|
vrd| _        n/d|
v rd| _        n#d|
v rd| _        nd|
v rd| _        nd|
v rd| _        | j                  |dk(  rd| _        | j                  | j                  dk(  rd|v rd| _        | j                  S )Nr   status status r   zstop/waitingFzstart/runningTr   z statusr   r+   )rJ   rK         E   r   rJ   r%   r   runznot r   zcould not access pid filezis dead and pid file existszdead but subsys lockedzdead but pid file existsr   iptablesACCEPT)r-   r  r  r,   rD   r/   r*   r   r   r$   r+   countlowerreplace)r6   r   status_stdoutstatus_stderr
initctl_rcinitctl_status_stdoutinitctl_status_stderr	openrc_rcopenrc_status_stdoutopenrc_status_stderrcleanouts              r   rB   zLinuxService.get_service_statuso  sl   <<DLL11+>2244+/+?+?+A(M=  4GKG[G[qu  rB  rB  DH  DM  DM  OS  O]  O]  ]^  H_DJ-/D!66$ $99#<<DLL11,?DLLDXDHDXDXkokwkwy}  zC  zC  ZD  EEAI+-A$(<<DL$(<<DL <<B*:$: DL <<M$7$7$=$B$**,44TYY__5FKH!$("$*h$6H$x)?#,8$.(:$)X5$+x7$ <<B!GDL <<yyJ&8}+D  $||r   c           
        | j                   )| j                  j                  d| j                  z         d| _        d }| j                   j                  d      r&d }d}| j                  t        d      k\  r8t        j                  dt        j                  t        j                  z        }d	}n7t        j                  d
t        j                  t        j                  z        }d}|d| j                  d}|d| j                  d}t        |      5 }|j                         }	d d d        |j                  	      r| j                  j                  d       d| _        t        j                   j#                  |      rt        |      5 }
|
j                         }d d d        | j$                  r+|j                        rd| _        |j'                  d|      }nP| j$                  s,|j                        sd| _        dj)                  ||f      }nn| j$                  s
d| _        |}n	 | j                  j*                  r&| j                  j-                  | j                         | j                  r	  ||       y y | j                   j                  d      r| j$                  rd}nd}| j1                  | j                   d| j                        \  }}}d| j                  z  |v rX| j1                  | j                   d| j                         | j1                  | j                   d| j                        \  }}}| j                  |vr)| j                  j                  d| j                  z         d|z  |v rd|z  |v rd| _        y | j                   j                  d      r8| j$                  rd}nd }| j3                         }| j$                  |k(  rd| _        y | j                   j                  d!      r| j$                  rd"}nd#}| j1                  d$| j                   z        \  }}}|j5                         D ]  }|j7                  d%      \  }}|j9                         }|| j                  k7  r7t        j6                  d&|      }| j$                  r| j:                  |v rd| _        n!| j$                  s| j:                  |vrd| _         n | j$                  sd| _        | j                  sy | j                   j                  d'      rd}t=        j<                  d(| j                  z         }|rd}| j$                  |k7  r}d| _        | j$                  rd}t=        j<                  d)| j                  z         }|s| j                  j*                  s| j1                  | j                   d*| j                  d+      \  }}}|d,k7  rX|r| j                  j                  |       n9| j                  j                  |      | j                   | j                  |fz   nd }| j                  j*                  s| j1                  | j                   d*| j                  d*|      \  }}}|d,k7  rU|r| j                  j                  |       y | j                  j                  |      | j                   | j                  |fz   y d| _        y | j                   j                  d-      r| j$                  r/| j1                  | j                   d.| j                        \  }}}n.| j1                  | j                   d/| j                        \  }}}d| _        |j5                         D ]U  }| j$                  r|j?                  d0      d1k7  r	d| _         n,| j$                  r9|j?                  d2      d1k7  sNd| _         n | j                  j*                  r&| j                  j-                  | j                         | j                  sy | j$                  rb| j1                  | j                   d*| j                        \  }}}|d,k7  s|dk7  r%| j                  j                  d3|d4|d5|       |||fS | j1                  | j                   d6| j                        \  }}}|d,k7  s|dk7  r%| j                  j                  d7|d4|d5|       |||fS d| _        | j                   j                  d!      r*| j                   || j                  d*z   | j:                  z   f}nN| j                   j                  d      r| j                   || j@                  f}n| j                   | j                  |f}| j                  j*                  r&| j                  j-                  | j                         | j1                  d8|z        \  }}}|d,k7  rg|r3| j                  j                  d9|d*| j                  d:|d*|       n2| j                  j                  d;|d*| j                  d:|d*|       |||fS # 1 sw Y   	xY w# 1 sw Y   xY w# t.        $ r | j                  j                  d       Y y w xY w)<NzScannot detect command to enable service %s, typo or init system potentially unknownr9   Tr   c                ^    t        | d      }|j                  |       |j                          y )Nw)r]   rr   rQ   )	file_namefile_contentsoverride_files      r   write_to_override_filez;LinuxService.service_enable.<locals>.write_to_override_file  s(     $Y 4##M2##%r   z	/etc/initz0.6.7z^manual\s*$zmanual
z^start on manual\s*$zstart on manual
rL   z.confz	.overridez+manual stanza not supported in a .conf fileFr%   r   )r)   zCould not modify override filer   onoffz --list zchkconfig --add %sz --add z%service %s does not support chkconfigz3:%sz5:%sr   r(   disabler   adddeletez%s show|z\s+r   r   z/etc/rc?.d/K??r   z	 defaultsr   r   z -n -v z
 -n -r -v zenable servicerY   zremove servicezFailed to install service. rc: z, out: z, err: z -r zFailed to remove service. rc: z%s %s %szError when trying to z: rc=zFailure for )!r0   r&   r<   r   r)   r  r   r   r   r   MIr]   rk   r   rP   r   r   r(   subr  r   r   r   r   r   r  re   r   r#   r   findr   )r6   r,   r+  initpathmanregconfig_lineconf_file_nameoverride_file_nameconf_file_fhconf_file_contentoverride_fhoverride_file_contentsoverride_stater   r   r   service_enabledr   r   	runlevelsr"   slinksklinksr   s                           r   r@   zLinuxService.service_enable  s   ??"KK!!&{  C  H  H  'H!  I
 ??##I.&
 #H##|G'<<NBDD244K@($;RTTBDD[I1-5tyyAN5=tyy!I n% 8$0$5$5$7!8}}./%%*W%X DLww~~01,- @-8-=-=-?*@ ;;6==1G#H#'DL%+ZZ4J%KN++>T0U#'DL%)YY0F/T%UN kk#!, {{%%%%dll%; ||P*+=~N 
 ??##K0{{!11DOOUYU^U^2_`NRc#dii/36$$dootyy%QR!%!5!5Y]YbYb6c!dS#yy#%%*QTXT]T]*]%^
 #%&6/S*@$
 ??##K0{{!" #>>@O {{o-$
 ??##K0{{!!11)doo2MNNRc( )*.**S/'i+113499,HHVY7	;;4==I#=#(DLi)G#(DL) {{#(DL<<
 ??##M2GYY/$));<F{{g%#;;%F!YY'7$))'CDF!#{{55-1-A-AVZVeVegkgpgpBq-rNRc!Qw#&$(KK$9$9c$9$B$(KK$9$9c$9$BdooW[W`W`bhEi$i&F{{--%)%9%9Y]YbYbdj:k%lNRcQw KK11c1: 	 !KK11c1:dootyyZ`=aa   %
 ??##I.{{!%!5!5tX\XaXa6b!cS#!%!5!5$//[_[d[d6e!fS# DL( ;;499-=#>"#D#'DL{{tyy1A'Bb'H#'DL {{%%%%dll%;<<{{!%!5!5RVR[R[6\!]S#!GKK))hjloqt/u)wC~%!%!5!5DOOUYU^U^6_!`S#!GKK))giknps/t)vC~%
 
 ??##K0OOVTYY_t}}-LMD__%%k2OOVT-@-@ADOOTYY7D;;!!KK!!$,,!7--j4.?@S#7%%TZ\`\e\egikn*o%p%%6SWS\S\^`be*f%gC~k8 8@ @> ! PKK)).N)OPs*   h:i6	i :ii%i<;i<c                   d}| j                   }| j                  r| j                  j                  d      sU| j                  j                  d      r| j                  }| j                  d|}na| j                  d| j                  }nE| j                  }| j                  d|}n'| j                  | j
                  rd| j
                  z  }| j                  rL| j                  j                  d      r1| j                  dk(  r"| j                  r| j                  d|z  d	
       | j                  dk7  re|dk7  r+| j                  |d| j                  d|d	
      \  }}}nm| j                  | j                  d| j                  d|d	
      \  }}}n8| j                  rE| j                  j                  d      r*| j                  |d| j                  d|d	
      \  }}}n|dk7  r | j                  |ddd|d	
      \  }}}n)| j                  dd| j                  d|d	
      \  }}}| j                  rt        j                  | j                         |dk7  r | j                  |ddd|d	
      \  }	}
}n)| j                  dd| j                  d|d	
      \  }	}
}|dk7  r|	dk(  r|	}|
}|}n||	z   }||
z   }||z   }|||fS )Nr%   r   r   r   %sr   r   z%s zapTrv   r   r   r   )r$   r-   r  r   r   r.   r,   r+   r   r    time)r6   r-   r$   rc_staterU   rV   rc1stdout1stderr1rc2stdout2stderr2s               r   rD   zLinuxService.service_control  s    NN	<<<<((5<<((3"llG+/99i @I *.tyyAG ,,'+':':IF	\\!d&9&9T000G <<DLL11,?DKKSZDZ_c_k_k  G!3t D;;)#"}+/+?+?gW[WbWbdm@nz~+?+(&& ,0+?+?dkk[_[d[dfo@p  }A+?  ,B(&&\\dll33LA'+';';'SWS^S^`i<jvz';'{$Hff "}(,(<(<7TZ\e=frv(<(w%Wg )-(<(<6SWS\S\^g=htx(<(y%Wgzz

4::&"}(,(<(<7T[]f=gsw(<(x%Wg )-(<(<7TXT]T]_h=iuy(<(z%Wg axC1H  9 7* 7*&&))r   )r   r   r   r   r   r   r>   r   r	  r  rB   r@   rD    r   r   r   r     s=     HLL3\&&P$<|qfB*r   r   c                  0    e Zd ZdZdZdZd Zd Zd Zd Z	y)FreeBsdServicez
    This is the FreeBSD Service manipulation class - it uses the /etc/rc.conf
    file for controlling services started at boot and the 'service' binary to
    check status and perform direct service manipulation.
    FreeBSDNc                    | j                   j                  dd      | _        | j                  s| j                   j                  d       | j                   j                  d      | _        y )Nr   Tzunable to find service binaryr9   sysrc)r&   r   r-   r<   	sysrc_cmdr=   s    r   r>   z FreeBsdService.get_service_tools  sM    {{//	4@||KK!!&E!F11':r   c           	         | j                  | j                  d| j                  d| j                  dd      \  }}}| j                  dk(  r
d|v | _        y |dk(  rd| _        y |dk(  rd| _        y y )	Nr   	onestatuspfEnabledrJ   Fr   T)r   r-   r$   r   r*   r6   r   rU   rV   s       r   rB   z!FreeBsdService.get_service_status  sn    !114<<QUQ_Q_aeajajlw2xyFF99$.DLQw$q# r   c           	        | j                   rd| _        nd| _        g d}|D ])  }t        j                  j	                  |      s#|| _        + | j                  | j                  d| j                  d| j                  dd      \  }}}	 t        j                  |d      }s| j                  j                  d||	       |D ]"  }d
|v s|j                  d
d      \  | _        } n | j                  | j                  j                  d||	       | j                   rd| j                  | j                   d| j                        \  }}	}|dk7  r}	|	j#                         j%                         | j                  k7  rd| _        | j                  j(                  r| j                  j+                  dd       | j                  | j                   d| j                  d| j                  d      \  }}
}|dk7  r| j                  j                  d|
|	       | j                  | j                  d| j                  dd      \  }}}| j                   |dk(  k7  r'| j                  j                  d|
|	       y d| _        y y 	 | j-                         S # t        $ r Y w xY w# t        $ r | j                  j                  d       Y y w xY w)NYESNO)/etc/rc.confz/etc/rc.conf.localz/usr/local/etc/rc.confr   rcvarTr   zunable to determine rcvar)r:   rU   rV   r   rJ   z -n r   r   r   r   "zunable to set rcvar using sysrcr"   z/unable to set rcvar: sysrc did not change valueFzunable to set rcvarr9   )r(   r4   rP   r   r   r2   r   r-   r$   r   rd   re   r   r&   r<   r3   rU  r   r   r)   r   r   r   )r6   rcfilesrcfiler   rU   rV   rcvarsr_  default_rcconf_valuecurrent_rcconf_valuechange_stdoutchange_stderrcheck_stdoutcheck_stderrs                 r   r@   zFreeBsdService.service_enable   s   ;; %D $DR 	*Fww~~f%#) 	* "114<<QUQ_Q_aeajajls2tuFF	[[$7F
 KK!!&A&Y_!`  	Ee|8=C8K5!5	
 ??"KK!!&A&Y_!`>>/3/C/CRVR`R`bfbqbqDr/s,B$f Qw';$#))+113t7H7HH#;;))KK))$<Y)Z373G3GZ^ZhZhjnjyjy{  |M  |M  IN  4O0M=7KK)).OXen{)| 261E1ETXT`T`bfbkbkmvFw1x.L,;;27+KK))._hu  L)  M  %	 ,A1133c  		d  A%%*?%@As$   J J' 	J$#J$'%KKc           	     h   | j                   dk(  rd| _         | j                   dk(  rd| _         | j                   dk(  rd| _         | j                  | j                  d| j                  d| j                  d| j                         }| j
                  rt        j
                  | j
                         |S )Nr   onestartr   onestopr   	onereloadr   )r,   r   r-   r$   r   r    rG  )r6   rets     r   rD   zFreeBsdService.service_controlC  s    ;;'!$DK;;& #DK;;("%DK""DLL$..RVR[R[]a]h]h#ij::JJtzz"
r   )
r   r   r   r   r   r   r>   rB   r@   rD   rO  r   r   rQ  rQ    s*     HL;$AAFr   rQ  c                      e Zd ZdZdZdZd Zy)DragonFlyBsdServicez
    This is the DragonFly BSD Service manipulation class - it uses the /etc/rc.conf
    file for controlling services started at boot and the 'service' binary to
    check status and perform direct service manipulation.
    	DragonFlyNc                   | j                   rd| _        nd| _        dg}|D ])  }t        j                  j	                  |      s#|| _        + d| j                  j                  dd      z  | _        | j                         S Nr\  r]  r^  rE  -_
r(   r4   rP   r   r   r2   r   r  r3   r   r6   ra  rb  s      r   r@   z"DragonFlyBsdService.service_enable^  v    ;; %D $D!" 	*Fww~~f%#) 	* !2!23!<<))++r   )r   r   r   r   r   r   r@   rO  r   r   rp  rp  T  s     HL,r   rp  c                  <     e Zd ZdZdZdZd Zd Zd Z fdZ	 xZ
S )OpenBsdServicez
    This is the OpenBSD Service manipulation class - it uses rcctl(8) or
    /etc/rc.d scripts for service control. Enabling a service is
    only supported if rcctl is present.
    OpenBSDNc                D   | j                   j                  d      | _        | j                  r| j                  | _        n9d}|d| j                  }t
        j                  j                  |      r|| _        | j                  s| j                   j                  d       y y )Nrcctl	/etc/rc.drL   zunable to find svc_cmdr9   )	r&   r   r0   r-   r   rP   r   r   r<   )r6   rcdir	rc_scripts      r   r>   z OpenBsdService.get_service_toolsx  sv    ++227;????DLE#($))4Iww~~i((||KK!!&>!? r   c                8   | j                   r2| j                  | j                  ddd| j                        \  }}}n$| j                  | j                  dd      \  }}}|r| j                  j                  |       |dk(  rd| _        y |dk(  rd| _        y y )Nr   checkr9   rJ   Fr   T)r0   r   r-   r   r&   r<   r*   rZ  s       r   rB   z!OpenBsdService.get_service_status  s    ??!%!5!5DLLRY[_[d[d6e!fB!%!5!5w6W!XBKK!!f!-7 DL1WDL r   c                    | j                   r9| j                  | j                  d| j                  d| j                  d      S | j                  | j                  d| j                        S )Nz -f r   TrF  )r0   r   r-   r,   r   r=   s    r   rD   zOpenBsdService.service_control  sT    ??''t{{TXT]T](^jn'oo''dllDKK(PQQr   c           	     "   | j                   st        t        |          S | j	                  | j                   ddd| j
                  dd      \  }}}d }| j                  r|dk7  rd}n| j                  |dk7  rd}|d| _        | j                  j                  ss| j	                  | j                   d	| j
                  d
|      \  }}}|dk7  r<|r| j                  j                  |       y | j                  j                  d       y y y y )Nr   r1   r  r   r,  rJ   r-  Tz set r  r9   z%rcctl failed to modify service status)r0   r   rz  r@   r   r   r(   r)   r&   r   r<   )r6   r   rU   rV   status_actionr   s        r   r@   zOpenBsdService.service_enable  s    =??!114??TY[_[d[dfn2opFF;;Qw $[[$Qw %$DL;;))%)%9%9SWSbSbdhdmdmo|:}%~"FF7--&-9--2Y-Z	  * %r   )r   r   r   r   r   r   r>   rB   rD   r@   r   r   s   @r   rz  rz  n  s1     HL@ R[ [r   rz  c                  0    e Zd ZdZdZdZd Zd Zd Zd Z	y)NetBsdServicea>  
    This is the NetBSD Service manipulation class - it uses the /etc/rc.conf
    file for controlling services started at boot, check status and perform
    direct service manipulation. Init scripts in /etc/rc.d are used for
    controlling services (start/stop) as well as for controlling the current
    state.
    NetBSDNc                    dg}|D ]:  }|d| j                   }t        j                  j                  |      s4|| _        < | j                  s| j
                  j                  d       y y )Nr~  rL   zunable to find rc.d scriptr9   )r   rP   r   r   r.   r&   r<   )r6   r   r   r   s       r   r>   zNetBsdService.get_service_tools  sd     M	  	1G$+TYY7Jww~~j)&0#	1
 ""KK!!&B!C #r   c                   | j                   rd| _        nd| _        dg}|D ])  }t        j                  j	                  |      s#|| _        + d| j                  j                  dd      z  | _        | j                         S rs  rv  rw  s      r   r@   zNetBsdService.service_enable  rx  r   c                    d| j                   z  | _        | j                  | j                  dd      \  }}}|dk(  rd| _        y |dk(  rd| _        y y )NrE  r   rW  rJ   Fr   T)r.   r-   r   r*   rZ  s       r   rB   z NetBsdService.get_service_status  sU    d111!11T\\;2WXFF7 DL1WDL r   c                    | j                   dk(  rd| _         | j                   dk(  rd| _         d| j                  z  | _        | j                  | j                  d| j                   d      S )	Nr   rk  r   rl  rE  r   TrF  )r,   r.   r-   r   r=   s    r   rD   zNetBsdService.service_control  s^    ;;'!$DK;;& #DKd111##t||T[[$IUY#ZZr   )
r   r   r   r   r   r   r>   r@   rB   rD   rO  r   r   r  r    s)     HL	D, [r   r  c                  <    e Zd ZdZdZdZd Zd Zd Zd Z	d Z
d	 Zy)
SunOSServicez
    This is the SunOS Service manipulation class - it uses the svcadm
    command for controlling services, and svcs command for checking status.
    It also tries to be smart about taking the service out of maintenance
    state if necessary.
    r   Nc                f   | j                   j                  dd      | _        | j                  s| j                   j                  d       | j                   j                  dd      | _        | j                  s| j                   j                  d       | j                         rd| _        y d| _        y )	NsvcsTzunable to find svcs binaryr9   svcadmzunable to find svcadm binary-sr%   )r&   r   svcs_cmdr<   
svcadm_cmdsvcadm_supports_syncsvcadm_syncr=   s    r   r>   zSunOSService.get_service_tools  s    00>}}KK!!&B!C++228TBKK!!&D!E$$&#D!Dr   c                    t        dd      j                         D ]>  }t        j                  d|j	                               }|s*|j                         dk\  s> y y )Nz/etc/releaser   z \s+Oracle Solaris (\d+)\.(\d+).*)112T)r]   	readlinesr   matchr  r   )r6   r   ms      r   r  z!SunOSService.svcadm_supports_sync	  sM     -779 	D<dkkmLAQXXZ;.	r   c                L    | j                         }|dk(  rd| _        y d| _        y )NonlineTF)get_sunos_svcs_statusr*   r6   r  s     r   rB   zSunOSService.get_service_status  s(    ++- XDL DLr   c                P   | j                  | j                  d| j                        \  }}}|dk(  r;|r| j                  j	                  |       n| j                  j	                  |       |j                  d      j                  d      }|d   j                  d      d   }|S )Nr   rJ   r9   r   rY   r   )r   r  r   r&   r<   r  re   )r6   r   rU   rV   r   r  s         r   r  z"SunOSService.get_sunos_svcs_status  s    !11T]]DII2VWFF7%%&%1%%&%1d#))$/r%a( r   c                   | j                  | j                  d| j                        \  }}}|dk7  r;|r| j                  j	                  |       n| j                  j	                  |       d}d}|j                  d      D ]!  }|j                  d      sd|v rd}d	|v s d}# |xr | xs | xr |}| j                  r|ry | j                  s|sy | j                  j                  s| j                  rd
}nd}| j                  | j                  d|d| j                        \  }}}|dk7  rB|r$| j                  j	                  |       d| _
        y | j                  j	                  |       d| _
        y )Nz -l r   r9   Fr   r"   trueT	temporaryz
enable -rsz
disable -sr   )r   r  r   r&   r<   re   r   r(   r   r  r)   )	r6   r   rU   rV   r"   r  r   startup_enabledsubcmds	            r   r@   zSunOSService.service_enable(  se   !11tyy2YZFF7%%&%1%%&%1	 LL& 	%Dy)T>"G$& $I	% #49}Tg+:S);;?++{{%% {{%%!%!5!5DOOU[]a]f]f6g!hBQwKK))f)5  KK))f)5r   c                p   | j                         }| j                  dv rL|dv rH| j                  | j                  d| j                        \  }}}|dk7  r|||fS | j                         }|dv r| j
                  j                  d|z         | j                  dk(  rd}nk| j                  d	k(  rd
}nY| j                  dk(  rd| j                  z  }n:| j                  dk(  r|dk(  rd| j                  z  }n| j                  dk(  r|dk7  rd}| j                  | j                  dd| j                        S )N)r   r   r   )maintenancedegradedz clear r   z)Failed to bring service out of %s status.r9   r   zenable -rstr   zdisable -str   z
refresh %sr   r  z
restart %sr   )r  r,   r   r  r   r&   r<   r  )r6   r  r   rU   rV   r  s         r   rD   zSunOSService.service_controlZ  s.   ++- ;;88VGb=b!%!5!5tX\XaXa6b!cBQw66))//1F00KK!!&QTZ&Z![;;'!"F[[F""F[[H$!T%5%56F[[I%&H*<!T%5%56F[[I%&H*<"F##$//6499$UVVr   )r   r   r   r   r   r   r>   r  rB   r  r@   rD   rO  r   r   r  r    s3     HL" !0dWr   r  c                  0    e Zd ZdZd ZdZd Zd Zd Zd Z	y)AIXa  
    This is the AIX Service (SRC) manipulation class - it uses lssrc, startsrc, stopsrc
    and refresh for service control. Enabling a service is currently not supported.
    Would require to add an entry in the /etc/inittab file (mkitab, chitab and rmitab
    commands)
    Nc                N   | j                   j                  dd      | _        | j                  s| j                   j                  d       | j                   j                  dd      | _        | j                  s| j                   j                  d       | j                   j                  dd      | _        | j
                  s| j                   j                  d       | j                   j                  d	d      | _        | j                  s| j                   j                  d
       y y )NlssrcTzunable to find lssrc binaryr9   startsrczunable to find startsrc binarystopsrczunable to find stopsrc binaryrefreshzunable to find refresh binary)r&   r   	lssrc_cmdr<   startsrc_cmdstopsrc_cmdrefresh_cmdr=   s    r   r>   zAIX.get_service_tools  s    11'4@~~KK!!&C!D KK44ZF  KK!!&F!G;;33ItDKK!!&E!F;;33ItDKK!!&E!F  r   c                L    | j                         }|dk(  rd| _        y d| _        y )Nr  TF)get_aix_src_statusr*   r  s     r   rB   zAIX.get_service_status  s(    ((* XDL DLr   c                   | j                  | j                  d| j                        \  }}}|dk(  r| j                  | j                  d| j                        \  }}}|dk(  r<|r| j                  j	                  |       y | j                  j	                  |       y |j                         }|dd  D ]J  }|j                         d   j                         dk7  s(|j                         d   j                         } |S  d}|S |j                  d      j                  d      }|d   j                  d      d   }|S )	Nz -s rJ   z -g r9   rY   r  r   r   )	r   r  r   r&   r<   r  re   r   r  )r6   r   rU   rV   r   r   r  s          r   r  zAIX.get_aix_src_status  s6   !11PTPYPY2Z[FF7 "&!5!5DNNTXT]T]6^!_BQwKK))f)5KK))f)5 ))+"12Y &E{{}R(..0H<!&r!2!8!8!:
 &
 &F MM$'--d3E2Y__S)"-F Mr   c           	     |   | j                  d| j                  z        \  }}}|dk(  r<|r| j                  j                  |       n| j                  j                  |       n|j	                         }g }g }|dd  D ]i  }|j                         d   j                         }|j                         d   j                         }	|j                  |       |	sY|j                  |	       k | j                  |v rd}
n| j                  |v rd}
| j                  dk(  r| j                  }n| j                  dk(  r| j                  }n| j                  d	k(  r| j                  }ns| j                  d
k(  rd| j                  | j                  d
d| j                         | j                  rt        j                  | j                         | j                  }| j                  r>| j                  dv r0| j                  d| j                  d
d| j                        S | j                  d
d| j                        S )Nz%s -arJ   r9   r   r  z-gr   r   r   r   r   )r   r   z -a "z" )r   r  r&   r<   r  re   r   r   r   r,   r  r  r  r    rG  r$   )r6   r   rU   rV   r   
subsystemsr   r   	subsystemgroupsrccmd_parametersrccmds               r   rD   zAIX.service_control  s    "11'T^^2LMFF7%%&%1%%&%1%%'EJFab	 ) JJLO113	

Q--/!!),MM%() yyJ&#' f$#' ;;'!&&F[[F"%%F[[H$%%F[[I%  t/?/?AQSWS\S\!]^zz

4::&&&F>>dkk-AA''Xhjnjsjs(tuu''f>NPTPYPY(Z[[r   )
r   r   r   r   r   r   r>   rB   r  rD   rO  r   r   r  r  u  s*     HLG*!@*\r   r  c                 `   t        t        t        dd      t        dg d      t        d      t        d      t        d      t        dd	
      t        dddg            dddgg      } t        |       }| j                  d|j                  z         |j
                  r| j                  d|j
                  z         d}d}d}i }|j                  |d<   |j                          |j                  j                  d   |j                          |j                  |d<   | j                  d   !|j                  |d<    | j                  di | |j                  |d<   |j                  r|j!                          n|j#                          |j%                          |j'                         \  }}}|dk7  r.|rd|v rn'|r| j)                  |       n| j)                  |       |j                  |j*                  z  |d<   |j                  j                  d   |j                  j                  d   |d<   |j                  j                  d   s(|j#                         }|d|d<   n6|du rd|d<   n,d|d<   n&|j                  j                  d   dv rd|d<   nd|d<    | j                  di | y )NstrT)typerequired)r   r   r   r   )r  choicesint)r  r   default)r  r  r%   r   )r  r  aliases)r   r   r    r!   r"   r#   r$   r   r"   )argument_specsupports_check_moderequired_one_ofz"Service instantiated - platform %sz&Service instantiated - distribution %sr   r   r)   zJob is already runningr9   absentFr   r   )r   r   r   rO  )r	   rZ   r   debugr   r   r   r>   r&   r'   r@   r(   r)   r   r   r!   r   rB   r   r   r<   r5   )r&   r   r   r   r   resultr  s          r   mainr    s   540E+Z[E"e$f%ui8rF8D
 !!9-.F foG
LL58H8HHI=@T@TTU	
B
C
CF\\F6N  ~~Y'3 	 #NNy}}W%#OOy"6"mmF7O ""$ !!# 113NRc	Qw+s2   S )  S )'*<*<<F9~~Y'3#NN11)<y>>  )++->&F7Ou_'F7O'F7O >>  )-QQ'F7O'F7OFvr   __main__),
__future__r   DOCUMENTATIONEXAMPLESRETURNr   ro   rP   r   r   ri   rd   rf   r   rG  r   #ansible.module_utils.compat.versionr   +ansible.module_utils.common.text.convertersr   r   ansible.module_utils.basicr	   "ansible.module_utils.common.localer
   $ansible.module_utils.common.sys_infor   ansible.module_utils.servicer   r   ansible.module_utils.sixr   r   objectr   r   rQ  rp  rz  r  r  r  r  r   rO  r   r   <module>r     s    #l\%N 
   	  	      8??@ I 4 G F L +MGf MG`X*7 X*vlW l^,. ,4F[W F[R5[G 5[pCW7 CWLs\' s\rXv zF r   