From d3c9560bf533f10dbc5debe7e4f0b90523ceda21 Mon Sep 17 00:00:00 2001 From: DrPsychick Date: Sat, 19 Dec 2020 16:06:02 +0100 Subject: [PATCH] feat: add credentials for influxdb (#13) --- examples/basic.yml | 2 ++ tasks/influxdb_database.yml | 22 ++++++++++++++++++++++ tasks/influxdb_measurement.yml | 12 ++++++++++++ tasks/main.yml | 18 ++++++++++++++++-- 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/examples/basic.yml b/examples/basic.yml index 133f2ae..3df4899 100644 --- a/examples/basic.yml +++ b/examples/basic.yml @@ -1,6 +1,8 @@ --- ansible_influx_url: "http://localhost:8086" +ansible_influx_user: "" +ansible_influx_pass: "" # timeout for HTTP call from ansible # ansible_influx_timeout: 600 diff --git a/tasks/influxdb_database.yml b/tasks/influxdb_database.yml index 7903c9b..5b13bc3 100644 --- a/tasks/influxdb_database.yml +++ b/tasks/influxdb_database.yml @@ -154,6 +154,8 @@ method: POST body: "q=CREATE DATABASE {{ ifx_db.name }}" return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" when: ifx_db.name not in ifx_dbs register: ansible_influx_db_create changed_when: ansible_influx_db_create.status == 200 and ansible_influx_db_create.json is defined @@ -165,6 +167,8 @@ method: POST body: "q=SHOW RETENTION POLICIES" return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_source_rps when: ifx_source_defined @@ -174,6 +178,8 @@ method: POST body: "q=SHOW MEASUREMENTS" return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_source_mms when: ifx_source_defined and ifx_mode != 'compact' @@ -183,6 +189,8 @@ method: POST body: "q=SHOW RETENTION POLICIES" return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_rps when: ansible_influx_db_create is not changed @@ -192,6 +200,8 @@ method: POST body: "q=SHOW MEASUREMENTS" return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_mms when: ansible_influx_db_create is not changed and ifx_mode != 'compact' @@ -246,6 +256,8 @@ q=CREATE RETENTION POLICY "{{ ifx_rp_name }}" ON "{{ ifx_db.name }}" DURATION {{ ifx_rp_duration }} REPLICATION 1 {{ ifx_rp_default_string }} return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" when: ansible_influx_db_create is changed or (ifx_rps is defined and ifx_rp_name not in ifx_rps) register: ansible_influx_rp_create changed_when: ansible_influx_rp_create.status == 200 and ansible_influx_rp_create.json is defined @@ -270,6 +282,8 @@ FROM "{{ ifx_db.source.name }}"."{{ ifx_source_rp_name }}"./^({{ ifx_db.measurements|join('|') }})$/ WHERE time >= {{ ifx_backfill_start }}000ms return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_newmetrics when: ifx_source_drop and ifx_source_exists - name: '{{ db_prefix }} Set count_newmetrics' @@ -299,6 +313,8 @@ WHERE time >= {{ ifx_backfill_start }}000ms GROUP BY * return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_bf_closegap when: ifx_source_drop and ifx_source_exists and count_newmetrics|int > 0 @@ -312,6 +328,8 @@ WHERE time >= {{ ifx_backfill_start2 }}000ms GROUP BY * return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_bf_closegap2 when: ifx_source_drop and ifx_source_exists and count_newmetrics|int > 0 @@ -341,6 +359,8 @@ body: > q=ALTER RETENTION POLICY "{{ ifx_rp_name }}" ON "{{ ifx_db.name }}" DEFAULT return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_rp_alter when: ifx_source_drop and ifx_source_exists changed_when: ansible_influx_rp_alter.status == 200 and ansible_influx_rp_alter.json is defined @@ -365,5 +385,7 @@ body: > q=DROP RETENTION POLICY "{{ ifx_db.source.rp_name }}" ON "{{ ifx_db.source.name }}" return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_source_drop when: ifx_source_drop and ifx_source_exists and ifx_source_rp_name diff --git a/tasks/influxdb_measurement.yml b/tasks/influxdb_measurement.yml index b528d12..5247d49 100644 --- a/tasks/influxdb_measurement.yml +++ b/tasks/influxdb_measurement.yml @@ -62,6 +62,8 @@ method: POST body: "q=SHOW FIELD KEYS FROM {{ source_mm }}" return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_mm_fields when: ifx_source_defined @@ -73,6 +75,8 @@ q=SELECT COUNT(*) FROM {{ source_mm }} WHERE time >= now() - {{ ifx_rp_duration }} return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_mm_count when: ifx_bf_enabled and measurement not in ifx_mms and ifx_source_defined and ifx_bf_stats @@ -115,6 +119,8 @@ AND time < now() - {{ seq|int - ifx_bf_step|int }}{{ ifx_rp_unit }} {{ bf_where }} GROUP BY {{ bf_interval_string }}{{ bf_groupby_string }} return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" with_sequence: start={{ ifx_rp_amount|int }} end={{ ifx_bf_step|int }} stride=-{{ ifx_bf_step|int }} loop_control: { loop_var: seq } register: ansible_influx_mm_backfill @@ -154,6 +160,8 @@ url: "{{ ansible_influx_url }}/query" method: POST body: 'q=DROP CONTINUOUS QUERY "{{ drop_cq_name }}" ON "{{ ifx_db.name }}"' + url_username: "{{ username }}" + url_password: "{{ password }}" when: ifx_source_defined and not ifx_source_drop and drop_cq_name in ifx_cqs - name: '{{ mm_prefix }} Creating continuous query {{ cq_name }}' @@ -166,6 +174,8 @@ BEGIN {{ cq_select }} INTO {{ target_mm }} FROM {{ source_mm }} {{ cq_where }} GROUP BY {{ cq_interval_string }}{{ cq_groupby_string }} END return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_mm_cq when: ifx_source_defined and not ifx_source_drop and ifx_cq_enabled @@ -175,6 +185,8 @@ method: POST body: "q=SELECT COUNT(*) FROM {{ target_mm }}" return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_mm_count when: ifx_bf_enabled and ifx_source_defined and mm_backfill_result|int > 0 diff --git a/tasks/main.yml b/tasks/main.yml index 943071b..515bc4b 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -3,9 +3,17 @@ - name: Set variables set_fact: ifx_start: "{{ lookup('pipe', 'date +%s') }}" + username: "{{ ansible_influx_user|default('') }}" + password: "{{ ansible_influx_pass|default('') }}" - name: Get databases - uri: url="{{ ansible_influx_url }}/query" method=POST body="q=SHOW DATABASES" return_content=yes + uri: + url: "{{ ansible_influx_url }}/query" + method: POST + body: "q=SHOW DATABASES" + return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_dbs changed_when: false failed_when: ansible_influx_dbs.status != 200 or (ansible_influx_dbs.rc|default(0) == 1) @@ -21,7 +29,13 @@ # needed to check if RP needs to be created - name: Show continuous queries - uri: url="{{ ansible_influx_url }}/query" method=POST body="q=SHOW CONTINUOUS QUERIES" return_content=yes + uri: + url: "{{ ansible_influx_url }}/query" + method: POST + body: "q=SHOW CONTINUOUS QUERIES" + return_content: yes + url_username: "{{ username }}" + url_password: "{{ password }}" register: ansible_influx_cqs changed_when: false failed_when: (ansible_influx_cqs.rc|default(0) == 1)