Repository: ansible/ansible

Issue:
consul + Python 3.5
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.3.0.0
  configured module search path = Default w/o overrides
```

##### 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.
-->
Python3.5.2

##### SUMMARY
<!--- Explain the problem briefly -->

##### 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.
-->

Problem with validating if 'script', 'ttl', and 'http' arguments [line in code](https://github.com/ansible/ansible/blob/3164e8b561931b413ee91f537e2ea3273b0a1775/lib/ansible/modules/clustering/consul.py#L363)

In Python2 filter() returned list, but in Python3 - iterator.


<!--- Paste example playbooks or commands between quotes below -->
```yaml
 - name: registerwith consul
   consul:
     service_name: "my app"
     host: "consul.service.consul"
     service_port: 8080
     script: curl http://localhost:8080
     interval: 20s
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
New service registered with consule

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
task path: /home/wgriffiths/Development/projects/service-deployment/roles/school-portal/tasks/main.yml:73
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/clustering/consul.py
<localhost> ESTABLISH SSH CONNECTION FOR USER: ubuntu
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/clustering/consul.py
<localhost> ESTABLISH SSH CONNECTION FOR USER: ubuntu
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=2203 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -i .vagrant/machines/app01/virtualbox/private_key -o UserKnownHostsFile=/dev/null -o ControlPath=/home/wgriffiths/.ansible/cp/a37fa08fdd localhost '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=2204 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -i .vagrant/machines/app02/virtualbox/private_key -o UserKnownHostsFile=/dev/null -o ControlPath=/home/wgriffiths/.ansible/cp/f96fc6261a localhost '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
<localhost> (0, '/home/ubuntu\n', '')
<localhost> ESTABLISH SSH CONNECTION FOR USER: ubuntu
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=2204 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -i .vagrant/machines/app02/virtualbox/private_key -o UserKnownHostsFile=/dev/null -o ControlPath=/home/wgriffiths/.ansible/cp/f96fc6261a localhost '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-261055622658635 `" && echo ansible-tmp-1493070572.82-261055622658635="` echo /home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-261055622658635 `" ) && sleep 0'"'"''
<localhost> (0, '/home/ubuntu\n', '')
<localhost> ESTABLISH SSH CONNECTION FOR USER: ubuntu
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=2203 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -i .vagrant/machines/app01/virtualbox/private_key -o UserKnownHostsFile=/dev/null -o ControlPath=/home/wgriffiths/.ansible/cp/a37fa08fdd localhost '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-226439627736959 `" && echo ansible-tmp-1493070572.82-226439627736959="` echo /home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-226439627736959 `" ) && sleep 0'"'"''
<localhost> (0, 'ansible-tmp-1493070572.82-261055622658635=/home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-261055622658635\n', '')
<localhost> PUT /tmp/tmp6ydtAR TO /home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-261055622658635/consul.py
<localhost> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=2204 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -i .vagrant/machines/app02/virtualbox/private_key -o UserKnownHostsFile=/dev/null -o ControlPath=/home/wgriffiths/.ansible/cp/f96fc6261a '[localhost]'
<localhost> (0, 'ansible-tmp-1493070572.82-226439627736959=/home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-226439627736959\n', '')
<localhost> PUT /tmp/tmpZeP8XY TO /home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-226439627736959/consul.py
<localhost> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=2203 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -i .vagrant/machines/app01/virtualbox/private_key -o UserKnownHostsFile=/dev/null -o ControlPath=/home/wgriffiths/.ansible/cp/a37fa08fdd '[localhost]'
<localhost> (0, 'sftp> put /tmp/tmp6ydtAR /home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-261055622658635/consul.py\n', '')
<localhost> ESTABLISH SSH CONNECTION FOR USER: ubuntu
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=2204 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -i .vagrant/machines/app02/virtualbox/private_key -o UserKnownHostsFile=/dev/null -o ControlPath=/home/wgriffiths/.ansible/cp/f96fc6261a localhost '/bin/sh -c '"'"'chmod u+x /home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-261055622658635/ /home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-261055622658635/consul.py && sleep 0'"'"''
<localhost> (0, 'sftp> put /tmp/tmpZeP8XY /home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-226439627736959/consul.py\n', '')
<localhost> ESTABLISH SSH CONNECTION FOR USER: ubuntu
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=2203 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -i .vagrant/machines/app01/virtualbox/private_key -o UserKnownHostsFile=/dev/null -o ControlPath=/home/wgriffiths/.ansible/cp/a37fa08fdd localhost '/bin/sh -c '"'"'chmod u+x /home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-226439627736959/ /home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-226439627736959/consul.py && sleep 0'"'"''
<localhost> (0, '', '')
<localhost> ESTABLISH SSH CONNECTION FOR USER: ubuntu
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=2204 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -i .vagrant/machines/app02/virtualbox/private_key -o UserKnownHostsFile=/dev/null -o ControlPath=/home/wgriffiths/.ansible/cp/f96fc6261a -tt localhost '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-wzivoikwiwbckcgvtwkcsmzzfesemldb; /usr/bin/python3 /home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-261055622658635/consul.py; rm -rf "/home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-261055622658635/" > /dev/null 2>&1'"'"'"'"'"'"'"'"' && sleep 0'"'"''
<localhost> (0, '', '')
<localhost> ESTABLISH SSH CONNECTION FOR USER: ubuntu
<localhost> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=2203 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=ubuntu -o ConnectTimeout=10 -i .vagrant/machines/app01/virtualbox/private_key -o UserKnownHostsFile=/dev/null -o ControlPath=/home/wgriffiths/.ansible/cp/a37fa08fdd -tt localhost '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-alqpralqsmjuvdbamvmufyxnddzhxybj; /usr/bin/python3 /home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-226439627736959/consul.py; rm -rf "/home/ubuntu/.ansible/tmp/ansible-tmp-1493070572.82-226439627736959/" > /dev/null 2>&1'"'"'"'"'"'"'"'"' && sleep 0'"'"''
<localhost> (0, '\r\n{"invocation": {"module_args": {"validate_certs": true, "service_id": null, "check_id": null, "timeout": null, "service_port": 8080, "script": "curl http://localhost:8080", "http": null, "service_address": null, "ttl": null, "port": 8500, "token": null, "interval": "20s", "check_node": null, "scheme": "http", "service_name": "my app", "notes": null, "host": "consul.service.consul", "tags": null, "check_name": null, "state": "present", "check_host": null}}, "msg": "object of type \'filter\' has no len()", "failed": true}\r\n', 'Shared connection to localhost closed.\r\n')
fatal: [app02]: FAILED! => {
    "changed": false,
    "failed": true,
    "invocation": {
        "module_args": {
            "check_host": null,
            "check_id": null,
            "check_name": null,
            "check_node": null,
            "host": "consul.service.consul",
            "http": null,
            "interval": "20s",
            "notes": null,
            "port": 8500,
            "scheme": "http",
            "script": "curl http://localhost:8080",
            "service_address": null,
            "service_id": null,
            "service_name": "my app",
            "service_port": 8080,
            "state": "present",
            "tags": null,
            "timeout": null,
            "token": null,
            "ttl": null,
            "validate_certs": true
        }
    },
    "msg": "object of type 'filter' has no len()"
}
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/clustering/consul.py b/lib/ansible/modules/clustering/consul.py
--- a/lib/ansible/modules/clustering/consul.py
+++ b/lib/ansible/modules/clustering/consul.py
@@ -359,9 +359,9 @@ def get_service_by_id_or_name(consul_api, service_id_or_name):
 
 
 def parse_check(module):
-    if len(filter(None, [module.params.get('script'), module.params.get('ttl'), module.params.get('http')])) > 1:
+    if len([p for p in (module.params.get('script'), module.params.get('ttl'), module.params.get('http')) if p]) > 1:
         module.fail_json(
-            msg='check are either script, http or ttl driven, supplying more than one does not make sense')
+            msg='checks are either script, http or ttl driven, supplying more than one does not make sense')
 
     if module.params.get('check_id') or module.params.get('script') or module.params.get('ttl') or module.params.get('http'):
 


Test patch: