Skip to content

Commit

Permalink
Specify the timezone in timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
ares committed Mar 13, 2021
1 parent c13ed21 commit fb73f96
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions plugins/callback/foreman.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@
from ansible.module_utils._text import to_text
from ansible.plugins.callback import CallbackBase

def get_now():
"""
Return the current timestamp as a string to be sent over the network.
The time is always in UTC timezone and printed in a rormat Ruby
DateTime object can easily parse.
"""
return datetime.datetime.now(datetime.timezone.utc).isoformat(sep=" ", timespec="seconds")

class CallbackModule(CallbackBase):
CALLBACK_VERSION = 2.0
Expand All @@ -101,7 +108,6 @@ class CallbackModule(CallbackBase):
"Content-Type": "application/json",
"Accept": "application/json"
}
TIME_FORMAT = "%Y-%m-%d %H:%M:%S %f"

def __init__(self):
super(CallbackModule, self).__init__()
Expand Down Expand Up @@ -161,7 +167,7 @@ def send_facts(self, host, data):
properly.
"""
data["_type"] = "ansible"
data["_timestamp"] = datetime.now().strftime(self.TIME_FORMAT)
data["_timestamp"] = get_now()
facts = {"name": host,
"facts": data,
}
Expand Down Expand Up @@ -214,11 +220,10 @@ def send_reports(self, stats):
status["skipped"] = sum['skipped']
log = self._build_log(self.items[host])
metrics["time"] = {"total": int(time.time()) - self.start_time}
now = datetime.now().strftime(self.TIME_FORMAT)
report = {
"config_report": {
"host": host,
"reported_at": now,
"reported_at": get_now(),
"metrics": metrics,
"status": status,
"logs": log,
Expand Down

0 comments on commit fb73f96

Please sign in to comment.