o nnha_@sddlmZdZdZdZddlZddlmZddlm Z ddl m Z m Z m Z dd lmZd d Zd d ZddZddZddZedkrIedSdS)) annotationsa module: systemd_service author: - Ansible Core Team version_added: "2.2" short_description: Manage systemd units description: - Controls systemd units (services, timers, and so on) on remote hosts. - M(ansible.builtin.systemd) is renamed to M(ansible.builtin.systemd_service) to better reflect the scope of the module. M(ansible.builtin.systemd) is kept as an alias for backward compatibility. options: name: description: - Name of the unit. This parameter takes the name of exactly one unit to work with. - When no extension is given, it is implied to a C(.service) as systemd. - When using in a chroot environment you always need to specify the name of the unit with the extension. For example, C(crond.service). type: str aliases: [ service, unit ] state: description: - V(started)/V(stopped) are idempotent actions that will not run commands unless necessary. V(restarted) will always bounce the unit. V(reloaded) will always reload and if the service is not running at the moment of the reload, it is started. - If set, requires O(name). type: str choices: [ reloaded, restarted, started, stopped ] enabled: description: - Whether the unit should start on boot. B(At least one of state and enabled are required.) - If set, requires O(name). type: bool force: description: - Whether to override existing symlinks. type: bool version_added: 2.6 masked: description: - Whether the unit should be masked or not. A masked unit is impossible to start. - If set, requires O(name). type: bool daemon_reload: description: - Run daemon-reload before doing any other operations, to make sure systemd has read any changes. - When set to V(true), runs daemon-reload even if the module does not start or stop anything. type: bool default: no aliases: [ daemon-reload ] daemon_reexec: description: - Run daemon_reexec command before doing any other operations, the systemd manager will serialize the manager state. type: bool default: no aliases: [ daemon-reexec ] version_added: "2.8" scope: description: - Run systemctl within a given service manager scope, either as the default system scope V(system), the current user's scope V(user), or the scope of all users V(global). - "For systemd to work with 'user', the executing user must have its own instance of dbus started and accessible (systemd requirement)." - "The user dbus process is normally started during normal login, but not during the run of Ansible tasks. Otherwise you will probably get a 'Failed to connect to bus: no such file or directory' error." - The user must have access, normally given via setting the C(XDG_RUNTIME_DIR) variable, see the example below. type: str choices: [ system, user, global ] default: system version_added: "2.7" no_block: description: - Do not synchronously wait for the requested operation to finish. Enqueued job will continue without Ansible blocking on its completion. type: bool default: no version_added: "2.3" extends_documentation_fragment: action_common_attributes attributes: check_mode: support: full diff_mode: support: none platform: platforms: posix notes: - O(state), O(enabled), O(masked) requires O(name). - Before 2.4 you always required O(name). - Globs are not supported in name, in other words, C(postgres*.service). - The service names might vary by specific OS/distribution. - The order of execution when having multiple properties is to first enable/disable, then mask/unmask and then deal with the service state. It has been reported that systemctl can behave differently depending on the order of operations if you do the same manually. requirements: - A system managed by systemd. a( - name: Make sure a service unit is running ansible.builtin.systemd_service: state: started name: httpd - name: Stop service cron on debian, if running ansible.builtin.systemd_service: name: cron state: stopped - name: Restart service cron on centos, in all cases, also issue daemon-reload to pick up config changes ansible.builtin.systemd_service: state: restarted daemon_reload: true name: crond - name: Reload service httpd, in all cases ansible.builtin.systemd_service: name: httpd.service state: reloaded - name: Enable service httpd and ensure it is not masked ansible.builtin.systemd_service: name: httpd enabled: true masked: no - name: Enable a timer unit for dnf-automatic ansible.builtin.systemd_service: name: dnf-automatic.timer state: started enabled: true - name: Just force systemd to reread configs (2.4 and above) ansible.builtin.systemd_service: daemon_reload: true - name: Just force systemd to re-execute itself (2.8 and above) ansible.builtin.systemd_service: daemon_reexec: true - name: Run a user service when XDG_RUNTIME_DIR is not set on remote login ansible.builtin.systemd_service: name: myservice state: started scope: user environment: XDG_RUNTIME_DIR: "/run/user/{{ myuid }}" aB status: description: A dictionary with the key=value pairs returned from C(systemctl show). returned: success type: dict sample: { "ActiveEnterTimestamp": "Sun 2016-05-15 18:28:49 EDT", "ActiveEnterTimestampMonotonic": "8135942", "ActiveExitTimestampMonotonic": "0", "ActiveState": "active", "After": "auditd.service systemd-user-sessions.service time-sync.target systemd-journald.socket basic.target system.slice", "AllowIsolate": "no", "Before": "shutdown.target multi-user.target", "BlockIOAccounting": "no", "BlockIOWeight": "1000", "CPUAccounting": "no", "CPUSchedulingPolicy": "0", "CPUSchedulingPriority": "0", "CPUSchedulingResetOnFork": "no", "CPUShares": "1024", "CanIsolate": "no", "CanReload": "yes", "CanStart": "yes", "CanStop": "yes", "CapabilityBoundingSet": "18446744073709551615", "ConditionResult": "yes", "ConditionTimestamp": "Sun 2016-05-15 18:28:49 EDT", "ConditionTimestampMonotonic": "7902742", "Conflicts": "shutdown.target", "ControlGroup": "/system.slice/crond.service", "ControlPID": "0", "DefaultDependencies": "yes", "Delegate": "no", "Description": "Command Scheduler", "DevicePolicy": "auto", "EnvironmentFile": "/etc/sysconfig/crond (ignore_errors=no)", "ExecMainCode": "0", "ExecMainExitTimestampMonotonic": "0", "ExecMainPID": "595", "ExecMainStartTimestamp": "Sun 2016-05-15 18:28:49 EDT", "ExecMainStartTimestampMonotonic": "8134990", "ExecMainStatus": "0", "ExecReload": "{ path=/bin/kill ; argv[]=/bin/kill -HUP $MAINPID ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", "ExecStart": "{ path=/usr/sbin/crond ; argv[]=/usr/sbin/crond -n $CRONDARGS ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", "FragmentPath": "/usr/lib/systemd/system/crond.service", "GuessMainPID": "yes", "IOScheduling": "0", "Id": "crond.service", "IgnoreOnIsolate": "no", "IgnoreOnSnapshot": "no", "IgnoreSIGPIPE": "yes", "InactiveEnterTimestampMonotonic": "0", "InactiveExitTimestamp": "Sun 2016-05-15 18:28:49 EDT", "InactiveExitTimestampMonotonic": "8135942", "JobTimeoutUSec": "0", "KillMode": "process", "KillSignal": "15", "LimitAS": "18446744073709551615", "LimitCORE": "18446744073709551615", "LimitCPU": "18446744073709551615", "LimitDATA": "18446744073709551615", "LimitFSIZE": "18446744073709551615", "LimitLOCKS": "18446744073709551615", "LimitMEMLOCK": "65536", "LimitMSGQUEUE": "819200", "LimitNICE": "0", "LimitNOFILE": "4096", "LimitNPROC": "3902", "LimitRSS": "18446744073709551615", "LimitRTPRIO": "0", "LimitRTTIME": "18446744073709551615", "LimitSIGPENDING": "3902", "LimitSTACK": "18446744073709551615", "LoadState": "loaded", "MainPID": "595", "MemoryAccounting": "no", "MemoryLimit": "18446744073709551615", "MountFlags": "0", "Names": "crond.service", "NeedDaemonReload": "no", "Nice": "0", "NoNewPrivileges": "no", "NonBlocking": "no", "NotifyAccess": "none", "OOMScoreAdjust": "0", "OnFailureIsolate": "no", "PermissionsStartOnly": "no", "PrivateNetwork": "no", "PrivateTmp": "no", "RefuseManualStart": "no", "RefuseManualStop": "no", "RemainAfterExit": "no", "Requires": "basic.target", "Restart": "no", "RestartUSec": "100ms", "Result": "success", "RootDirectoryStartOnly": "no", "SameProcessGroup": "no", "SecureBits": "0", "SendSIGHUP": "no", "SendSIGKILL": "yes", "Slice": "system.slice", "StandardError": "inherit", "StandardInput": "null", "StandardOutput": "journal", "StartLimitAction": "none", "StartLimitBurst": "5", "StartLimitInterval": "10000000", "StatusErrno": "0", "StopWhenUnneeded": "no", "SubState": "running", "SyslogLevelPrefix": "yes", "SyslogPriority": "30", "TTYReset": "no", "TTYVHangup": "no", "TTYVTDisallocate": "no", "TimeoutStartUSec": "1min 30s", "TimeoutStopUSec": "1min 30s", "TimerSlackNSec": "50000", "Transient": "no", "Type": "simple", "UMask": "0022", "UnitFileState": "enabled", "WantedBy": "multi-user.target", "Wants": "system.slice", "WatchdogTimestampMonotonic": "0", "WatchdogUSec": "0", } N) AnsibleModule) is_chroot) sysv_existssysv_is_enabledfail_if_missing) to_nativecCs|dtddgvS)N ActiveStateactive activatingsetservice_statusrJ/usr/local/lib/python3.10/dist-packages/ansible/modules/systemd_service.pyis_running_service$srcCs|dtdgvS)Nr deactivatingr rrrris_deactivating_service(srcCsd|vo d|vp d|vS)N=zignoring requestzignoring commandr)outrrrrequest_was_ignored,srcCsi}g}d}|D]M}|durj||d?\}}}||v} | d@j||dA\}}}|d2|d3dB<n5gdC}| dD||f\}}}||vrd} n| dE||f\}}}|d$krd} n|j |ddF| p| }| r| s| dG||jd6durm| dD||f\}}}|d6k}||jd6krmd|dH<|jd6rMdI}ndJ}|j sm| dK|||f\}}}|d$krmt|||dLd|jdMdur|jdMr~dN}ndO}t|||dLdd }| dP||f\}}}|d$krd}|dQgks|dRgkrd }n|d9kr|jdd kr| r|dSst|rd}||dM<||jdMkrd|dH<|j s| dK|||f\}}}|d$kr|jdT||||fd| |dM<|jdUdurt|||dLd|jdU|dU<dB|d3vrd}|jdUdVkr9t|d3s8dW}n0|jdUdXkrRt|d3sOt|d3rQdY}nt|d3s\dW}n |jdUddZ}dV|dU<|rd|dH<|j s| dK|||f\}}}|d$kr|jdT|||fdnt |stj d)d*kr| d[n |jd\|d3d]|jd^i|dS)_Nstrserviceunit)typealiases)reloaded restartedstartedstopped)r/choicesbool)r/Fz daemon-reload)r/defaultr0z daemon-reexecsystem)r8userglobal)r/r7r5)r/r7) namestateenabledforcemasked daemon_reload daemon_reexecscopeno_blockT)r<r=r?r@rA)r;)r<r=r?) argument_specsupports_check_moderequired_one_of required_byr;)*?[zZThis module does not currently support using glob patterns, found '%s' in service name: %s)msg systemctlXDG_RUNTIME_DIRz /run/user/%srBz --%srCz --no-blockr>z --forcer)r;changedstatusr@z%s daemon-reloadSYSTEMD_OFFLINE1zbdaemon-reload failed, but target is a chroot or systemd is offline. Continuing. Error was: %d / %sz#failure %d during daemon-reload: %srAz%s daemon-reexeczbdaemon-reexec failed, but target is a chroot or systemd is offline. Continuing. Error was: %d / %sz#failure %d during daemon-reexec: %sz %s show '%s'rrP LoadStatez not-foundr? LoadErrorz Error loading unit file '%s': %srzFailed to parse bus message@z{unit_base}{sep}) unit_basesepz,{systemctl} list-unit-files '{unit_search}*')rL unit_searchz{systemctl} is-active '{unit}')rLr.r ) r=zenabled-runtimelinkedzlinked-runtimer?zmasked-runtimestaticindirectdisabled generated transientz%s is-enabled '%s'z%s list-unit-files '%s')check_rczPThe service (%s) is actually an init script but the system is managed by systemdrOmaskunmaskz %s %s '%s'hostr=enabledisablez%s is-enabled '%s' -lr[aliasr\zUnable to %s service %s: %sr<r3startr4stopzyTarget is a chroot or systemd is offline. This can lead to false positives or prevent the init system tools from working.zService is in unknown state)rKrPr)rdictparams fail_json get_bin_pathosgetenvgeteuidenviron check_mode run_commandrgetwarnrrr+rr partitionformatr r#r splitlinesr!rrr exit_json)moduler. globpatternrLrcrerrresultfoundis_initd is_systemd is_maskedrVrWsuffixrXvalid_enabled_statesr?actionr=rrrmainVs6                       r__main__) __future__r DOCUMENTATIONEXAMPLESRETURNrmansible.module_utils.basicr(ansible.module_utils.facts.system.chrootransible.module_utils.servicerrr+ansible.module_utils.common.text.convertersrrrrr+r__name__rrrrs& ^3   &o