
    Vh@                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ d dlmZ d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: nosh
author:
  - "Thomas Caravia (@tacatac)"
short_description: Manage services with nosh
description:
  - Control running and enabled state for system-wide or user services.
  - BSD and Linux systems are supported.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  name:
    type: str
    required: true
    description:
      - Name of the service to manage.
  state:
    type: str
    required: false
    choices: [started, stopped, reset, restarted, reloaded]
    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 send a SIGHUP or start the service.
      - V(reset) will start or stop the service according to whether it is enabled or not.
  enabled:
    required: false
    type: bool
    description:
      - Enable or disable the service, independently of C(*.preset) file preference or running state. Mutually exclusive with
        O(preset). Will take effect prior to O(state=reset).
  preset:
    required: false
    type: bool
    description:
      - Enable or disable the service according to local preferences in C(*.preset) files. Mutually exclusive with O(enabled).
        Only has an effect if set to true. Will take effect prior to O(state=reset).
  user:
    required: false
    default: false
    type: bool
    description:
      - Run system-control talking to the calling user's service manager, rather than the system-wide service manager.
requirements:
  - A system with an active nosh service manager, see Notes for further information.
notes:
  - Information on the nosh utilities suite may be found at U(https://jdebp.eu/Softwares/nosh/).
a  
- name: Start dnscache if not running
  community.general.nosh:
    name: dnscache
    state: started

- name: Stop mpd, if running
  community.general.nosh:
    name: mpd
    state: stopped

- name: Restart unbound or start it if not already running
  community.general.nosh:
    name: unbound
    state: restarted

- name: Reload fail2ban or start it if not already running
  community.general.nosh:
    name: fail2ban
    state: reloaded

- name: Disable nsd
  community.general.nosh:
    name: nsd
    enabled: false

- name: For package installers, set nginx running state according to local enable settings, preset and reset
  community.general.nosh:
    name: nginx
    preset: true
    state: reset

- name: Reboot the host if nosh is the system manager, would need a "wait_for*" task at least, not recommended as-is
  community.general.nosh:
    name: reboot
    state: started

- name: Using conditionals with the module facts
  tasks:
    - name: Obtain information on tinydns service
      community.general.nosh:
        name: tinydns
      register: result

    - name: Fail if service not loaded
      ansible.builtin.fail:
        msg: "The {{ result.name }} service is not loaded"
      when: not result.status

    - name: Fail if service is running
      ansible.builtin.fail:
        msg: "The {{ result.name }} service is running"
      when: result.status and result.status['DaemontoolsEncoreState'] == "running"
a  
name:
  description: Name used to find the service.
  returned: success
  type: str
  sample: "sshd"
service_path:
  description: Resolved path for the service.
  returned: success
  type: str
  sample: "/var/sv/sshd"
enabled:
  description: Whether the service is enabled at system bootstrap.
  returned: success
  type: bool
  sample: true
preset:
  description: Whether the enabled status reflects the one set in the relevant C(*.preset) file.
  returned: success
  type: bool
  sample: 'False'
state:
  description: Service process run state, V(none) if the service is not loaded and will not be started.
  returned: if state option is used
  type: str
  sample: "reloaded"
status:
  description: A dictionary with the key=value pairs returned by C(system-control show-json) or V(none) if the service is
    not loaded.
  returned: success
  type: complex
  contains:
    After:
      description: []        # FIXME
      returned: success
      type: list
      sample: ["/etc/service-bundles/targets/basic", "../sshdgenkeys", "log"]
    Before:
      description: []        # FIXME
      returned: success
      type: list
      sample: ["/etc/service-bundles/targets/shutdown"]
    Conflicts:
      description: []        # FIXME
      returned: success
      type: list
      sample: []
    DaemontoolsEncoreState:
      description: []        # FIXME
      returned: success
      type: str
      sample: "running"
    DaemontoolsState:
      description: []        # FIXME
      returned: success
      type: str
      sample: "up"
    Enabled:
      description: []        # FIXME
      returned: success
      type: bool
      sample: true
    LogService:
      description: []        # FIXME
      returned: success
      type: str
      sample: "../cyclog@sshd"
    MainPID:
      description: []        # FIXME
      returned: success
      type: int
      sample: 661
    Paused:
      description: []        # FIXME
      returned: success
      type: bool
      sample: 'False'
    ReadyAfterRun:
      description: []        # FIXME
      returned: success
      type: bool
      sample: 'False'
    RemainAfterExit:
      description: []        # FIXME
      returned: success
      type: bool
      sample: 'False'
    Required-By:
      description: []        # FIXME
      returned: success
      type: list
      sample: []
    RestartExitStatusCode:
      description: []        # FIXME
      returned: success
      type: int
      sample: '0'
    RestartExitStatusNumber:
      description: []        # FIXME
      returned: success
      type: int
      sample: '0'
    RestartTimestamp:
      description: []        # FIXME
      returned: success
      type: int
      sample: 4611686019935648081
    RestartUTCTimestamp:
      description: []        # FIXME
      returned: success
      type: int
      sample: 1508260140
    RunExitStatusCode:
      description: []        # FIXME
      returned: success
      type: int
      sample: '0'
    RunExitStatusNumber:
      description: []        # FIXME
      returned: success
      type: int
      sample: '0'
    RunTimestamp:
      description: []        # FIXME
      returned: success
      type: int
      sample: 4611686019935648081
    RunUTCTimestamp:
      description: []        # FIXME
      returned: success
      type: int
      sample: 1508260140
    StartExitStatusCode:
      description: []        # FIXME
      returned: success
      type: int
      sample: 1
    StartExitStatusNumber:
      description: []        # FIXME
      returned: success
      type: int
      sample: '0'
    StartTimestamp:
      description: []        # FIXME
      returned: success
      type: int
      sample: 4611686019935648081
    StartUTCTimestamp:
      description: []        # FIXME
      returned: success
      type: int
      sample: 1508260140
    StopExitStatusCode:
      description: []        # FIXME
      returned: success
      type: int
      sample: '0'
    StopExitStatusNumber:
      description: []        # FIXME
      returned: success
      type: int
      sample: '0'
    StopTimestamp:
      description: []        # FIXME
      returned: success
      type: int
      sample: 4611686019935648081
    StopUTCTimestamp:
      description: []        # FIXME
      returned: success
      type: int
      sample: 1508260140
    Stopped-By:
      description: []        # FIXME
      returned: success
      type: list
      sample: ["/etc/service-bundles/targets/shutdown"]
    Timestamp:
      description: []        # FIXME
      returned: success
      type: int
      sample: 4611686019935648081
    UTCTimestamp:
      description: []        # FIXME
      returned: success
      type: int
      sample: 1508260140
    Want:
      description: []        # FIXME
      returned: success
      type: str
      sample: "nothing"
    Wanted-By:
      description: []        # FIXME
      returned: success
      type: list
      sample: ["/etc/service-bundles/targets/server", "/etc/service-bundles/targets/sockets"]
    Wants:
      description: []        # FIXME
      returned: success
      type: list
      sample: ["/etc/service-bundles/targets/basic", "../sshdgenkeys"]
user:
  description: Whether the user-level service manager is called.
  returned: success
  type: bool
  sample: false
N)AnsibleModule)fail_if_missing)	to_nativec                 |    | j                  dd      g}| j                  d   r|dgz   }| j                  ||z         S )Nzsystem-controlT)requireduserz--user)get_bin_pathparamsrun_command)moduleargssys_ctls      j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/nosh.pyrun_sys_ctlr   R  sH    ""#3d"CDG}}VXJ&gn--    c                     t        | d|g      \  }}}|dk7  rt        | d|d       y t        |      j                         S )Nfindr   Fhostmsg)r   r   r   strip)r   servicercouterrs        r   get_service_pathr   Y  sA     &'):;NRc	QwwF;~##%%r   c                 0    t        | d|g      \  }}}|dk(  S )Nz
is-enabledr   r   r   service_pathr   r   r   s        r   service_is_enabledr$   b  s"     ,)EFNRc7Nr   c                 x    t        | dd|g      \  }}}t        |      j                         j                  d      S )Npresetz	--dry-runenable)r   r   r   
startswithr"   s        r   service_is_preset_enabledr)   g  s9     (K)NONRcS>!,,X66r   c                 0    t        | d|g      \  }}}|dk(  S )Nz	is-loadedr   r!   r"   s        r   service_is_loadedr+   l  s"     +|)DENRc7Nr   c                     t        | d|g      \  }}}||r| j                  |       y t        j                  t	        |      j                               }||   }|S )Nz	show-jsonr   )r   	fail_jsonjsonloadsr   r   )r   r#   r   r   r   json_outstatuss          r   get_service_statusr2   q  s[     +|)DENRc
3S!::in2245,'r   c                 &    | d   t        g d      v S )NDaemontoolsEncoreState)startingstartedrunning)set)service_statuss    r   service_is_runningr:   |  s    23s;]7^^^r   c           	         |d   }|d   }| j                   d   rfd}|| j                   d   k7  rRd|d<   | j                  s5t        | ||g      \  }}}|dk7  r| j                  d|d|d||z   	       | |d<   | |d<   | j                   d   z| j                   d   rd}nd}|| j                   d   k7  rSd|d<   | j                  s5t        | ||g      \  }}}|dk7  r| j                  d|d|d||z   	       | |d<   | |d<   y
y
y
)ab  Enable or disable a service as needed.

    - 'preset' will set the enabled state according to available preset file settings.
    - 'enabled' will set the enabled state explicitly, independently of preset settings.

    These options are set to "mutually exclusive" but the explicit 'enabled' option will
    have priority if the check is bypassed.
    r&   enabledTchangedr   
