Skip to content

Instantly share code, notes, and snippets.

@aderixon
Created November 13, 2018 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aderixon/9e5550b8f91de3dee38936d56e75dd7d to your computer and use it in GitHub Desktop.
Save aderixon/9e5550b8f91de3dee38936d56e75dd7d to your computer and use it in GitHub Desktop.
Example of creating Graylog API token in Ansible
# Ansible tasks to obtain an existing API token from Graylog or
# create a new token if there isn't one.
# (Token can be used to create utility scripts from templates.)
# Token will be named 'ansible'.
# Illustrates use of uri module to interact with REST API,
# and JSON parsing in Ansible.
# Requires Graylog admin user & password.
- set_fact:
graylog_token: ''
# get or create a new API token
- name: check for existing Graylog admin token
block:
- uri:
url: "http://localhost:9000/api/users/admin/tokens/"
user: admin
password: "{{ graylog_password }}"
headers:
Accept: application/json
return_content: yes
force_basic_auth: yes
register: graylog_tokens
- set_fact:
graylog_token: "{{ (graylog_tokens.json.tokens | selectattr('name', 'match', '^ansible$') | map(attribute='token') | list | first | default('')) }}"
when: (graylog_tokens.json.tokens | length) > 0
check_mode: no
tags:
- graylog-config
- graylog-api
# create a new token if we didn't find an existing one
- name: create new Graylog token
block:
- uri:
url: "http://localhost:9000/api/users/admin/tokens/ansible"
user: admin
password: "{{ graylog_password }}"
method: POST
return_content: yes
force_basic_auth: yes
body: '{}'
body_format: json
register: graylog_new_token
- set_fact:
graylog_token: "{{ graylog_new_token.json.token }}"
when: graylog_new_token.status is defined and graylog_new_token.status == 200
when: not graylog_token
tags:
- graylog-config
- graylog-api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment