summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/systemd_service.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ansible/modules/systemd_service.py')
-rw-r--r--lib/ansible/modules/systemd_service.py53
1 files changed, 31 insertions, 22 deletions
diff --git a/lib/ansible/modules/systemd_service.py b/lib/ansible/modules/systemd_service.py
index 3580fa5e..7dec0446 100644
--- a/lib/ansible/modules/systemd_service.py
+++ b/lib/ansible/modules/systemd_service.py
@@ -25,8 +25,9 @@ options:
aliases: [ service, unit ]
state:
description:
- - C(started)/C(stopped) are idempotent actions that will not run commands unless necessary.
- C(restarted) will always bounce the unit. C(reloaded) will always reload.
+ - 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.
type: str
choices: [ reloaded, restarted, started, stopped ]
enabled:
@@ -45,7 +46,7 @@ options:
daemon_reload:
description:
- Run daemon-reload before doing any other operations, to make sure systemd has read any changes.
- - When set to C(true), runs daemon-reload even if the module does not start or stop anything.
+ - 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 ]
@@ -58,8 +59,8 @@ options:
version_added: "2.8"
scope:
description:
- - Run systemctl within a given service manager scope, either as the default system scope C(system),
- the current user's scope C(user), or the scope of all users C(global).
+ - 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."
@@ -85,59 +86,61 @@ attributes:
platform:
platforms: posix
notes:
- - Since 2.4, one of the following options is required C(state), C(enabled), C(masked), C(daemon_reload), (C(daemon_reexec) since 2.8),
- and all except C(daemon_reload) and (C(daemon_reexec) since 2.8) also require C(name).
- - Before 2.4 you always required C(name).
+ - Since 2.4, one of the following options is required O(state), O(enabled), O(masked), O(daemon_reload), (O(daemon_reexec) since 2.8),
+ and all except O(daemon_reload) and (O(daemon_reexec) since 2.8) also require O(name).
+ - Before 2.4 you always required O(name).
- Globs are not supported in name, i.e 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 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.
'''
EXAMPLES = '''
- name: Make sure a service unit is running
- ansible.builtin.systemd:
+ ansible.builtin.systemd_service:
state: started
name: httpd
- name: Stop service cron on debian, if running
- ansible.builtin.systemd:
+ 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:
+ ansible.builtin.systemd_service:
state: restarted
daemon_reload: true
name: crond
- name: Reload service httpd, in all cases
- ansible.builtin.systemd:
+ ansible.builtin.systemd_service:
name: httpd.service
state: reloaded
- name: Enable service httpd and ensure it is not masked
- ansible.builtin.systemd:
+ ansible.builtin.systemd_service:
name: httpd
enabled: true
masked: no
- name: Enable a timer unit for dnf-automatic
- ansible.builtin.systemd:
+ 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:
+ ansible.builtin.systemd_service:
daemon_reload: true
- name: Just force systemd to re-execute itself (2.8 and above)
- ansible.builtin.systemd:
+ 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:
+ ansible.builtin.systemd_service:
name: myservice
state: started
scope: user
@@ -149,7 +152,7 @@ RETURN = '''
status:
description: A dictionary with the key=value pairs returned from C(systemctl show).
returned: success
- type: complex
+ type: dict
sample: {
"ActiveEnterTimestamp": "Sun 2016-05-15 18:28:49 EDT",
"ActiveEnterTimestampMonotonic": "8135942",
@@ -280,7 +283,7 @@ import os
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.facts.system.chroot import is_chroot
from ansible.module_utils.service import sysv_exists, sysv_is_enabled, fail_if_missing
-from ansible.module_utils._text import to_native
+from ansible.module_utils.common.text.converters import to_native
def is_running_service(service_status):
@@ -367,7 +370,7 @@ def main():
if os.getenv('XDG_RUNTIME_DIR') is None:
os.environ['XDG_RUNTIME_DIR'] = '/run/user/%s' % os.geteuid()
- ''' Set CLI options depending on params '''
+ # Set CLI options depending on params
# if scope is 'system' or None, we can ignore as there is no extra switch.
# The other choices match the corresponding switch
if module.params['scope'] != 'system':
@@ -391,13 +394,19 @@ def main():
if module.params['daemon_reload'] and not module.check_mode:
(rc, out, err) = module.run_command("%s daemon-reload" % (systemctl))
if rc != 0:
- module.fail_json(msg='failure %d during daemon-reload: %s' % (rc, err))
+ if is_chroot(module) or os.environ.get('SYSTEMD_OFFLINE') == '1':
+ module.warn('daemon-reload failed, but target is a chroot or systemd is offline. Continuing. Error was: %d / %s' % (rc, err))
+ else:
+ module.fail_json(msg='failure %d during daemon-reload: %s' % (rc, err))
# Run daemon-reexec
if module.params['daemon_reexec'] and not module.check_mode:
(rc, out, err) = module.run_command("%s daemon-reexec" % (systemctl))
if rc != 0:
- module.fail_json(msg='failure %d during daemon-reexec: %s' % (rc, err))
+ if is_chroot(module) or os.environ.get('SYSTEMD_OFFLINE') == '1':
+ module.warn('daemon-reexec failed, but target is a chroot or systemd is offline. Continuing. Error was: %d / %s' % (rc, err))
+ else:
+ module.fail_json(msg='failure %d during daemon-reexec: %s' % (rc, err))
if unit:
found = False