0

How can I create a user in Influxdb using ansible module ?

In influxdb ansible docs , I could not find that how to create a user.

I am trying to create it via influxdb API, but have not got success in that.

  - name: create user in influxdb
    uri:
      url: "http://localhost:8086/query"
      method: POST
      body: "--data-urlencode 'q=CREATE USER myuser WITH PASSWORD 'mypass' WITH ALL PRIVILEGES'"
      body_format: raw

The response is,

fatal: [192.168.122.62]: FAILED! => {"changed": false, "connection": "close", "content": "{\"error\":\"missing required parameter \\\"q\\\"\"}\n", "content_length": "45", "content_type": "application/json", "date": "Wed, 05 Jul 2017 12:08:26 GMT", "failed": true, "json": {"error": "missing required parameter \"q\""}, "msg": "Status code was not [200]: HTTP Error 400: Bad Request", "redirected": false, "request_id": "a6c36bfd-617a-11e7-800c-000000000000", "status": 400, "url": "http://localhost:8086/query", "x_influxdb_version": "1.2.2"}
Luv33preet
  • 1,686
  • 7
  • 33
  • 66

4 Answers4

1

I had a nightmare with quotes... BUT this one worked for me:

  - name: "[Centos7_InfluxDB] Create InfluxDB user"
    command: 'influx -database {{item.database}} -execute "CREATE USER {{item.user}} WITH PASSWORD
             {{item.pass}} WITH ALL PRIVILEGES"'
    with_items:
      - { database: "sample_database", user: "adminuser", pass: "'adminpass'" }
1

This code should work as you expect :

- name: create user in influxdb
  uri:
    url: "http://localhost:8086/query"
    method: POST
    body: "q=CREATE USER myuser WITH PASSWORD 'mypass' WITH ALL PRIVILEGES"
broferek
  • 81
  • 3
0

You can easily create a user using the command module.

- name: Create InfluxDB user
  command: "influx -execute \"CREATE USER {{item.user}} WITH PASSWORD 
           '{{item.pass}}' WITH ALL PRIVILEGES\""
  with_items:
    - { user: 'admin', pass: 'admin' }

This should be a lot more efficient than using the REST api.

0

If anyone's wondering about that as well - just keep in mind that there are community modules for Ansible that already do everything you want, without having to do it with raw/command modules

Damian Fajfer
  • 109
  • 2
  • 4