Unable to 	 service : r   Nr'   disable)r   
check_moder   r-   )	r   resultr#   r&   r<   actionr   r   r   s	            r   handle_enabledrE     sT    HFYG }}X V]]8,, $F9$$!,Vfl5K!LS#7$$&R^`cfi`i)j$k#)zF8$+F9 }}Y+==#FF fmmI.. $F9$$!,Vfl5K!LS#7$$&R^`cfi`i)j$k$+F9#)zF8 / ,r   c                 (   | j                   d   |d<   | j                   d   }d}|d   }t        | |      s'|dv rd}d|d<   n|dk(  r|rd}d|d<   n{d|d<   nud|d<   not        | |      |d<   t        |d         }|dk(  r|sKd}nH|d	k(  r|rAd
}n>|dk(  r||ur5|rd
}d	|d<   n+d}d|d<   n#|dk(  r|sd}d|d<   nd}n|dk(  r|sd}d|d<   nd}|rFd|d<   | j                  s4t        | ||g      \  }}}	|dk7  r| j                  d|d|d|	       yyyy)a#  Set service running state as needed.

    Takes into account the fact that a service may not be loaded (no supervise directory) in
    which case it is 'stopped' as far as the service manager is concerned. No status information
    can be obtained and the service can only be 'started'.
    stateNr<   )r6   	restartedreloadedstartr6   resetr1   stoppedstoprH   condrestartrI   hangupTr=   r   r>   r?   r@   r   )r   r+   r2   r:   rB   r   r-   )
