├── .gitignore ├── LICENSE ├── README.md ├── Vagrantfile ├── callback_plugins └── json_logs.py └── playbook.yml /.gitignore: -------------------------------------------------------------------------------- 1 | .vagrant/ 2 | *.pyc -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Peter Souter 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | =============== 2 | ansible-json 3 | =============== 4 | 5 | An Ansible plugin for outputting logs to json 6 | 7 | Right now, it only outputs to stdout, but in the future it will output to a `.json` file. 8 | 9 | # Usage 10 | 11 | Make a directory called `callback_plugins` next to your playbook and put `json_logs.py` inside of it 12 | 13 | ```bash 14 | mkdir callback_plugins 15 | cd callback_plugins 16 | wget https://raw.githubusercontent.com/petems/ansible-json/master/callback_plugins/json_logs.py 17 | ``` -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | VAGRANTFILE_API_VERSION = "2" 5 | 6 | Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| 7 | 8 | config.vm.box = "hashicorp/precise64" 9 | 10 | config.vm.provision "ansible" do |ansible| 11 | ansible.playbook = "playbook.yml" 12 | end 13 | 14 | end 15 | -------------------------------------------------------------------------------- /callback_plugins/json_logs.py: -------------------------------------------------------------------------------- 1 | # adapted from https://gist.github.com/cliffano/9868180 2 | import os 3 | import time 4 | import json 5 | from json import JSONEncoder 6 | 7 | def json_log(res, host): 8 | if type(res) == type(dict()): 9 | if 'verbose_override' not in res: 10 | res.update({"host": host}) 11 | combined_json = JSONEncoder().encode(res) 12 | print(combined_json) 13 | 14 | class CallbackModule(object): 15 | 16 | def on_any(self, *args, **kwargs): 17 | pass 18 | 19 | def runner_on_failed(self, host, res, ignore_errors=False): 20 | json_log(res, host) 21 | 22 | def runner_on_ok(self, host, res): 23 | json_log(res, host) 24 | 25 | def runner_on_error(self, host, msg): 26 | pass 27 | 28 | def runner_on_skipped(self, host, item=None): 29 | pass 30 | 31 | def runner_on_unreachable(self, host, res): 32 | json_log(res, host) 33 | 34 | def runner_on_no_hosts(self): 35 | pass 36 | 37 | def runner_on_async_poll(self, host, res, jid, clock): 38 | json_log(res, host) 39 | 40 | def runner_on_async_ok(self, host, res, jid): 41 | json_log(res, host) 42 | 43 | def runner_on_async_failed(self, host, res, jid): 44 | json_log(res, host) 45 | 46 | def playbook_on_start(self): 47 | pass 48 | 49 | def playbook_on_notify(self, host, handler): 50 | pass 51 | 52 | def playbook_on_no_hosts_matched(self): 53 | pass 54 | 55 | def playbook_on_no_hosts_remaining(self): 56 | pass 57 | 58 | def playbook_on_task_start(self, name, is_conditional): 59 | pass 60 | 61 | def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None): 62 | pass 63 | 64 | def playbook_on_setup(self): 65 | pass 66 | 67 | def playbook_on_import_for_host(self, host, imported_file): 68 | pass 69 | 70 | def playbook_on_not_import_for_host(self, host, missing_file): 71 | pass 72 | 73 | def playbook_on_play_start(self, pattern): 74 | pass 75 | 76 | def playbook_on_stats(self, stats): 77 | pass -------------------------------------------------------------------------------- /playbook.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: all 3 | tasks: 4 | - name: say hello world! 5 | command: echo 'Hello World' --------------------------------------------------------------------------------