Skip to content

Instantly share code, notes, and snippets.

@kaz29 kaz29/ansible.cfg
Created Mar 13, 2018

What would you like to do?
Ansible custom module for ansible
ssh_args=-o ControlMaster=auto -o ControlPersist=30m
# -*- coding: utf-8 -*-
# (c) 2018, Kaz Watanabe <>
ANSIBLE_METADATA = {'status': ['stableinterface'],
'supported_by': 'community',
'version': '1.0'}
module: mackerel
short_description: Send Mackerek graph annotation
version_added: "1.6"
author: "kaz29 (@kaz_29)"
html_escape_table = {
'&': "&amp;",
'>': "&gt;",
'<': "&lt;",
'"': "\"",
"'": "\'",
def html_escape(text):
'''Produce entities within text.'''
return "".join(html_escape_table.get(c,c) for c in text)
def build_payload_for_mackerel(module, service, title, description, from_time, to_time, roles):
payload = {
'title': title,
'service': service,
'from': from_time,
'to': to_time,
if description is not None:
payload['description'] = description
if roles is not None:
payload['roles'] = roles
return payload
def do_post_graph_annotation_mackerel(module, apikey, payload):
request_url = BASE_URL % ('/graph-annotations')
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-Api-Key': apikey,
response, info = fetch_url(module=module, url=request_url, headers=headers, method='POST', data=payload)
if info['status'] != 200:
module.fail_json(msg=" failed to send %s to %s: %s" % (payload, request_url, info['msg']))
def main():
module = AnsibleModule(
argument_spec = dict(
apikey = dict(type='str', required=True, no_log=True),
service = dict(type='str', required=True, default=None),
title = dict(type='str', required=True, default=None),
description = dict(type='str', required=False, default=None),
from_time = dict(type='int', required=True, default=None),
to_time = dict(type='int', required=True, default=None),
roles = dict(type='list', required=False, default=None)
apikey = module.params['apikey']
service = module.params['service']
title = module.params['title']
description = module.params['description']
from_time = module.params['from_time']
to_time = module.params['to_time']
roles = module.params['roles']
payload = build_payload_for_mackerel(module, service, title, description, from_time, to_time, roles)
do_post_graph_annotation_mackerel(module, apikey, payload)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
if __name__ == '__main__':
- name: Send graph annotation to Mackerel
module: mackerel
title: "{{ inventory_hostname }} provisioning completed!"
apikey: "APIKEY"
service: "SERVICE NAME"
from_time: "{{ lookup('pipe','date +%s') }}"
to_time: "{{ lookup('pipe','date +%s') }}"
become: no
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.