Repository: ansible/ansible

Issue:
pattern parameter not recognized in service module (2.2.0 devel)
<!--- Verify first that your issue/request is not already reported in GitHub -->
##### ISSUE TYPE

<!--- Pick one below and delete the rest: -->
- Bug Report
##### ANSIBLE VERSION

<!--- Paste verbatim output from “ansible --version” between quotes below -->

```
ansible 2.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/usr/share/ansible/']

```
##### CONFIGURATION

<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
##### OS / ENVIRONMENT

<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->

N/A
##### SUMMARY

<!--- Explain the problem briefly -->

When the "pattern" parameter when using the "service" module, an error occurs indicating that it is not a valid parameter.  See error message below.

TASK [service] *****************************************************************
fatal: [massachusetts]: FAILED! => {"changed": false, "failed": true, "msg": "unsupported parameter for module: pattern"}
##### STEPS TO REPRODUCE

<!---
For bugs, show exactly how to reproduce the problem.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->

```
- hosts: trusty:xenial
  tasks:
  - service: name=ganglia-monitor pattern=/usr/sbin/gmond state=started

```

<!--- You can also paste gist.github.com links for larger files -->
##### EXPECTED RESULTS

<!--- What did you expect to happen when running the steps above? -->
##### ACTUAL RESULTS

<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->

```
TASK [service] *****************************************************************
fatal: [massachusetts]: FAILED! => {"changed": false, "failed": true, "msg": "unsupported parameter for module: pattern"}
```



Discussion:
Are you sure you don't have a service.py in your library path or something? Pattern support hasn't been removed, and works fine for me here...

Yes, a cursory glance at the code shows that 2.2.0 still intends to support the pattern option.  And this was working fine for me until I cloned and installed the 2.2.0 development branch.  I just repeated the process now using the following steps on my RHEL 6.8 server:

rpm -e ansible
rm -rf ansible
git clone https://github.com/ansible/ansible.git
cd ansible/
git submodule update --init --recursive
make rpm
rpm -ivh rpm-build/ansible-2.2.0-0.git201607141715.1905b7c.devel.el6.noarch.rpm

And I still get the same error.  BTW, I found that the sleep option also fails with an "unsupported parameter" error.  If I have neither the sleep or pattern options, then the play succeeds.

Has the targed host systemd installed? If so, systemd module is used which does not have the patterm arg. I already discussed it on IRC that it might be an issue and we must handle it somehow either by showing a proper error message or ignoring it.

That's it!  Yes, the target host does have systemd installed.  I'll leave the ticket open as a reminder to implement one of the fixes you mentioned.


Resolving patch:
diff --git a/lib/ansible/plugins/action/service.py b/lib/ansible/plugins/action/service.py
--- a/lib/ansible/plugins/action/service.py
+++ b/lib/ansible/plugins/action/service.py
@@ -25,6 +25,10 @@ class ActionModule(ActionBase):
 
     TRANSFERS_FILES = False
 
+    UNUSED_PARAMS = {
+        'systemd': ['pattern', 'runlevels', 'sleep', 'arguments'],
+    }
+
     def run(self, tmp=None, task_vars=None):
         ''' handler for package operations '''
         if task_vars is None:
@@ -59,6 +63,12 @@ def run(self, tmp=None, task_vars=None):
             if 'state' in new_module_args and new_module_args['state'] == 'running':
                 new_module_args['state'] = 'started'
 
+            if module in self.UNUSED_PARAMS:
+                for unused in self.UNUSED_PARAMS[module]:
+                    if unused in new_module_args:
+                        del new_module_args[unused]
+                        self._display.warning('Ignoring "%s" as it is not used in "%s"' % (unused, module))
+
             self._display.vvvv("Running %s" % module)
             result.update(self._execute_module(module_name=module, module_args=new_module_args, task_vars=task_vars))
         else:


Test patch: