Repository: ansible/ansible

Issue:
influxdb_query fails with 'NoneType' object is not iterable
<!---
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
influxdb_query throws an error when trying to create/drop a continuous query in influxdb using influxdb_query module but the continuous query gets created/deleted successfully nevertheless.

##### 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-->
influxdb_query

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.5
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/plamen/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/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).-->
DEFAULT_STDOUT_CALLBACK(/etc/ansible/ansible.cfg) = skippy
RETRY_FILES_ENABLED(/etc/ansible/ansible.cfg) = False

##### 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.-->
Arch Linux

##### 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. -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: InfluxDB | Create InfluxDB DB
  influxdb_database:
    database_name: "mydb"

- name: InfluxDB | Create InfluxDB Retention Policy
  influxdb_retention_policy:
    database_name: "mydb"
    policy_name: "aggregated"
    default: no
    replication: 1
    duration: "400d"

- name: InfluxDB | Create InfluxDB Continuous Query
  influxdb_query:
    database_name: "mydb"
    query: "CREATE CONTINUOUS QUERY host_router ON mydb BEGIN select sum(activeQueuedInteractions) as activeQueuedInteractions, sum(loggedUsers) as loggedUsers INTO aggregated.host_router FROM router GROUP BY hostname, time(10s, 1m) END"
```
I get the same result when I try to drop the continuous query - the continuous query gets deleted but ansible fails with the same error
```yaml
- name: InfluxDB | Drop InfluxDB Continuous Queries
  influxdb_query:
    database_name: "{{ cluster_name }}"
    query: "DROP CONTINUOUS QUERY host_router   ON {{ cluster_name }}"
```



<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
The continuous query should be created successfully and Ansible should not fail.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
It looks like influxdb_query manages to create/drop the continuous query but it throws the following error:
<!--- Paste verbatim command output between quotes below -->
```
ERROR! Unexpected Exception, this is probably a bug: 'NoneType' object is not iterable
the full traceback was:

Traceback (most recent call last):
  File "/usr/bin/ansible-playbook", line 118, in <module>
    exit_code = cli.run()
  File "/usr/lib/python3.6/site-packages/ansible/cli/playbook.py", line 122, in run
    results = pbex.run()
  File "/usr/lib/python3.6/site-packages/ansible/executor/playbook_executor.py", line 159, in run
    result = self._tqm.run(play=play)
  File "/usr/lib/python3.6/site-packages/ansible/executor/task_queue_manager.py", line 289, in run
    play_return = strategy.run(iterator, play_context)
  File "/usr/lib/python3.6/site-packages/ansible/plugins/strategy/linear.py", line 321, in run
    results += self._wait_on_pending_results(iterator)
  File "/usr/lib/python3.6/site-packages/ansible/plugins/strategy/__init__.py", line 674, in _wait_on_pending_results
    results = self._process_pending_results(iterator)
  File "/usr/lib/python3.6/site-packages/ansible/plugins/strategy/__init__.py", line 117, in inner
    results = func(self, iterator, one_pass=one_pass, max_passes=max_passes)
  File "/usr/lib/python3.6/site-packages/ansible/plugins/strategy/__init__.py", line 447, in _process_pending_results
    if task_result.is_failed() or task_result.is_unreachable():
  File "/usr/lib/python3.6/site-packages/ansible/executor/task_result.py", line 59, in is_failed
    'results' in self._result and True in [True for x in self._result['results'] if 'failed_when_result' in x]:
TypeError: 'NoneType' object is not iterable

```



Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/database/influxdb/influxdb_query.py b/lib/ansible/modules/database/influxdb/influxdb_query.py
--- a/lib/ansible/modules/database/influxdb/influxdb_query.py
+++ b/lib/ansible/modules/database/influxdb/influxdb_query.py
@@ -52,11 +52,11 @@
 
 - name: Print results from the query
   debug:
-    var: connection.results
+    var: connection.query_results
 '''
 
 RETURN = '''
-results:
+query_results:
   description: Result from the query
   returned: success
   type: list
@@ -96,7 +96,7 @@ def main():
     influx = AnsibleInfluxDBRead(module)
     query = module.params.get('query')
     results = influx.read_by_query(query)
-    module.exit_json(changed=True, results=results)
+    module.exit_json(changed=True, query_results=results)
 
 
 if __name__ == '__main__':


Test patch: