Repository: ansible/ansible

Issue:
Service reports changed when nothing changed.
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
Starting fail2ban with the service module always reports `changed`

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
`service` (Actual task)

`sysvinit` (Actually used, according to debug information)

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.7.0.dev0 (devel b6e9df2065) last updated 2018/07/17 14:58:07 (GMT +200)
  config file = None
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/user/.venv-asf/ansible/lib/ansible
  executable location = /home/user/.venv-asf/bin/ansible
  python version = 3.6.5 (default, May 11 2018, 04:00:52) [GCC 8.1.0]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->
```
ANSIBLE_NOCOWS(env: ANSIBLE_NOCOWS) = True
```

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
Using Arch Linux to manage a `ubuntu:18.04` Docker container.

##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->

In a `ubuntu:18.04` container, place some file at `/var/log/auth.log` (you could make a task for that if you want; if you don't do this then the task will fail and still report `changed` (yet another issue)), then run the following tasks:

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Fail2Ban installed
  apt:
    name: fail2ban
    state: present
  become: true

- name: Fail2Ban running
  service:
    name: fail2ban
    state: started
    enabled: true
  become: true
```

Now run them again.

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Both tasks report `ok` as these tasks are idempotent.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
The first task reports `ok`, but the second task, that of starting the service reports `changed`.
<!--- Paste verbatim command output between quotes below -->
```
<instance> ESTABLISH DOCKER CONNECTION FOR USER: root
    <instance> EXEC ['/usr/bin/docker', b'exec', b'-i', 'instance', '/bin/sh', '-c', "/bin/sh -c 'echo ~ && sleep 0'"]
    <instance> EXEC ['/usr/bin/docker', b'exec', b'-i', 'instance', '/bin/sh', '-c', '/bin/sh -c \'( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1531924283.5131176-256548827571456 `" && echo ansible-tmp-1531924283.5131176-256548827571456="` echo /root/.ansible/tmp/ansible-tmp-1531924283.5131176-256548827571456 `" ) && sleep 0\'']
    Using module file /home/user/.venv-asf/ansible/lib/ansible/modules/system/sysvinit.py
    <instance> PUT /home/user/.ansible/tmp/ansible-local-14722mvwsmt__/tmp5pyr6k7z TO /root/.ansible/tmp/ansible-tmp-1531924283.5131176-256548827571456/sysvinit.py
    <instance> EXEC ['/usr/bin/docker', b'exec', b'-i', 'instance', '/bin/sh', '-c', "/bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1531924283.5131176-256548827571456/ /root/.ansible/tmp/ansible-tmp-1531924283.5131176-256548827571456/sysvinit.py && sleep 0'"]
    <instance> EXEC ['/usr/bin/docker', b'exec', b'-i', 'instance', '/bin/sh', '-c', '/bin/sh -c \'sudo -H -S -n -u root /bin/sh -c \'"\'"\'echo BECOME-SUCCESS-agrrzmahvosihatpvrdtvgshxacpafxi; /usr/bin/python /root/.ansible/tmp/ansible-tmp-1531924283.5131176-256548827571456/sysvinit.py\'"\'"\' && sleep 0\'']
    <instance> EXEC ['/usr/bin/docker', b'exec', b'-i', 'instance', '/bin/sh', '-c', "/bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1531924283.5131176-256548827571456/ > /dev/null 2>&1 && sleep 0'"]
    changed: [instance] => {
        "changed": true,
        "invocation": {
            "module_args": {
                "arguments": null,
                "daemonize": false,
                "enabled": true,
                "name": "fail2ban",
                "pattern": null,
                "runlevels": null,
                "sleep": 1,
                "state": "started"
            }
        },
        "name": "fail2ban",
        "status": {
            "enabled": {
                "changed": false,
                "rc": null,
                "stderr": null,
                "stdout": null
            },
            "started": {
                "changed": true,
                "rc": 0,
                "stderr": "",
                "stdout": " * Starting authentication failure monitor fail2ban\n   ...done.\n"
            }
        }
    }
    META: ran handlers
    META: ran handlers
```

##### Additional information
I just fixed a similar issue, that of  #42620, fixed in #42786, so I thought that perhaps I screwed up my fix. This one is slightly different however. In that issue the problem was that it wouldn't start the container. In this case the issue is actually that the service is already started, but it still reports `changed`. If you actually go into the container and run `service fail2ban status` it's obvious that the service is started just as is should.


Discussion:
Files identified in the description:
* [lib/ansible/modules/system/service.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/system/service.py)
* [lib/ansible/modules/system/sysvinit.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/system/sysvinit.py)

If these files are inaccurate, 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/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->

Resolving patch:
diff --git a/lib/ansible/modules/system/sysvinit.py b/lib/ansible/modules/system/sysvinit.py
--- a/lib/ansible/modules/system/sysvinit.py
+++ b/lib/ansible/modules/system/sysvinit.py
@@ -221,6 +221,7 @@ def main():
         if not worked:
             # hail mary
             if rc == 0:
+                is_started = True
                 worked = True
             # ps for luck, can only assure positive match
             elif get_ps(module, name):


Test patch: