How to Import a Kibana Dashboard with Ansible?

We are upgrading from 7.9.3 to 7.17.8. I see that the method to import Kibana dashboards has changed. I can successfully import a dashboard using curl and using Postman.

curl -k -u <username>:<password> -X POST https://aln-nbadev4.labs.netscout.com:5601/api/saved_objects/_import?overwrite=true -H 'kbn-xsrf: true' --form file=@roles/kibana/files/database-monitor.ndjson

We normally install our dashboards using Ansible and it worked great using the old /api/kibana/dashboards/import URL. However, I can't figure out how to get Ansible to work correctly to install a dashboard with this new API.

- name: Load Kibana dashboards
  uri:
    url: "{{kibana_url}}/api/saved_objects/_import?overwrite=true"
    method: POST
    headers:
      kbn-version: "{{es_stack_version}}"
      kbn-xsrf: true
    body-format: raw
    user: "{{user.name}}"
    password: "{{user.password}}"
    force_basic_auth: yes
    validate_certs: no
    body: "file=@{{item}}"
  with_items:
    - database-monitor.ndjson
  run_once: True

When I try this I get:
msg: 'Status code was 415 and not [200]: HTTP Error 415: Unsupported Media Type'

Ansible doesn't support it. We just use a shell or command module with the same curl command u posted.

1 Like

Here's the solution as implemented:

- name: Load Kibana dashboards via shell
  shell: "curl -k -u {{user.name}}:{{user.password}} \
    -X POST {{kibana_url}}/api/saved_objects/_import?overwrite=true -H 'kbn-xsrf: true' \
    --form file=@{{playbook_dir}}/<path>/{{item}}"
  args:
    warn: False
  with_items:
    - database-monitor.ndjson
  run_once: True

Set "warn" to false to keep ansible from displaying a suggestion to use the uri command instead.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.