CarlFK
(Carl Karsten)
February 22, 2025, 9:14am
1
I just came across something I find strange:
- name: Enable the enabled-runtime service using shell command
shell: systemctl enable --runtime baz
The next task:
- name: Enable enabled-runtime service
systemd:
name: baz.service
enabled: true
register: baz_test_1
is this the same workaround as in
opened 02:42AM - 03 Nov 20 UTC
closed 02:40PM - 21 May 24 UTC
needs_info
python3
module
support:core
bug
has_pr
P3
system
affects_2.10
bot_closed
##### SUMMARY
Ansible treats enabled-runtime equal to enabled, which causes pro… blems.
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME
ansible.builtin.systemd_service
systemd
https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/systemd.py#L416
https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/systemd.py#L470
https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/systemd.py#L495
##### ANSIBLE VERSION
```paste below
ansible 2.10.2
config file = ./ansible.cfg
configured module search path = ['./library']
ansible python module location = ./venv/lib/python3.8/site-packages/ansible
executable location = ./venv/bin/ansible
python version = 3.8.6 (default, Sep 30 2020, 04:00:38) [GCC 10.2.0]
```
##### CONFIGURATION
```paste below
ANSIBLE_PIPELINING(./ansible.cfg) = True
ANSIBLE_SSH_ARGS(./ansible.cfg) = -i ./.ssh.vpass -o ControlMaster=auto -o ControlPersist=30m -o ConnectionAttempts=100 -o UserKnownHostsFile=./known_hosts
```
##### OS / ENVIRONMENT
Linux pc 5.9.2-arch1-1 #1 SMP PREEMPT Thu, 29 Oct 2020 17:01:28 +0000 x86_64 GNU/Linux
##### STEPS TO REPRODUCE
1. A systemd service that is enabled only in runtime config (I don't know what causes this exactly, but that is how it is after initial boot...)
2. Than run an ansible playbook with this task:
```
- systemd:
name: systemd-networkd
enabled: yes
```
3. Run `systemctl enable systemd-networkd` and notice that it wasn't enabled before by ansible.
##### EXPECTED RESULTS
I relied upon ansible truely enabling a service using aboves task e.g. it being equivalent to `systemctl enable systemd-networkd`, which it aparently isn't.
Further down my playbook I reconfigured some other stuff and after a reboot the vm was without network, as ansible did not enable the service as it should have...
Also the documentation for the services and systemd module say:
> Whether the service should start on boot.
Which isn't satisfied in it's current state.
##### ACTUAL RESULTS
Ansible says it is in desired state and it wasn't modified, where as in fact the service won't launch at next boot at all...
!component lib/ansible/modules/systemd_service.py
CarlFK
(Carl Karsten)
February 22, 2025, 9:48am
2
I copied some and tried to run them, seems one failed:
TASK [sysmd : assert] *********************************************************************************************
task path: /home/carl/src/bug_reports/absd/ansible/roles/sysmd/tasks/test_enabled_runtime.yml:42
fatal: [pi3]: FAILED! => {
"assertion": "baz_test_1 is changed",
"changed": false,
"evaluated_to": false,
"msg": "Assertion failed"
}
PLAY RECAP ********************************************************************************************************
pi3 : ok=24 changed=6 unreachable=0 failed=1 skipped=0 rescued=0 ignored=2
I fixed it:
- name: Enable the enabled-runtime service using shell command
- shell: systemctl enable --runtime baz
+ shell: systemctl disable --runtime baz
I’m still not sure what is going on.
chris
(Chris Croome)
February 22, 2025, 9:49am
3
Doesn’t the service also need to be started?
- name: Enable and start enabled-runtime service
ansible.builtin.systemd_service:
name: baz.service
enabled: true
state: started
@CarlFK
I haven’t read all the messages on Element and I’m not quite sure what you’re trying to do, but from what I’ve seen in the systemd_service
module’s code, the --runtime
functions are not supported by the module.
You can set user
mode and enable
the service, but it will not use the --runtime
function.
user
mode is implemented by appending the appropriate flag to the systemctl
command. In the code, you’ll see this right after retrieving systemctl’s binary
So if you set scope: user
, the command becomes systemctl --user …
, which runs the service in the current user’s context.
The service enabling operation takes place as follows:
It checks the value of the enabled
parameter. If it’s true
, it sets the action to enable
, otherwise to disable
.
It compares the current enabled state with the desired state. If they differ, it marks the result as changed and executes the corresponding enable
or disable
command:
if enabled != module.params['enabled']:
result['changed'] = True
if not module.check_mode:
(rc, out, err) = module.run_command("%s %s '%s'" % (systemctl, action, unit))
if rc != 0:
module.fail_json(msg="Unable to %s service %s: %s" % (action, unit, out + err))
result['enabled'] = not enabled
For the runtime
enablement, check the section that verifies if a service is enabled. After executing:
the code inspects the output:
if rc == 0:
# https://www.freedesktop.org/software/systemd/man/systemctl.html#is-enabled%20UNIT%E2%80%A6
if out.rstrip() in (
"enabled-runtime", # transiently enabled but we're trying to set a permanent enabled
"indirect", # We've been asked to enable this unit so do so despite possible reasons
# that systemctl may have for thinking it's enabled already.
"alias"): # Let systemd handle the alias as we can't be sure what's needed.
enabled = False
else:
enabled = True
if the output is enabled-runtime
, the module does not consider the service as permanently enabled
and will attempt to change it if enabled: true
is requested. This means the module doesn’t support runtime
enablement as a permanent configuration.
1 Like