From bfb52641380710664c166457a222b253cb4f6c2b Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Thu, 11 Jan 2018 09:10:14 -0600 Subject: [PATCH] Support datetime serialization in json encoder. Fixes #34312 (#34516) --- lib/ansible/plugins/filter/core.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index 573363c7820..f4487f7d245 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -35,7 +35,7 @@ import uuid import yaml from collections import MutableMapping, MutableSequence -from datetime import datetime +import datetime from functools import partial from random import Random, SystemRandom, shuffle @@ -69,6 +69,8 @@ class AnsibleJSONEncoder(json.JSONEncoder): def default(self, o): if isinstance(o, HostVars): return dict(o) + elif isinstance(o, (datetime.date, datetime.datetime)): + return o.isoformat() else: return super(AnsibleJSONEncoder, self).default(o) @@ -126,7 +128,7 @@ def to_bool(a): def to_datetime(string, format="%Y-%m-%d %H:%M:%S"): - return datetime.strptime(string, format) + return datetime.datetime.strptime(string, format) def strftime(string_format, second=None):