From a067575cab10da106f7ee2b5bb30914aa7c18854 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Thu, 21 Dec 2017 07:03:27 +0100 Subject: [PATCH] influxdb: new module influxdb_query (#33801) --- .../database/influxdb/influxdb_query.py | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 lib/ansible/modules/database/influxdb/influxdb_query.py diff --git a/lib/ansible/modules/database/influxdb/influxdb_query.py b/lib/ansible/modules/database/influxdb/influxdb_query.py new file mode 100644 index 0000000000..fb57f6fb3c --- /dev/null +++ b/lib/ansible/modules/database/influxdb/influxdb_query.py @@ -0,0 +1,103 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright (c) 2017, René Moser +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} + + +DOCUMENTATION = ''' +--- +module: influxdb_query +short_description: Query data points from InfluxDB. +description: + - Query data points from InfluxDB. +version_added: 2.5 +author: "René Moser (@resmo)" +requirements: + - "python >= 2.6" + - "influxdb >= 0.9" +options: + query: + description: + - Query to be executed. + required: true + database_name: + description: + - Name of the database. + required: true +extends_documentation_fragment: influxdb +''' + +EXAMPLES = r''' +- name: Query connections + influxdb_query: + hostname: "{{ influxdb_ip_address }}" + database_name: "{{ influxdb_database_name }}" + query: "select mean(value) from connections" + register: connection + +- name: Query connections with tags filters + influxdb_query: + hostname: "{{ influxdb_ip_address }}" + database_name: "{{ influxdb_database_name }}" + query: "select mean(value) from connections where region='zue01' and host='server01'" + register: connection + +- name: Print results from the query + debug: + var: connection.results +''' + +RETURN = ''' +results: + description: Result from the query + returned: success + type: list + sample: + - mean: 1245.5333333333333 + time: "1970-01-01T00:00:00Z" +''' + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils._text import to_native +from ansible.module_utils.influxdb import InfluxDb + + +class AnsibleInfluxDBRead(InfluxDb): + + def read_by_query(self, query): + client = self.connect_to_influxdb() + try: + rs = client.query(query) + if rs: + return list(rs.get_points()) + except Exception as e: + self.module.fail_json(msg=to_native(e)) + + +def main(): + argument_spec = InfluxDb.influxdb_argument_spec() + argument_spec.update( + query=dict(type='str', required=True), + database_name=dict(required=True, type='str'), + ) + module = AnsibleModule( + argument_spec=argument_spec, + supports_check_mode=True + ) + + influx = AnsibleInfluxDBRead(module) + query = module.params.get('query') + results = influx.read_by_query(query) + module.exit_json(changed=True, results=results) + + +if __name__ == '__main__': + main()