From cb1e3dab0d3e58eb4484ee5fee5ab8303b008f6f Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 31 Oct 2016 15:36:24 -0500 Subject: [PATCH] Add 'type' filter for display the underlying python type of a variable (#18242) * Add 'type' filter for display the underlying python type of a variable * Update playbooks_filters.rst Minor copyedit. --- docsite/rst/playbooks_filters.rst | 12 ++++++++++++ lib/ansible/plugins/filter/core.py | 3 +++ 2 files changed, 15 insertions(+) diff --git a/docsite/rst/playbooks_filters.rst b/docsite/rst/playbooks_filters.rst index 562fb385a8d..cff5314e2b1 100644 --- a/docsite/rst/playbooks_filters.rst +++ b/docsite/rst/playbooks_filters.rst @@ -514,6 +514,18 @@ To get date object from string use the `to_datetime` filter, (new in version in # get amount of seconds between two dates, default date format is %Y-%d-%m %H:%M:%S but you can pass your own one {{ (("2016-08-04 20:00:12"|to_datetime) - ("2015-10-06"|to_datetime('%Y-%d-%m'))).seconds }} +Debugging Filters +----------------- + +.. versionadded:: 2.3 + +Use the ``type`` filter to display the underlying Python type of a variable. +This can be useful in debugging in situations where you may need to know the exact +type of a variable:: + + {{ myvar | type }} + + A few useful filters are typically added with each new Ansible release. The development documentation shows how to extend Ansible filters by writing your own as plugins, though in general, we encourage new ones to be added to core so everyone can make use of them. diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index 81463ce0e34..bbd7b295b3d 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -525,4 +525,7 @@ class FilterModule(object): # skip testing 'skipped' : skipped, 'skip' : skipped, + + # debug + 'type': lambda o: o.__class__.__name__, }