Skip to content

Commit

Permalink
feat: add credentials for influxdb (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrPsychick authored Dec 19, 2020
1 parent c48f8d4 commit d3c9560
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions examples/basic.yml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
22 changes: 22 additions & 0 deletions tasks/influxdb_database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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'

Expand All @@ -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

Expand All @@ -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'

Expand Down Expand Up @@ -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
Expand All @@ -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'
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
12 changes: 12 additions & 0 deletions tasks/influxdb_measurement.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 }}'
Expand All @@ -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

Expand All @@ -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

Expand Down
18 changes: 16 additions & 2 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit d3c9560

Please sign in to comment.