Repository: ansible/ansible
Pull request: note in systemd that systemctl can change behaviour due to diff order…
… of operations

fixes #80533
##### ISSUE TYPE
<!--- Pick one below and delete the rest -->

- Docs Pull Request

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below -->
modules/systemd_service

Problem statement:
systemd module result is different than calling systemctl to stop and disable
### Summary

when i use ansible systemd module, but the result is different between using systemd module and not using systemd module. 


```
 - name: using systemd module
    systemd:
      name: my.service
      state: stopped
      enabled: no
```

```
- name: using just command stop
  command: systemctl stop my.service

- name: using just command disable
  command: systemctl disable my.service
```
In Detail, my.service's killMode is process (KillMode=process), so if i stop my.service, another process in my.service cgroup  are not killed.  but when i use systemd module to stop and disable,  all process in my.service cgroup are killed 

### Issue Type

Bug Report

### Component Name

systemd

### Ansible Version

```console
$ ansible --version
ansible [core 2.11.10] 
  config file = /Users/kakao_ent/cloudms/git_ms/msg-deploy/ansible/ansible.cfg
  configured module search path = ['/Users/kakao_ent/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/kakao_ent/cloudms/git_ms/msg-deploy/ansible/env/lib/python3.7/site-packages/ansible
  ansible collection location = /Users/kakao_ent/.ansible/collections:/usr/share/ansible/collections
  executable location = env/bin/ansible
  python version = 3.7.8 (v3.7.8:4b47a5b6ba, Jun 27 2020, 04:47:50) [Clang 6.0 (clang-600.0.57)]
  jinja version = 3.1.1
  libyaml = True
```


### Configuration

```console
# if using a version older than ansible-core 2.12 you should omit the '-t all'
$ ansible-config dump --only-changed -t all
```


### OS / Environment

Ubuntu 18.04

### Steps to Reproduce

1. not working task
```
 - name: using systemd module
    systemd:
      name: my.service
      state: stopped
      enabled: no
```

2. working task
```
- name: using just command stop
  command: systemctl stop my.service

- name: using just command disable
  command: systemctl disable my.service
```

and the service file attribute have to add like this

`KillMode=process`



### Expected Results

same as summary

### Actual Results

```console
same as summary
```


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct

Hints from issue discussion:
Files identified in the description:
None

If these files are incorrect, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
@kimhongji: Greetings! Thanks for taking the time to open this issue. In order for the community to handle your issue effectively, we need a bit more information.

Here are the items we could not find in your description:
- component name

Please set the description of this issue with an appropriate template from:
https://github.com/ansible/ansible/tree/devel/.github/ISSUE_TEMPLATE

[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: issue_missing_data --->
Files identified in the description:
* [`lib/ansible/modules/systemd.py`](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/systemd.py)

If these files are incorrect, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/devel/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
First, the modules is designed to have a different behavior, mostly to add better feedback and avoid needless operations.
That said, I would expect the final result to be more or less the same.

Can you supply the `-vvv` and `ANSIBLE_DEBUG=1` output?

Also, I suspect that the difference is due to the order of operations in the module (disable first, stop 2nd).

needs_info
@bcoca 

yes.. you're right. It was because of the order of operations disable and stop.
when i changed the order like below,  it worked same as ansible systemd module. 

```
- name: using just command disable
  command: systemctl disable my.service

- name: using just command stop
  command: systemctl stop my.service
```

so.. i was wondering if there is any doc or information available regarding this matter.  I happened to come across it through this  [systemd service code](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/systemd_service.py). If it is not too much trouble, would you be able to kindly inform me if there is such a document available? Thank you.


Additionally, when I stop the service after disabling it, the systemd settings that I have configured (e.g. KillMode) do not seem to be applied. Is this behavior consistent with the ansible systemd module?
No documentation on this, this behavior was a surprise to us also. For many reasons we have made enable/disable, mask/unmask operations always run before services start/stop/restarts (or equivalents) on all service manager modules.

Specially when service managers might have a 'restart/ensure always running' facility .. like systemd, runnit and daemontools have. I might add a note to the module now that we know that systemd acts differently depending on the operation order.
@bcoca 
Thank you. If you happen to work on it, could you please share the link (doc or pr or.. etc..) about this issue?

Commit message: note in systemd that systemctl can change behaviour due to diff order of operations

Patch to review:
diff --git a/lib/ansible/modules/systemd_service.py b/lib/ansible/modules/systemd_service.py
index bad29bba67a917..1fa4aa9b89f4e4 100644
--- a/lib/ansible/modules/systemd_service.py
+++ b/lib/ansible/modules/systemd_service.py
@@ -90,6 +90,8 @@
     - Before 2.4 you always required C(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 mulitple 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.
 '''


Code review comments:
diff_hunk: @@ -90,6 +90,8 @@
     - Before 2.4 you always required C(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 mulitple properties is to first enable/disable, then mask/unmask and then deal with service state.
path: lib/ansible/modules/systemd_service.py
text: @user1:
```suggestion
    - The order of execution when having multiple properties is to first enable/disable, then mask/unmask and then deal with service state.
```