Created
November 13, 2018 11:30
-
-
Save aderixon/9e5550b8f91de3dee38936d56e75dd7d to your computer and use it in GitHub Desktop.
Example of creating Graylog API token in Ansible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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