From eedf2e476c5b887d40502e080943a4e4573e339c Mon Sep 17 00:00:00 2001 From: James Livulpi Date: Thu, 23 Dec 2021 10:35:28 -0500 Subject: [PATCH] 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 * simplify indirect check / remove integration test restriction on rhel9 * add dummy indirect service for integration tests --- .../76453-indirect-systemd-status.yml | 2 + lib/ansible/modules/systemd.py | 6 ++- .../targets/systemd/handlers/main.yml | 8 ++++ .../targets/systemd/tasks/main.yml | 1 + .../systemd/tasks/test_indirect_service.yml | 37 +++++++++++++++++++ .../targets/systemd/templates/dummy.service | 11 ++++++ .../targets/systemd/templates/dummy.socket | 8 ++++ .../targets/systemd/vars/Debian.yml | 1 + .../targets/systemd/vars/default.yml | 1 + 9 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/76453-indirect-systemd-status.yml create mode 100644 test/integration/targets/systemd/tasks/test_indirect_service.yml create mode 100644 test/integration/targets/systemd/templates/dummy.service create mode 100644 test/integration/targets/systemd/templates/dummy.socket diff --git a/changelogs/fragments/76453-indirect-systemd-status.yml b/changelogs/fragments/76453-indirect-systemd-status.yml new file mode 100644 index 00000000000..5cf4d0a26d4 --- /dev/null +++ b/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). diff --git a/lib/ansible/modules/systemd.py b/lib/ansible/modules/systemd.py index e94fc660501..f6b58daa1b2 100644 --- a/lib/ansible/modules/systemd.py +++ b/lib/ansible/modules/systemd.py @@ -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 \ diff --git a/test/integration/targets/systemd/handlers/main.yml b/test/integration/targets/systemd/handlers/main.yml index 8643a2a0eeb..57469a04c57 100644 --- a/test/integration/targets/systemd/handlers/main.yml +++ b/test/integration/targets/systemd/handlers/main.yml @@ -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 diff --git a/test/integration/targets/systemd/tasks/main.yml b/test/integration/targets/systemd/tasks/main.yml index 96781eb8255..3c585e07bc8 100644 --- a/test/integration/targets/systemd/tasks/main.yml +++ b/test/integration/targets/systemd/tasks/main.yml @@ -119,3 +119,4 @@ - systemd_enable_ssh_2 is not changed - import_tasks: test_unit_template.yml +- import_tasks: test_indirect_service.yml diff --git a/test/integration/targets/systemd/tasks/test_indirect_service.yml b/test/integration/targets/systemd/tasks/test_indirect_service.yml new file mode 100644 index 00000000000..fc11343e9b9 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/test/integration/targets/systemd/templates/dummy.service b/test/integration/targets/systemd/templates/dummy.service new file mode 100644 index 00000000000..f38dce10e6d --- /dev/null +++ b/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 diff --git a/test/integration/targets/systemd/templates/dummy.socket b/test/integration/targets/systemd/templates/dummy.socket new file mode 100644 index 00000000000..f23bf9b5d3d --- /dev/null +++ b/test/integration/targets/systemd/templates/dummy.socket @@ -0,0 +1,8 @@ +[Unit] +Description=Dummy Server Activation Socket + +[Socket] +ListenDatagram=69 + +[Install] +WantedBy=sockets.target \ No newline at end of file diff --git a/test/integration/targets/systemd/vars/Debian.yml b/test/integration/targets/systemd/vars/Debian.yml index 9760744d07d..613410f0771 100644 --- a/test/integration/targets/systemd/vars/Debian.yml +++ b/test/integration/targets/systemd/vars/Debian.yml @@ -1,2 +1,3 @@ ssh_service: ssh sleep_bin_path: /bin/sleep +indirect_service: dummy \ No newline at end of file diff --git a/test/integration/targets/systemd/vars/default.yml b/test/integration/targets/systemd/vars/default.yml index 57491ff0df7..0bf1f892baf 100644 --- a/test/integration/targets/systemd/vars/default.yml +++ b/test/integration/targets/systemd/vars/default.yml @@ -1,2 +1,3 @@ ssh_service: sshd +indirect_service: dummy sleep_bin_path: /usr/bin/sleep