Support datetime.date object in module result (#70595)

* Support datetime.date object in module result

Fixes #70583

* change blank lines for pep8 sanity test
This commit is contained in:
jabdr 2020-07-14 17:42:40 +02:00 committed by GitHub
parent 40591d5fbb
commit 0690b68bd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 1 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- Add support for datetime.date object type in module result (https://github.com/ansible/ansible/issues/70583).

View file

@ -391,7 +391,7 @@ def _remove_values_conditions(value, no_log_strings, deferred_removals):
if omit_me in stringy_value:
return 'VALUE_SPECIFIED_IN_NO_LOG_PARAMETER'
elif isinstance(value, datetime.datetime):
elif isinstance(value, (datetime.datetime, datetime.date)):
value = value.isoformat()
else:
raise TypeError('Value of unknown type: %s, %s' % (type(value), value))

View file

@ -0,0 +1,19 @@
#!/usr/bin/python
# Most of these names are only available via PluginLoader so pylint doesn't
# know they exist
# pylint: disable=no-name-in-module
from __future__ import absolute_import, division, print_function
__metaclass__ = type
from ansible.module_utils.basic import AnsibleModule
import datetime
module = AnsibleModule(argument_spec=dict(
datetime=dict(type=str, required=True),
date=dict(type=str, required=True),
))
result = {
'datetime': datetime.datetime.strptime(module.params.get('datetime'), '%Y-%m-%dT%H:%M:%S'),
'date': datetime.datetime.strptime(module.params.get('date'), '%Y-%m-%d').date(),
}
module.exit_json(**result)

View file

@ -97,3 +97,15 @@
- file:
path: "{{ output_dir }}/nulltest"
state: absent
- name: Test that date and datetime in module output works
test_datetime:
date: "2020-10-05"
datetime: "2020-10-05T10:05:05"
register: datetimetest
- name:
assert:
that:
- datetimetest.date == '2020-10-05'
- datetimetest.datetime == '2020-10-05T10:05:05'

View file

@ -8,11 +8,13 @@ __metaclass__ = type
import json
import sys
import datetime
import pytest
EMPTY_INVOCATION = {u'module_args': {}}
DATETIME = datetime.datetime.strptime('2020-07-13 12:50:00', '%Y-%m-%d %H:%M:%S')
class TestAnsibleModuleExitJson:
@ -26,6 +28,10 @@ class TestAnsibleModuleExitJson:
{'msg': 'success', 'changed': True, 'invocation': EMPTY_INVOCATION}),
({'msg': 'nochange', 'changed': False},
{'msg': 'nochange', 'changed': False, 'invocation': EMPTY_INVOCATION}),
({'msg': 'message', 'date': DATETIME.date()},
{'msg': 'message', 'date': DATETIME.date().isoformat(), 'invocation': EMPTY_INVOCATION}),
({'msg': 'message', 'datetime': DATETIME},
{'msg': 'message', 'datetime': DATETIME.isoformat(), 'invocation': EMPTY_INVOCATION}),
)
# pylint bug: https://github.com/PyCQA/pylint/issues/511