check for indirect service in systemd (#76462)

* check if service is indirect status / add integration test

* Update changelogs/fragments/76453-indirect-systemd-status.yml

Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com>

* simplify indirect check / remove integration test restriction on rhel9

* add dummy indirect service for integration tests
pull/76617/head
James Livulpi 4 years ago committed by GitHub
parent 43e99e4132
commit eedf2e476c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- systemd - check if service is indirect so it gets enabled (https://github.com/ansible/ansible/issues/76453).

@ -497,11 +497,15 @@ def main():
# do we need to enable the service? # do we need to enable the service?
enabled = False 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 # check systemctl result or if it is a init script
if rc == 0: if rc == 0:
enabled = True 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: 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 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 \ if module.params['scope'] == 'system' and \

@ -2,3 +2,11 @@
file: file:
path: /etc/systemd/system/sleeper@.service path: /etc/systemd/system/sleeper@.service
state: absent state: absent
- name: remove dummy indirect service
file:
path: "/etc/systemd/system/{{item}}"
state: absent
loop:
- dummy.service
- dummy.socket

@ -119,3 +119,4 @@
- systemd_enable_ssh_2 is not changed - systemd_enable_ssh_2 is not changed
- import_tasks: test_unit_template.yml - import_tasks: test_unit_template.yml
- import_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

@ -0,0 +1,11 @@
[Unit]
Description=Dummy Server
Requires=dummy.socket
Documentation=dummy
[Service]
ExecStart=/bin/yes
StandardInput=socket
[Install]
Also=dummy.socket

@ -0,0 +1,8 @@
[Unit]
Description=Dummy Server Activation Socket
[Socket]
ListenDatagram=69
[Install]
WantedBy=sockets.target

@ -1,2 +1,3 @@
ssh_service: ssh ssh_service: ssh
sleep_bin_path: /bin/sleep sleep_bin_path: /bin/sleep
indirect_service: dummy

@ -1,2 +1,3 @@
ssh_service: sshd ssh_service: sshd
indirect_service: dummy
sleep_bin_path: /usr/bin/sleep sleep_bin_path: /usr/bin/sleep

Loading…
Cancel
Save