1 # (C) 2012, Michael DeHaan, <michael.dehaan@gmail.com>
3 # This file is part of Ansible
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
23 def task_error(host, data):
24 logging.info("task_error: host=%s,data=%s" % (host, data))
26 if type(data) == dict:
27 invocation = data.pop('invocation', {})
29 notify_host("localhost", host, "failed")
31 class CallbackModule(object):
33 logs playbook results, per host, in /var/log/ansible/hosts
36 def on_any(self, *args, **kwargs):
39 def runner_on_failed(self, host, res, ignore_errors=False):
42 def runner_on_ok(self, host, res):
45 def runner_on_skipped(self, host, item=None):
48 def runner_on_unreachable(self, host, res):
51 def runner_on_no_hosts(self):
54 def runner_on_async_poll(self, host, res, jid, clock):
57 def runner_on_async_ok(self, host, res, jid):
60 def runner_on_async_failed(self, host, res, jid):
63 def playbook_on_start(self):
66 def playbook_on_notify(self, host, handler):
69 def playbook_on_no_hosts_matched(self):
72 def playbook_on_no_hosts_remaining(self):
75 def playbook_on_task_start(self, name, is_conditional):
78 def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None):
81 def playbook_on_setup(self):
84 def playbook_on_import_for_host(self, host, imported_file):
87 def playbook_on_not_import_for_host(self, host, missing_file):
90 def playbook_on_play_start(self, name):
93 def playbook_on_stats(self, stats):
94 logging.info("playbook_on_stats enter")
95 hosts = sorted(stats.processed.keys())
96 host_vars = self.playbook.inventory.get_variables(hosts[0])
97 cluster_name = host_vars['cluster_name']
102 summary = stats.summarize(host)
104 if summary['failures'] > 0:
106 if summary['unreachable'] > 0:
109 if failures or unreachable:
111 notify_host("localhost", host, "error")
115 clusterhost_name = host + "." + cluster_name
116 notify_host("localhost", clusterhost_name, "succ")
119 def raise_for_status(resp):
120 if resp.status < 200 or resp.status > 300:
121 raise RuntimeError("%s, %s, %s" % (resp.status, resp.reason, resp.read()))
125 credential['email'] = "admin@huawei.com"
126 credential['password'] = "admin"
127 url = "/api/users/token"
128 headers = {"Content-type": "application/json",
130 conn.request("POST", url, json.dumps(credential), headers)
131 resp = conn.getresponse()
133 raise_for_status(resp)
134 return json.loads(resp.read())["token"]
136 def notify_host(compass_host, host, status):
138 body = {"ready": True}
139 url = "/api/clusterhosts/%s/state_internal" % host
140 elif status == "error":
141 body = {"state": "ERROR"}
142 host = host.strip("host")
143 url = "/api/clusterhosts/%s/state" % host
145 logging.error("notify_host: host %s with status %s is not supported" \
149 headers = {"Content-type": "application/json",
152 conn = httplib.HTTPConnection(compass_host, 80)
154 headers["X-Auth-Token"] = token
155 logging.info("host=%s,url=%s,body=%s,headers=%s" % (compass_host,url,json.dumps(body),headers))
156 conn.request("POST", url, json.dumps(body), headers)
157 resp = conn.getresponse()
159 raise_for_status(resp)
160 logging.info("notify host status success!!! status=%s, body=%s" % (resp.status, resp.read()))
161 except Exception as e:
162 logging.error("http request failed %s" % str(e))
167 if __name__ == "__main__":
168 if len(sys.argv) != 3:
169 logging.error("params: host, status is need")
174 notify_host(host, status)