r   rC   r#   rG   rD   r<   r7   r   r   r   s
             r   handle_staterP     s    mmG,F7OMM'"EF YG V\288F'F7Og "+w"&w"F7O
 .flCx$VH%56I igg%#F&/F7O$F&/F7Ok! "+w&j  "+w!  y  (&,1GHNRcQw  fl\_%` a  ! r   c                  Z   t        t        t        dd      t        dg d      t        d      t        d      t        dd	      
      dddgg      } | j                  d   }d}dx}}|dd d}t        | |      }||d<   | j                  d   |d<   t	        | |      |d<   |d   t        | |      u |d<   | j                  d   | j                  d   rt        | ||       | j                  d   t        | ||       t        | |      rt        | |      |d<    | j                  di | y )NstrT)typer
   )r6   rL   rK   rH   rI   )rS   choicesbool)rS   F)rS   default)namerG   r<   r&   r   r<   r&   )argument_specsupports_check_modemutually_exclusiverW   r    )rW   r=   r1   r#   r   rG   r1    )r   dictr   r   r$   r)   rE   rP   r+   r2   	exit_json)r   r   r   r   r   rC   r#   s          r   mainr_     sO   540E+cdf%V$651
 !&12
F mmF#G	
BNC#F $FG4L *F>]]6*F6N*6<@F9i(,Efl,[[F8 }}Y+v}}X/Fvv|4 }}W)VV\2 .-flCxFvr   __main__)
__future__r   r   r   rS   __metaclass__DOCUMENTATIONEXAMPLESRETURNr.   ansible.module_utils.basicr   ansible.module_utils.servicer   +ansible.module_utils.common.text.convertersr   r   r   r$   r)   r+   r2   r:   rE   rP   r_   __name__r\   r   r   <module>rj      s    A @4l5nO
d  4 8 A.&
7

_+*\GbZ+\ zF r   