Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check for indirect service in systemd #76462

Merged
merged 14 commits into from Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/76453-indirect-systemd-status.yml
@@ -0,0 +1,2 @@
bugfixes:
- systemd - check if service is indirect so it gets enabled (https://github.com/ansible/ansible/issues/76453).
6 changes: 5 additions & 1 deletion lib/ansible/modules/systemd.py
Expand Up @@ -497,11 +497,15 @@ def main():

# do we need to enable the service?
enabled = False
(rc, out, err) = module.run_command("%s is-enabled '%s'" % (systemctl, unit))
(rc, out, err) = module.run_command("%s is-enabled '%s' -l" % (systemctl, unit))

# check systemctl result or if it is a init script
if rc == 0:
enabled = True
# Check if service is indirect and if out contains exactly 1 line of string 'indirect' it's disabled
if out.splitlines() == ["indirect"]:
enabled = False

elif rc == 1:
# if not a user or global user service and both init script and unit file exist stdout should have enabled/disabled, otherwise use rc entries
if module.params['scope'] == 'system' and \
Expand Down
8 changes: 8 additions & 0 deletions test/integration/targets/systemd/handlers/main.yml
Expand Up @@ -2,3 +2,11 @@
file:
path: /etc/systemd/system/sleeper@.service
state: absent

- name: remove dummy indirect service
file:
path: "/etc/systemd/system/{{item}}"
state: absent
loop:
- dummy.service
- dummy.socket
1 change: 1 addition & 0 deletions test/integration/targets/systemd/tasks/main.yml
Expand Up @@ -119,3 +119,4 @@
- systemd_enable_ssh_2 is not changed

- import_tasks: test_unit_template.yml
- import_tasks: test_indirect_service.yml
37 changes: 37 additions & 0 deletions test/integration/targets/systemd/tasks/test_indirect_service.yml
@@ -0,0 +1,37 @@
- name: Copy service file
template:
src: "{{item}}"
dest: "/etc/systemd/system/{{item}}"
owner: root
group: root
loop:
- dummy.service
- dummy.socket
notify: remove dummy indirect service

- name: Ensure dummy indirect service is disabled
systemd:
name: "{{indirect_service}}"
enabled: false
register: dummy_disabled

- assert:
that:
- dummy_disabled is not changed

- name: Enable indirect service 1
systemd:
name: '{{ indirect_service }}'
enabled: true
register: systemd_enable_dummy_indirect_1

- name: Enable indirect service 2
systemd:
name: '{{ indirect_service }}'
enabled: true
register: systemd_enable_dummy_indirect_2

- assert:
that:
- systemd_enable_dummy_indirect_1 is changed
- systemd_enable_dummy_indirect_2 is not changed
11 changes: 11 additions & 0 deletions test/integration/targets/systemd/templates/dummy.service
@@ -0,0 +1,11 @@
[Unit]
Description=Dummy Server
Requires=dummy.socket
Documentation=dummy

[Service]
ExecStart=/bin/yes
StandardInput=socket

[Install]
Also=dummy.socket
8 changes: 8 additions & 0 deletions test/integration/targets/systemd/templates/dummy.socket
@@ -0,0 +1,8 @@
[Unit]
Description=Dummy Server Activation Socket

[Socket]
ListenDatagram=69

[Install]
WantedBy=sockets.target
1 change: 1 addition & 0 deletions test/integration/targets/systemd/vars/Debian.yml
@@ -1,2 +1,3 @@
ssh_service: ssh
sleep_bin_path: /bin/sleep
indirect_service: dummy
1 change: 1 addition & 0 deletions test/integration/targets/systemd/vars/default.yml
@@ -1,2 +1,3 @@
ssh_service: sshd
indirect_service: dummy
sleep_bin_path: /usr/bin/sleep